From 22adc985b84a09e74bf478ddec2d0b70697a99f4 Mon Sep 17 00:00:00 2001 From: Chad Ostrowski Date: Sun, 6 Aug 2017 14:02:14 -0400 Subject: [PATCH] Country flag names: use name from emojiLib * Always match on emoji character * For flag emojis where emojiLib data is found, use emojiLib name --- scripts/build-data.js | 53 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/scripts/build-data.js b/scripts/build-data.js index 2df3b92..016d379 100644 --- a/scripts/build-data.js +++ b/scripts/build-data.js @@ -5,6 +5,42 @@ var fs = require('fs'), mkdirp = require('mkdirp'), buildSearch = require('../src/utils/build-search') +var unifiedToNative = unified => String.fromCodePoint( + ...unified.split('-').map(s => parseInt(s, 16)) +); + +// Turn this: +// +// "uruguay": { +// "keywords": ["uy", "flag", "nation", "country", "banner"], +// "char": "๐Ÿ‡บ๐Ÿ‡พ", +// "fitzpatrick_scale": false, +// "category": "flags" +// }, +// +// into this: +// +// "๐Ÿ‡บ๐Ÿ‡พ": { +// "name": "uruguay", +// "keywords": ["uruguay", "uy", "flag", "nation", "country", "banner"], +// "char": "๐Ÿ‡บ๐Ÿ‡พ", +// "fitzpatrick_scale": false, +// "category": "flags" +// }, +var emojiLibByChar = Object.keys(emojiLib.lib).reduce( + (acc, shortName) => { + var data = Object.assign({}, emojiLib.lib[shortName]) + + data.keywords.unshift(shortName) + data.name = shortName + + acc[data.char] = data + + return acc + }, + {} +) + var categories = ['People', 'Nature', 'Foods', 'Activity', 'Places', 'Objects', 'Symbols', 'Flags'], data = { categories: [], emojis: {}, skins: {}, short_names: {} }, categoriesIndex = {} @@ -24,16 +60,25 @@ emojiData.sort((a, b) => { emojiData.forEach((datum) => { var category = datum.category, keywords = [], - categoryIndex + categoryIndex, + char = unifiedToNative(datum.unified), + emojiLibMatch = emojiLibByChar[char] if (!datum.category) { throw new Error('โ€œ' + datum.short_name + 'โ€ doesnโ€™t have a category') } datum.name || (datum.name = datum.short_name.replace(/\-/g, ' ')) + + if (datum.category == 'Flags' && emojiLibMatch) { + // prefer name from emojiLib ("Uruguay") + // instead of emojiData ("REGIONAL INDICATOR SYMBOL LETTERS UY") + datum.name = emojiLibMatch.name + } + datum.name = inflection.titleize(datum.name || '') - if (datum.category == 'Flags') { + if (datum.category == 'Flags' && !emojiLibMatch) { datum.name = datum.name.replace(/\s(\w+)$/, (letters) => letters.toUpperCase()) } @@ -45,8 +90,8 @@ emojiData.forEach((datum) => { datum.text = datum.text || '' delete datum.texts - if (emojiLib.lib[datum.short_name]) { - keywords = emojiLib.lib[datum.short_name].keywords + if (emojiLibMatch) { + keywords = emojiLibMatch.keywords } datum.search = buildSearch({