emoji-mart-lazyload/scripts/build-data.js

69 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var fs = require('fs'),
emojiData = require('emoji-data'),
inflection = require('inflection'),
mkdirp = require('mkdirp')
var categories = ['People', 'Nature', 'Foods', 'Activity', 'Places', 'Objects', 'Symbols', 'Flags'],
data = { categories: [], emojis: {}, skins: {} },
categoriesIndex = {}
categories.forEach((category, i) => {
data.categories[i] = { name: category, emojis: [] }
categoriesIndex[category] = i
})
emojiData.sort((a, b) => {
var aTest = a.sort_order || a.short_name,
bTest = b.sort_order || b.short_name
return aTest - bTest
})
emojiData.forEach((datum) => {
var category = datum.category,
shortName = datum.short_name,
categoryIndex
if (!datum.category) {
throw new Error('“' + datum.short_name + '” doesnt have a category')
}
datum.name || (datum.name = datum.short_name.replace(/\-/g, ' '))
datum.name = inflection.titleize(datum.name || '')
if (datum.category == 'Flags') {
datum.name = datum.name.replace(/\s(\w+)$/, (letters) => letters.toUpperCase())
}
if (!datum.name) {
throw new Error('“' + datum.short_name + '” doesnt have a name')
}
datum.emoticons = datum.texts || []
if (datum.text && !datum.emoticons.length) {
datum.emoticons = [datum.text]
}
delete datum.text
delete datum.texts
if (datum.category == 'Skin Tones') {
data.skins[datum.short_name] = datum
} else {
categoryIndex = categoriesIndex[category]
data.categories[categoryIndex].emojis.push(datum.short_name)
data.emojis[datum.short_name] = datum
}
})
var flags = data.categories[categoriesIndex['Flags']];
flags.emojis.sort()
mkdirp('data', (err) => {
if (err) throw err
fs.writeFile('data/index.js', `export default ${JSON.stringify(data)}`, (err) => {
if (err) throw err
})
})