Skip to content

Commit

Permalink
Merge pull request #184 from OpenHistoricalMap/1ec5-label-dates-430
Browse files Browse the repository at this point in the history
Append date range to feature labels. Looks great
  • Loading branch information
danrademacher authored Dec 12, 2023
2 parents 83092bc + bac3fb4 commit cb4bb64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,10 @@ en:
lock:
suggestion: 'The "{label}" field is locked because there is a Wikidata tag. You can delete it or edit the tags in the "Tags" section.'
display_name:
# format for the label of a feature that has a name and date range
dated: "{name} [{dateRange}]"
# compact format for expressing a range of years
date_range: "{start}–{end}"
direction: "{direction}"
network: "{network}"
from_to: "from {from} to {to}"
Expand Down
38 changes: 36 additions & 2 deletions modules/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,43 @@ export function utilGetAllNodes(ids, graph) {


export function utilDisplayName(entity) {
let dateRange;
if (entity.tags.start_date || entity.tags.end_date) {
// Parse the start and end dates, discarding unnecessary precision, since the display name must be fairly succinct.
let start = entity.tags.start_date && utilNormalizeDateString(entity.tags.start_date);
if (start) {
delete start.localeOptions.month;
delete start.localeOptions.day;
}
let end = entity.tags.end_date && utilNormalizeDateString(entity.tags.end_date);
if (end) {
delete end.localeOptions.month;
delete end.localeOptions.day;

if (start) {
// If the start date has an explicit era, then so must the end date.
end.localeOptions.era = start.localeOptions.era;
}
}

let dates = [
start ? new Intl.DateTimeFormat(localizer.languageCode(), start.localeOptions).format(start.date) : '',
end ? new Intl.DateTimeFormat(localizer.languageCode(), end.localeOptions).format(end.date) : '',
];

// Omit a redundant date.
if (dates[0] === dates[1]) {
dateRange = dates[0];
} else {
dateRange = t('inspector.display_name.date_range', {start: dates[0], end: dates[1]});
}
}

var localizedNameKey = 'name:' + localizer.languageCode().toLowerCase();
var name = entity.tags[localizedNameKey] || entity.tags.name || '';
if (name) return name;
if (name) {
return dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
}

var tags = {
direction: entity.tags.direction,
Expand Down Expand Up @@ -218,7 +252,7 @@ export function utilDisplayName(entity) {
name = t('inspector.display_name.' + keyComponents.join('_'), tags);
}

return name;
return name && dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
}


Expand Down
10 changes: 10 additions & 0 deletions test/spec/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ describe('iD.util', function() {
// BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae
expect(iD.utilDisplayName({tags: {network: 'BART', ref: 'Yellow', from: 'Antioch', to: 'Millbrae', via: 'Pittsburg/Bay Point;San Francisco International Airport', route: 'subway'}})).to.eql('BART Yellow from Antioch to Millbrae via Pittsburg/Bay Point;San Francisco International Airport');
});
it('appends a date range', function() {
expect(iD.utilDisplayName({tags: {name: 'Arbre Perdu', 'name:en': 'Lost Tree'}})).to.eql('Lost Tree');
expect(iD.utilDisplayName({tags: {name: 'Arbre du Ténéré', 'name:en': 'Tree of Ténéré', start_date: '1673', end_date: '1973'}})).to.eql('Tree of Ténéré [1673–1973]');
expect(iD.utilDisplayName({tags: {name: 'Charter Oak', start_date: '1614', end_date: '1856-08-21'}})).to.eql('Charter Oak [1614–1856]');
expect(iD.utilDisplayName({tags: {name: 'Prometheus', start_date: '-2899', end_date: '1964-08'}})).to.eql('Prometheus [2900 BC–1964 AD]');
expect(iD.utilDisplayName({tags: {name: 'ජය ශ්‍රී මහා බෝධිය', 'name:en': 'Jaya Sri Maha Bodhi', start_date: '-287'}})).to.eql('Jaya Sri Maha Bodhi [288 BC–]');
expect(iD.utilDisplayName({tags: {name: 'Son of the Tree That Owns Itself', start_date: '1946-12-04'}})).to.eql('Son of the Tree That Owns Itself [1946–]');
expect(iD.utilDisplayName({tags: {name: 'Great Elm', end_date: '1876-02-15'}})).to.eql('Great Elm [–1876]');
expect(iD.utilDisplayName({tags: {name: 'Capitol Christmas Tree', start_date: '2021-11-19', end_date: '2021-12-25'}})).to.eql('Capitol Christmas Tree [2021]');
});
});

describe('utilOldestID', function() {
Expand Down

0 comments on commit cb4bb64

Please sign in to comment.