Created
May 13, 2021 20:43
-
-
Save gtcdevop/a4a6edb8e51e6732f2d1ff8c98dbbdd4 to your computer and use it in GitHub Desktop.
Solucao
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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