Replace charCodeAt and fromCharCode with codePointAt and fromCodePoint

This commit is contained in:
toasted-nutbread 2020-03-09 21:34:31 -04:00
parent 0cd1f8f20a
commit 72219ba353
4 changed files with 34 additions and 32 deletions

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/*global jpIsCharCodeKanji, jpDistributeFurigana, Handlebars*/ /*global jpIsCodePointKanji, jpDistributeFurigana, Handlebars*/
function handlebarsEscape(text) { function handlebarsEscape(text) {
return Handlebars.Utils.escapeExpression(text); return Handlebars.Utils.escapeExpression(text);
@ -62,7 +62,7 @@ function handlebarsFuriganaPlain(options) {
function handlebarsKanjiLinks(options) { function handlebarsKanjiLinks(options) {
let result = ''; let result = '';
for (const c of options.fn(this)) { for (const c of options.fn(this)) {
if (jpIsCharCodeKanji(c.charCodeAt(0))) { if (jpIsCodePointKanji(c.codePointAt(0))) {
result += `<a href="#" class="kanji-link">${c}</a>`; result += `<a href="#" class="kanji-link">${c}</a>`;
} else { } else {
result += c; result += c;

View File

@ -115,9 +115,9 @@ const JP_JAPANESE_RANGES = [
// Helper functions // Helper functions
function _jpIsCharCodeInRanges(charCode, ranges) { function _jpIsCodePointInRanges(codePoint, ranges) {
for (const [min, max] of ranges) { for (const [min, max] of ranges) {
if (charCode >= min && charCode <= max) { if (codePoint >= min && codePoint <= max) {
return true; return true;
} }
} }
@ -127,16 +127,16 @@ function _jpIsCharCodeInRanges(charCode, ranges) {
// Character code testing functions // Character code testing functions
function jpIsCharCodeKanji(charCode) { function jpIsCodePointKanji(codePoint) {
return _jpIsCharCodeInRanges(charCode, JP_CJK_RANGES); return _jpIsCodePointInRanges(codePoint, JP_CJK_RANGES);
} }
function jpIsCharCodeKana(charCode) { function jpIsCodePointKana(codePoint) {
return _jpIsCharCodeInRanges(charCode, JP_KANA_RANGES); return _jpIsCodePointInRanges(codePoint, JP_KANA_RANGES);
} }
function jpIsCharCodeJapanese(charCode) { function jpIsCodePointJapanese(codePoint) {
return _jpIsCharCodeInRanges(charCode, JP_JAPANESE_RANGES); return _jpIsCodePointInRanges(codePoint, JP_JAPANESE_RANGES);
} }
@ -144,8 +144,8 @@ function jpIsCharCodeJapanese(charCode) {
function jpIsStringEntirelyKana(str) { function jpIsStringEntirelyKana(str) {
if (str.length === 0) { return false; } if (str.length === 0) { return false; }
for (let i = 0, ii = str.length; i < ii; ++i) { for (const c of str) {
if (!jpIsCharCodeKana(str.charCodeAt(i))) { if (!jpIsCodePointKana(c.codePointAt(0))) {
return false; return false;
} }
} }
@ -154,8 +154,8 @@ function jpIsStringEntirelyKana(str) {
function jpIsStringPartiallyJapanese(str) { function jpIsStringPartiallyJapanese(str) {
if (str.length === 0) { return false; } if (str.length === 0) { return false; }
for (let i = 0, ii = str.length; i < ii; ++i) { for (const c of str) {
if (jpIsCharCodeJapanese(str.charCodeAt(i))) { if (jpIsCodePointJapanese(c.codePointAt(0))) {
return true; return true;
} }
} }
@ -264,8 +264,8 @@ function jpDistributeFurigana(expression, reading) {
const groups = []; const groups = [];
let modePrev = null; let modePrev = null;
for (const c of expression) { for (const c of expression) {
const charCode = c.charCodeAt(0); const codePoint = c.codePointAt(0);
const modeCurr = jpIsCharCodeKanji(charCode) || charCode === JP_ITERATION_MARK_CHAR_CODE ? 'kanji' : 'kana'; const modeCurr = jpIsCodePointKanji(codePoint) || codePoint === JP_ITERATION_MARK_CHAR_CODE ? 'kanji' : 'kana';
if (modeCurr === modePrev) { if (modeCurr === modePrev) {
groups[groups.length - 1].text += c; groups[groups.length - 1].text += c;
} else { } else {
@ -311,10 +311,11 @@ function jpDistributeFuriganaInflected(expression, reading, source) {
function jpConvertHalfWidthKanaToFullWidth(text, sourceMapping) { function jpConvertHalfWidthKanaToFullWidth(text, sourceMapping) {
let result = ''; let result = '';
const ii = text.length;
const hasSourceMapping = Array.isArray(sourceMapping); const hasSourceMapping = Array.isArray(sourceMapping);
for (let i = 0; i < ii; ++i) { // This function is safe to use charCodeAt instead of codePointAt, since all
// the relevant characters are represented with a single UTF-16 character code.
for (let i = 0, ii = text.length; i < ii; ++i) {
const c = text[i]; const c = text[i];
const mapping = JP_HALFWIDTH_KATAKANA_MAPPING.get(c); const mapping = JP_HALFWIDTH_KATAKANA_MAPPING.get(c);
if (typeof mapping !== 'string') { if (typeof mapping !== 'string') {
@ -355,13 +356,13 @@ function jpConvertHalfWidthKanaToFullWidth(text, sourceMapping) {
function jpConvertNumericTofullWidth(text) { function jpConvertNumericTofullWidth(text) {
let result = ''; let result = '';
for (let i = 0, ii = text.length; i < ii; ++i) { for (const char of text) {
let c = text.charCodeAt(i); let c = char.codePointAt(0);
if (c >= 0x30 && c <= 0x39) { // ['0', '9'] if (c >= 0x30 && c <= 0x39) { // ['0', '9']
c += 0xff10 - 0x30; // 0xff10 = '0' full width c += 0xff10 - 0x30; // 0xff10 = '0' full width
result += String.fromCharCode(c); result += String.fromCodePoint(c);
} else { } else {
result += text[i]; result += char;
} }
} }
return result; return result;
@ -377,9 +378,9 @@ function jpConvertAlphabeticToKana(text, sourceMapping) {
sourceMapping.fill(1); sourceMapping.fill(1);
} }
for (let i = 0; i < ii; ++i) { for (const char of text) {
// Note: 0x61 is the character code for 'a' // Note: 0x61 is the character code for 'a'
let c = text.charCodeAt(i); let c = char.codePointAt(0);
if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z'] if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z']
c += (0x61 - 0x41); c += (0x61 - 0x41);
} else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z'] } else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z']
@ -395,10 +396,10 @@ function jpConvertAlphabeticToKana(text, sourceMapping) {
result += jpToHiragana(part, sourceMapping, result.length); result += jpToHiragana(part, sourceMapping, result.length);
part = ''; part = '';
} }
result += text[i]; result += char;
continue; continue;
} }
part += String.fromCharCode(c); part += String.fromCodePoint(c);
} }
if (part.length > 0) { if (part.length > 0) {

View File

@ -20,7 +20,7 @@
dictTermsMergeBySequence, dictTagBuildSource, dictTermsMergeByGloss, dictTermsSort, dictTagsSort dictTermsMergeBySequence, dictTagBuildSource, dictTermsMergeByGloss, dictTermsSort, dictTagsSort
dictEnabledSet, dictTermsGroup, dictTermsCompressTags, dictTermsUndupe, dictTagSanitize dictEnabledSet, dictTermsGroup, dictTermsCompressTags, dictTermsUndupe, dictTagSanitize
jpDistributeFurigana, jpConvertHalfWidthKanaToFullWidth, jpConvertNumericTofullWidth jpDistributeFurigana, jpConvertHalfWidthKanaToFullWidth, jpConvertNumericTofullWidth
jpConvertAlphabeticToKana, jpHiraganaToKatakana, jpKatakanaToHiragana, jpIsCharCodeJapanese jpConvertAlphabeticToKana, jpHiraganaToKatakana, jpKatakanaToHiragana, jpIsCodePointJapanese
Database, Deinflector*/ Database, Deinflector*/
class Translator { class Translator {
@ -621,13 +621,14 @@ class Translator {
static getSearchableText(text, options) { static getSearchableText(text, options) {
if (!options.scanning.alphanumeric) { if (!options.scanning.alphanumeric) {
const ii = text.length; let newText = '';
for (let i = 0; i < ii; ++i) { for (const c of text) {
if (!jpIsCharCodeJapanese(text.charCodeAt(i))) { if (!jpIsCodePointJapanese(c.codePointAt(0))) {
text = text.substring(0, i);
break; break;
} }
newText += c;
} }
text = newText;
} }
return text; return text;

View File

@ -298,7 +298,7 @@ class DisplayGenerator {
} }
static _isCharacterKanji(c) { static _isCharacterKanji(c) {
const code = c.charCodeAt(0); const code = c.codePointAt(0);
return ( return (
code >= 0x4e00 && code < 0x9fb0 || code >= 0x4e00 && code < 0x9fb0 ||
code >= 0x3400 && code < 0x4dc0 code >= 0x3400 && code < 0x4dc0