Advertisement
BiRabittoh

emoji list parser

Sep 21st, 2022 (edited)
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs')
  2. const path = require('path')
  3. const https = require('https')
  4.  
  5. const EMOJI_VERSION = '14.0'
  6.  
  7. main()
  8.  
  9. async function main () {
  10.   const text = await getTestFile(EMOJI_VERSION)
  11.  
  12.   console.log(`Format text to json...`)
  13.   const collected = text.trim().split('\n').reduce((accu, line) => {
  14.     if (line.startsWith('# group: ')) {
  15.       console.log(`  Processing ${line.substr(2)}...`)
  16.       accu.group = line.substr(9)
  17.     } else if (line.startsWith('# subgroup: ')) {
  18.       accu.subgroup = line.substr(12)
  19.     } else if (line.startsWith('#')) {
  20.       accu.comments = accu.comments + line + '\n'
  21.     } else {
  22.       const meta = parseLine(line)
  23.       if (meta) {
  24.         //meta.category = `${accu.group} (${accu.subgroup})`
  25.         //meta.group = accu.group
  26.         //meta.subgroup = accu.subgroup
  27.         accu.full.push(meta)
  28.         accu.compact.push(meta.char)
  29.       } else {
  30.         accu.comments = accu.comments.trim() + '\n\n'
  31.       }
  32.     }
  33.     return accu
  34.   }, { comments: '', full: [], compact: [] })
  35.  
  36.   console.log(`Processed emojis: ${collected.full.length}`)
  37.  
  38.   console.log('Write file: emoji.json, emoji-compact.json \n')
  39.   await writeFiles(collected)
  40.  
  41.   console.log(collected.comments)
  42. }
  43.  
  44. async function getTestFile (ver="latest") {
  45.   const url = `https://unicode.org/Public/emoji/${ver}/emoji-test.txt`
  46.  
  47.   process.stdout.write(`Fetch emoji-test.txt (v${EMOJI_VERSION})`)
  48.   return new Promise((resolve, reject) => {
  49.     https.get(url, res => {
  50.       let text = ''
  51.       res.setEncoding('utf8')
  52.       res.on('data', (chunk) => {
  53.         process.stdout.write('.')
  54.         text += chunk
  55.       })
  56.       res.on('end', () => {
  57.         process.stdout.write('\n')
  58.         resolve(text)
  59.       })
  60.       res.on('error', reject)
  61.     })
  62.   })
  63. }
  64.  
  65. function parseLine (line) {
  66.   const data = line.trim().split(/\s+[;#] /)
  67.  
  68.   if (data.length !== 3) {
  69.     return null
  70.   }
  71.  
  72.   const [ codes, status, charAndName ] = data
  73.   const [ , char, name ] = charAndName.match(/^(\S+) E\d+\.\d+ (.+)$/)
  74.  
  75.   return { /*codes, */char, name }
  76. }
  77.  
  78. const rel = (...args) => path.resolve(__dirname, ...args)
  79.  
  80. function writeFiles({ full, compact }) {
  81.   fs.writeFileSync(rel('emoji.json'), JSON.stringify(full), 'utf8')
  82.   fs.writeFileSync(rel('emoji-compact.json'), JSON.stringify(compact), 'utf8')
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement