Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 27 additions & 9 deletions localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ const BIDI_MAP = {
Z: "Z"
};

const EVEN_NUMBER_MAP = {
0: '⁰',
2: '²',
4: '⁴',
6: '⁶',
8: '⁸'
};

const strategies = {
accented: {
prefix: "",
Expand All @@ -126,30 +134,40 @@ const strategies = {
};

const psuedoLocalizeString = (string, options = { strategy: "accented" }) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be conflicts on this line after rebase, I think the best fix is

{ strategy = 'accented', elongateNumbers = false } = {}

to keep deep defaults and avoid can not find elongateNumbers of undefined Errors in edge cases!

let opts = strategies[options.strategy];
let strategyOptions = strategies[options.strategy];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice rename :)


let pseudoLocalizedText = "";
for (let character of string) {
if (opts.map[character]) {
const convertedCharacter = strategyOptions.map[character]
const characterAsInt = parseInt(character)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move this inline to line 146.

!isNaN(parseInt(character))

imo it's still totally legible and we avoid executing parseInt for every character if elongateNumbers number is false.

Now that I think about it do we need to parseInt?

would

      options.elongateNumbers &&
      EVEN_NUMBER_MAP[character]

not be enough?


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;