-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor helper functions out into scripts/utils.js
- Loading branch information
Nathan Downs
committed
Jun 27, 2018
1 parent
1827097
commit 1bbd060
Showing
3 changed files
with
36 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |