Skip to content

Commit f666689

Browse files
authored
include all tags of a preset in the taginfo output (#223)
instead of only the last key-value pair in the object
1 parent 0d565ab commit f666689

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

lib/build.js

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,19 @@ function generateTranslations(fields, presets, tstrings, searchableFieldIDs) {
486486
}
487487

488488

489+
/**
490+
* @import * as Taginfo from 'taginfo-projects'
491+
*
492+
* this library doesn't follow the type-defintions strictly, it uses `string[]` instead
493+
* of `string` for the description field.
494+
*
495+
* @typedef {Omit<Taginfo.Tag, "description"> & { description?: string[] }} TaginfoTag
496+
* @typedef {Omit<Taginfo.Schema, "tags"> & { tags: TaginfoTag[] }} TaginfoSchema
497+
*/
498+
499+
/**
500+
* @param {Taginfo.Project} projectInfo
501+
*/
489502
function generateTaginfo(presets, fields, deprecated, discarded, tstrings, projectInfo) {
490503

491504
const packageInfo = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
@@ -504,6 +517,7 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
504517
}
505518
}
506519

520+
/** @type {TaginfoSchema} */
507521
let taginfo = {
508522
data_format: 1,
509523
project: projectInfo,
@@ -515,43 +529,51 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
515529
if (preset.suggestion) return;
516530
if (id.startsWith('@')) return;
517531

518-
let keys = Object.keys(preset.tags);
519-
let last = keys[keys.length - 1];
520-
let tag = { key: last };
532+
/** @type {Record<string, Set<string>>} */
533+
const everyTag = {};
534+
for (const group of [preset.tags, preset.addTags, preset.removeTags]) {
535+
for (const key in group) {
536+
everyTag[key] ||= new Set();
537+
everyTag[key].add(preset.tags[key]);
538+
}
539+
}
521540

522-
if (!last) return;
541+
for (const key in everyTag) {
542+
for (const value of everyTag[key]) {
543+
/** @type {TaginfoTag} */
544+
let tag = { key };
545+
if (value !== '*') tag.value = value;
523546

524-
if (preset.tags[last] !== '*') {
525-
tag.value = preset.tags[last];
526-
}
527547

528-
let name = tstrings.presets[id].name;
529-
if (!name && preset.name.startsWith('{')) {
530-
name = tstrings.presets[preset.name.slice(1, -1)].name;
531-
}
532-
let legacy = (preset.searchable === false) ? ' (unsearchable)' : '';
533-
tag.description = [ `🄿 ${name}${legacy}` ];
548+
let name = tstrings.presets[id].name;
549+
if (!name && preset.name.startsWith('{')) {
550+
name = tstrings.presets[preset.name.slice(1, -1)].name;
551+
}
552+
let legacy = (preset.searchable === false) ? ' (unsearchable)' : '';
553+
tag.description = [ `🄿 ${name}${legacy}` ];
534554

535-
if (preset.geometry) {
536-
setObjectType(tag, preset);
537-
}
555+
if (preset.geometry) {
556+
setObjectType(tag, preset);
557+
}
538558

539-
// add icon
540-
if (/^maki-/.test(preset.icon)) {
541-
tag.icon_url = 'https://cdn.jsdelivr.net/gh/mapbox/maki/icons/' +
542-
preset.icon.replace(/^maki-/, '') + '.svg';
543-
} else if (/^temaki-/.test(preset.icon)) {
544-
tag.icon_url = 'https://cdn.jsdelivr.net/gh/rapideditor/temaki/icons/' +
545-
preset.icon.replace(/^temaki-/, '') + '.svg';
546-
} else if (/^fa[srb]-/.test(preset.icon)) {
547-
tag.icon_url = 'https://cdn.jsdelivr.net/gh/openstreetmap/iD@develop/svg/fontawesome/' +
548-
preset.icon + '.svg';
549-
} else if (/^iD-/.test(preset.icon)) {
550-
tag.icon_url = 'https://cdn.jsdelivr.net/gh/openstreetmap/iD@develop/svg/iD-sprite/presets/' +
551-
preset.icon.replace(/^iD-/, '') + '.svg';
552-
}
559+
// add icon
560+
if (/^maki-/.test(preset.icon)) {
561+
tag.icon_url = 'https://cdn.jsdelivr.net/gh/mapbox/maki/icons/' +
562+
preset.icon.replace(/^maki-/, '') + '.svg';
563+
} else if (/^temaki-/.test(preset.icon)) {
564+
tag.icon_url = 'https://cdn.jsdelivr.net/gh/rapideditor/temaki/icons/' +
565+
preset.icon.replace(/^temaki-/, '') + '.svg';
566+
} else if (/^fa[srb]-/.test(preset.icon)) {
567+
tag.icon_url = 'https://cdn.jsdelivr.net/gh/openstreetmap/iD@develop/svg/fontawesome/' +
568+
preset.icon + '.svg';
569+
} else if (/^iD-/.test(preset.icon)) {
570+
tag.icon_url = 'https://cdn.jsdelivr.net/gh/openstreetmap/iD@develop/svg/iD-sprite/presets/' +
571+
preset.icon.replace(/^iD-/, '') + '.svg';
572+
}
553573

554-
coalesceTags(taginfo, tag);
574+
coalesceTags(taginfo, tag);
575+
}
576+
}
555577
});
556578

557579
Object.keys(fields).forEach(id => {
@@ -567,6 +589,7 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
567589
}
568590

569591
keys.forEach(key => {
592+
/** @type {TaginfoTag} */
570593
let tag = { key: key };
571594

572595
let label = tstrings.fields[id].label;
@@ -580,6 +603,7 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
580603
if (field.options && !isRadio && field.type !== 'manyCombo') {
581604
field.options.forEach(value => {
582605
if (value === 'undefined' || value === '*' || value === '') return;
606+
/** @type {TaginfoTag} */
583607
let tag;
584608
if (field.type === 'multiCombo') {
585609
tag = { key: key + value };
@@ -607,6 +631,7 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
607631
let oldKeys = Object.keys(old);
608632
if (oldKeys.length === 1) {
609633
let oldKey = oldKeys[0];
634+
/** @type {TaginfoTag} */
610635
let tag = { key: oldKey };
611636

612637
let oldValue = old[oldKey];
@@ -629,6 +654,7 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
629654
if (!discarded) discarded = {};
630655
Object.keys(discarded).forEach(key => {
631656
let description = '🄳🄳 (discarded tag)';
657+
/** @type {TaginfoTag} */
632658
let tag = {
633659
key,
634660
description: [description]
@@ -643,6 +669,10 @@ function generateTaginfo(presets, fields, deprecated, discarded, tstrings, proje
643669
});
644670

645671

672+
/**
673+
* @param {TaginfoSchema} taginfo
674+
* @param {TaginfoTag} tag
675+
*/
646676
function coalesceTags(taginfo, tag) {
647677
if (!tag.key) return;
648678

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"shelljs": "^0.9.2"
2323
},
2424
"devDependencies": {
25+
"@types/taginfo-projects": "github:taginfo/taginfo-projects#6daafd2e30a86a8bfb9615855af2c2d73bd98279",
2526
"eslint": "^9.0.0",
2627
"jest": "^29.1.1"
2728
},

0 commit comments

Comments
 (0)