Skip to content

Instantly share code, notes, and snippets.

@gtcdevop
Created May 13, 2021 20:43
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 gtcdevop/a4a6edb8e51e6732f2d1ff8c98dbbdd4 to your computer and use it in GitHub Desktop.
Save gtcdevop/a4a6edb8e51e6732f2d1ff8c98dbbdd4 to your computer and use it in GitHub Desktop.
Solucao
const formatters = {
current: {
count: {
value: 'numeric',
percent: 'percent'
},
sum: {
value: 'currency'
}
}
}
const getFormatter = (key, obj=null) => {
let match = key.match(/([aA-zZ]+)\.?(([aA-zZ]{1,40}\.?)*)/);
if(typeof obj == "object") {
return getFormatter(match[2], obj[match[1]]);
} else {
return obj;
}
}
const test1 = getFormatter('current.count.value', formatters) // numeric
const test2 = getFormatter('current.count.percent', formatters) // percent
const test3 = getFormatter('current.sum.value', formatters) // currency
console.log(test1, test2, test3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment