-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex.js
67 lines (57 loc) · 2.09 KB
/
regex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Created by chenzhuokai on 16/6/3.
*/
const fs = require('fs-extra')
const _ = require('lodash')
let emojiList = fs.readJsonSync(__dirname + '/emoji.json')
const fromCharCode = String.fromCharCode
const unicodeToJsEscape = require('unicode-escape')
let unicodeList = emojiList.map(function (emoji) {
emoji.converted = emoji.unicode.replace(/U\+/g, '').replace(/\s/g, '-')
return emoji
}).filter(emoji => /U\+/.test(emoji.unicode))
unicodeList = _.sortBy(unicodeList, function (unicode) {
return -(unicodeToJsEscape(unicode.converted).length)
})
unicodeList = unicodeList.map(function (unicode) {
unicode.converted = unicodeToJsEscape(unicode.converted.split('-').map(fromCodePoint).join(''))
return unicode
})
console.log('Exporting json')
fs.outputJsonSync(__dirname + '/unicode.json', unicodeList)
console.log('Exporting regex-es5.txt')
let regexEs5Txt = unicodeList.map((u) => {
return u.converted.replace('\\ufe0f\\u20e3', '\\ufe0f?\\u20e3')
}).join('|')
fs.outputFileSync(__dirname + '/regex-es5.txt', regexEs5Txt)
console.log('Exporting regex-es6.txt')
fs.outputFileSync(__dirname + '/regex-es6.txt', unicodeList.map((u) => {
return u.unicode.replace(/\s/g, '').replace(/U\+([0-9A-F]+)/ig, '\\u{$1}').toLocaleLowerCase()
}).join('|'))
console.log('Exporting regex-python.txt')
fs.outputFileSync(__dirname + '/regex-python.txt', unicodeList.map((u) => {
let unicode = u.unicode.replace(/\s/g, '')
return unicode.split('U+').map(char => {
if (!char) {
return ''
}
return '\\U' + _.padStart(char, 8, '0')
}).join('')
}).join('|'))
console.log('Exporting emoji-parser.js')
let jsFile = fs.readFileSync('./emoji-parser.js.template').toString()
jsFile = jsFile.replace('<% regex-es5 %>', regexEs5Txt)
jsFile = `/* Generated at ${new Date()} */\n` + jsFile
fs.outputFileSync('emoji-parser.js', jsFile)
function fromCodePoint(codepoint) {
let code = typeof codepoint === 'string' ?
parseInt(codepoint, 16) : codepoint
if (code < 0x10000) {
return fromCharCode(code)
}
code -= 0x10000
return fromCharCode(
0xD800 + (code >> 10),
0xDC00 + (code & 0x3FF)
)
}