Skip to content

Instantly share code, notes, and snippets.

@1000hz
Last active January 16, 2017 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1000hz/6f43e06fb1c34c5c0ddb3f146f3f5043 to your computer and use it in GitHub Desktop.
Save 1000hz/6f43e06fb1c34c5c0ddb3f146f3f5043 to your computer and use it in GitHub Desktop.
i18n for dogs
{
const random = (min, max) => {
if (min === undefined && max === undefined) return Math.random()
if (max === undefined) {
max = min
min = 0
}
return ~~(Math.random() * max) + min
}
const sample = (arr) => arr[random(arr.length)]
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1)
const isCapitalized = (str) =>
str.charCodeAt(0) >= "A".charCodeAt(0) &&
str.charCodeAt(0) <= "Z".charCodeAt(0)
const replaceTextContent = (html, replaceFn) => {
const htmlOrTextRegex = /(<.*?>)|([\w-]+)/g
return html.replace(
htmlOrTextRegex,
(_, htmlTag, textContent) => htmlTag || replaceFn(textContent)
)
}
document.querySelectorAll('body').forEach(node =>
node.innerHTML = replaceTextContent(node.innerHTML, (match) => {
const BARKS = ["arf", "woof", "bark", "woof-woof", "ruff", "awooo", "bow-wow"]
const bark = sample(BARKS)
return isCapitalized(match)
? capitalize(bark)
: bark
})
)
String.fromCodePoint(128054)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment