Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append date range to feature labels #184

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading