diff --git a/index.js b/index.js index 79e16b5..0a75d35 100644 --- a/index.js +++ b/index.js @@ -79,11 +79,13 @@ 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..74fefbc 100644 --- a/localize.js +++ b/localize.js @@ -108,6 +108,14 @@ const BIDI_MAP = { Z: "Z" }; +const EVEN_NUMBER_MAP = { + 0: '⁰', + 2: '²', + 4: '⁴', + 6: '⁶', + 8: '⁸' +}; + const strategies = { accented: { prefix: "", @@ -126,30 +134,40 @@ 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) && + EVEN_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 ( - 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;