Skip to content

Commit

Permalink
Refactor helper functions out into scripts/utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Downs committed Jun 27, 2018
1 parent 1827097 commit 1bbd060
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
37 changes: 6 additions & 31 deletions 2/scripts/generate
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 2 additions & 14 deletions 2/scripts/preview
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down
28 changes: 28 additions & 0 deletions 2/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1bbd060

Please sign in to comment.