mirror of
https://github.com/twitter/twemoji.git
synced 2024-06-15 03:35:16 +00:00
Refactor helper functions out into scripts/utils.js
This commit is contained in:
parent
1827097fb7
commit
1bbd06079e
@ -8,6 +8,7 @@
|
||||
var fs = require('fs');
|
||||
var http = require('http');
|
||||
var path = require('path');
|
||||
var Utils = require('./utils');
|
||||
|
||||
function file(which) {
|
||||
return path.join(__dirname, '../..', which);
|
||||
@ -31,32 +32,6 @@ var skinToneOptions = [
|
||||
// there is no asset equivalent for these
|
||||
var ignoreMissing = ['2002', '2003', '2005'];
|
||||
|
||||
|
||||
// basic utilities to convert codepoints to JSON strings
|
||||
function toJSON(codePoints) {
|
||||
return codePoints.split('-').map(function (point) {
|
||||
return UTF162JSON(fromCodePoint(point));
|
||||
}).join('');
|
||||
}
|
||||
function fromCodePoint(codepoint) {
|
||||
var code = typeof codepoint === 'string' ?
|
||||
parseInt(codepoint, 16) : codepoint;
|
||||
if (code < 0x10000) {
|
||||
return String.fromCharCode(code);
|
||||
}
|
||||
code -= 0x10000;
|
||||
return String.fromCharCode(
|
||||
0xD800 + (code >> 10),
|
||||
0xDC00 + (code & 0x3FF)
|
||||
);
|
||||
}
|
||||
function UTF162JSON(text) {
|
||||
for (var i = 0, r = []; i < text.length; i++) {
|
||||
r.push('\\u' + ('000' + text.charCodeAt(i).toString(16)).slice(-4));
|
||||
}
|
||||
return r.join('');
|
||||
}
|
||||
|
||||
// Items is an array of unicode sequences with \u escaping, like ["\u2963\ufe0f", "\u263a\ufe0f"]
|
||||
// items get sorted by length (long to short), then unicode hex values (low to high)
|
||||
// output is "or" ed together using | for regex
|
||||
@ -80,7 +55,7 @@ function generateRegexPartial(items) {
|
||||
flushCharClass();
|
||||
}
|
||||
currentPrefix = prefix;
|
||||
var suffixMinusOne = UTF162JSON(String.fromCharCode(parseInt(suffix, 16) - 1));
|
||||
var suffixMinusOne = Utils.UTF162JSON(String.fromCharCode(parseInt(suffix, 16) - 1));
|
||||
|
||||
if (charRange.length && charRange.slice(-1)[0] !== suffixMinusOne) {
|
||||
flushCharRange();
|
||||
@ -342,12 +317,12 @@ Queue([
|
||||
return hex.length < 4 ? ('000' + hex).slice(-4) : hex;
|
||||
});
|
||||
if (q.ignore.indexOf(codePoints) < 0) {
|
||||
u = toJSON(codePoints);
|
||||
u = Utils.toJSON(codePoints);
|
||||
codePointsWithoutKeycap = codePoints.replace(/-20E3$/, '');
|
||||
if (codePoints.indexOf('200D') >= 0) {
|
||||
q.zwj.push(u);
|
||||
} else if (codePoints != codePointsWithoutKeycap && q.variantsSensitive.indexOf(codePointsWithoutKeycap) >= 0) {
|
||||
q.sensitiveKeycaps.push(toJSON(codePointsWithoutKeycap));
|
||||
q.sensitiveKeycaps.push(Utils.toJSON(codePointsWithoutKeycap));
|
||||
} else if (q.diversityBase.indexOf(codePoints.replace(/-1F3F[B-F]$/, '')) >= 0) {
|
||||
// This is a diversity Emoji with or without a skin tone modifier
|
||||
// Add it to the regex if this is the base without the modifier
|
||||
@ -407,10 +382,10 @@ Queue([
|
||||
var matches = mapOfMatches[key];
|
||||
// Only a complete set may be replaced
|
||||
if (matches.length === pattern.numCombinations) {
|
||||
replacements.push(UTF162JSON(key));
|
||||
replacements.push(Utils.UTF162JSON(key));
|
||||
// Remove all items in the match set from the original zwj list
|
||||
matches.forEach(function(rawString) {
|
||||
var indexToRemove = q.zwj.indexOf(UTF162JSON(rawString));
|
||||
var indexToRemove = q.zwj.indexOf(Utils.UTF162JSON(rawString));
|
||||
if (indexToRemove >= 0) {
|
||||
q.zwj.splice(indexToRemove, 1);
|
||||
}
|
||||
|
@ -7,22 +7,10 @@
|
||||
// dependencies
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Utils = require('./utils');
|
||||
|
||||
var regex = new RegExp(fs.readFileSync(file('scripts/regex')).toString(), 'g');
|
||||
|
||||
function fromCodePoint(codepoint) {
|
||||
var code = typeof codepoint === 'string' ?
|
||||
parseInt(codepoint, 16) : codepoint;
|
||||
if (code < 0x10000) {
|
||||
return String.fromCharCode(code);
|
||||
}
|
||||
code -= 0x10000;
|
||||
return String.fromCharCode(
|
||||
0xD800 + (code >> 10),
|
||||
0xDC00 + (code & 0x3FF)
|
||||
);
|
||||
}
|
||||
|
||||
function countEmoji(emoji) {
|
||||
var count = 0;
|
||||
regex.lastIndex = 0;
|
||||
@ -42,7 +30,7 @@ fs.readdir(file('assets'), function (err, files) {
|
||||
'<li>' + files.map(function (filename) {
|
||||
var codepoints = filename.replace('.ai', '').split('-');
|
||||
var emoji = codepoints.map(function(codepoint) {
|
||||
return fromCodePoint(codepoint);
|
||||
return Utils.fromCodePoint(codepoint);
|
||||
}).join('');
|
||||
if (countEmoji(emoji + '\ufe0f') === 1) {
|
||||
codepoints.push('fe0f');
|
||||
|
28
2/scripts/utils.js
Normal file
28
2/scripts/utils.js
Normal file
@ -0,0 +1,28 @@
|
||||
function fromCodePoint(codepoint) {
|
||||
var code = typeof codepoint === 'string' ?
|
||||
parseInt(codepoint, 16) : codepoint;
|
||||
if (code < 0x10000) {
|
||||
return String.fromCharCode(code);
|
||||
}
|
||||
code -= 0x10000;
|
||||
return String.fromCharCode(
|
||||
0xD800 + (code >> 10),
|
||||
0xDC00 + (code & 0x3FF)
|
||||
);
|
||||
}
|
||||
module.exports.fromCodePoint = fromCodePoint;
|
||||
|
||||
function toJSON(codePoints) {
|
||||
return codePoints.split('-').map(function (point) {
|
||||
return UTF162JSON(fromCodePoint(point));
|
||||
}).join('');
|
||||
}
|
||||
module.exports.toJSON = toJSON;
|
||||
|
||||
function UTF162JSON(text) {
|
||||
for (var i = 0, r = []; i < text.length; i++) {
|
||||
r.push('\\u' + ('000' + text.charCodeAt(i).toString(16)).slice(-4));
|
||||
}
|
||||
return r.join('');
|
||||
}
|
||||
module.exports.UTF162JSON = UTF162JSON;
|
Loading…
Reference in New Issue
Block a user