From 1bbd06079e03b4069acc6f206f5500cd76a3256c Mon Sep 17 00:00:00 2001 From: Nathan Downs Date: Wed, 27 Jun 2018 11:28:43 -0700 Subject: [PATCH] Refactor helper functions out into scripts/utils.js --- 2/scripts/generate | 37 ++++++------------------------------- 2/scripts/preview | 16 ++-------------- 2/scripts/utils.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 2/scripts/utils.js diff --git a/2/scripts/generate b/2/scripts/generate index 0e9f8340..5efbc8de 100755 --- a/2/scripts/generate +++ b/2/scripts/generate @@ -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); } diff --git a/2/scripts/preview b/2/scripts/preview index cbe51c02..6eeeb9f5 100755 --- a/2/scripts/preview +++ b/2/scripts/preview @@ -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) { '
  • ' + 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'); diff --git a/2/scripts/utils.js b/2/scripts/utils.js new file mode 100644 index 00000000..2ef2fb02 --- /dev/null +++ b/2/scripts/utils.js @@ -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;