From b037a6e6d9ba88861ab6cedeb1120656bd287a63 Mon Sep 17 00:00:00 2001 From: Jop de Klein Date: Thu, 14 Feb 2019 12:15:08 +0100 Subject: [PATCH 1/4] Add safeguard for empty strings, pass correct options to pseudoLocalize --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 79e16b5..e129865 100644 --- a/index.js +++ b/index.js @@ -84,7 +84,7 @@ const pseudoLocalization = (() => { ) => { opts.blacklistedNodeNames = blacklistedNodeNames; opts.strategy = strategy; - + pseudoLocalize(document.body); observer.observe(document.body, observerConfig); }; From 081b38e37b91b7149e15e2dccc68ff6522e5c956 Mon Sep 17 00:00:00 2001 From: Jop de Klein Date: Thu, 14 Feb 2019 14:17:37 +0100 Subject: [PATCH 2/4] Support for elongation of numbers --- index.js | 6 ++++-- localize.js | 25 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index e129865..0a75d35 100644 --- a/index.js +++ b/index.js @@ -79,12 +79,14 @@ const pseudoLocalization = (() => { const start = ( { strategy = "accented", - blacklistedNodeNames = opts.blacklistedNodeNames + blacklistedNodeNames = opts.blacklistedNodeNames, + elongateNumbers = false } = {} ) => { opts.blacklistedNodeNames = blacklistedNodeNames; opts.strategy = strategy; - + opts.elongateNumbers = elongateNumbers; + pseudoLocalize(document.body); observer.observe(document.body, observerConfig); }; diff --git a/localize.js b/localize.js index e2ff921..98fe1da 100644 --- a/localize.js +++ b/localize.js @@ -126,30 +126,39 @@ const strategies = { }; const psuedoLocalizeString = (string, options = { strategy: "accented" }) => { - let opts = strategies[options.strategy]; + let strategyOptions = strategies[options.strategy]; let pseudoLocalizedText = ""; for (let character of string) { - if (opts.map[character]) { + const convertedCharacter = strategyOptions.map[character] + const characterAsInt = parseInt(character) + + if ( + options.elongateNumbers && + !isNaN(characterAsInt) && + characterAsInt >= 3 + ) { + pseudoLocalizedText += character.repeat(2); + } else if (convertedCharacter) { const cl = character.toLowerCase(); // duplicate "a", "e", "o" and "u" to emulate ~30% longer text if ( - opts.elongate && + strategyOptions.elongate && (cl === "a" || cl === "e" || cl === "o" || cl === "u") ) { - pseudoLocalizedText += opts.map[character] + opts.map[character]; - } else pseudoLocalizedText += opts.map[character]; + pseudoLocalizedText += convertedCharacter.repeat(2); + } else pseudoLocalizedText += strategyOptions.map[character]; } else pseudoLocalizedText += character; } // If this string is from the DOM, it should already contain the pre- and postfix. if ( - pseudoLocalizedText.startsWith(opts.prefix) && - pseudoLocalizedText.endsWith(opts.postfix) + pseudoLocalizedText.startsWith(strategyOptions.prefix) && + pseudoLocalizedText.endsWith(strategyOptions.postfix) ) { return pseudoLocalizedText; } - return opts.prefix + pseudoLocalizedText + opts.postfix; + return strategyOptions.prefix + pseudoLocalizedText + strategyOptions.postfix; }; module.exports = psuedoLocalizeString; From c91ce03d6e4962315bd1489f445aad121e893323 Mon Sep 17 00:00:00 2001 From: Jop de Klein Date: Mon, 18 Feb 2019 12:02:41 +0100 Subject: [PATCH 3/4] Use character map for numbers up to 7 --- localize.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/localize.js b/localize.js index 98fe1da..2a233ab 100644 --- a/localize.js +++ b/localize.js @@ -108,6 +108,16 @@ const BIDI_MAP = { Z: "Z" }; +const NUMBER_MAP = { + 0: '⁰', + 1: 'ⁱ', + 2: '²', + 3: '³', + 4: '⁴', + 5: '⁵', + 6: '⁶' +}; + const strategies = { accented: { prefix: "", @@ -136,9 +146,9 @@ const psuedoLocalizeString = (string, options = { strategy: "accented" }) => { if ( options.elongateNumbers && !isNaN(characterAsInt) && - characterAsInt >= 3 + NUMBER_MAP[character] ) { - pseudoLocalizedText += character.repeat(2); + pseudoLocalizedText += character + NUMBER_MAP[character]; } else if (convertedCharacter) { const cl = character.toLowerCase(); // duplicate "a", "e", "o" and "u" to emulate ~30% longer text From 0676878a2278d1d07cc14f3baec63dd8ead5dcc4 Mon Sep 17 00:00:00 2001 From: Jop de Klein Date: Tue, 19 Feb 2019 15:33:56 +0100 Subject: [PATCH 4/4] Elongate even numbers only --- localize.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/localize.js b/localize.js index 2a233ab..74fefbc 100644 --- a/localize.js +++ b/localize.js @@ -108,14 +108,12 @@ const BIDI_MAP = { Z: "Z" }; -const NUMBER_MAP = { +const EVEN_NUMBER_MAP = { 0: '⁰', - 1: 'ⁱ', 2: '²', - 3: '³', 4: '⁴', - 5: '⁵', - 6: '⁶' + 6: '⁶', + 8: '⁸' }; const strategies = { @@ -146,12 +144,13 @@ const psuedoLocalizeString = (string, options = { strategy: "accented" }) => { if ( options.elongateNumbers && !isNaN(characterAsInt) && - NUMBER_MAP[character] + EVEN_NUMBER_MAP[character] ) { - pseudoLocalizedText += character + NUMBER_MAP[character]; + // Duplicate even numbers with superscript variations + pseudoLocalizedText += character + EVEN_NUMBER_MAP[character]; } else if (convertedCharacter) { const cl = character.toLowerCase(); - // duplicate "a", "e", "o" and "u" to emulate ~30% longer text + // Duplicate "a", "e", "o" and "u" to emulate ~30% longer text if ( strategyOptions.elongate && (cl === "a" || cl === "e" || cl === "o" || cl === "u")