Fix toLocalString language key - Fixes #13255 (#13295)

* Finish localizeNumber ctch null
- Incomplete null catch: accidentally called string replace on an array
- Call lang resolution on array just incase service is passing valid lang array
(linting hasn't been checked)

* Got my local linter working
This commit is contained in:
Helcostr 2021-05-28 14:37:02 -07:00 committed by GitHub
parent 5b4cf5d158
commit 967cd1f47f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,21 @@
// Lang resolution (passed into replace)
const langRes = [
// Strip beyond @ symbol to allow custom languages
[/@(?:.+)$/, ''],
// We use underscore, this mthd uses dash
['_', '-'],
];
// Lang resolution reducer
const langReduce = (str, params) => str.replace(...params);
export default function localizeNumber (valIn, lang, optIn = {}) {
// Extra catch just incase non number
const val = (typeof valIn === 'number') ? valIn : parseFloat(valIn);
// Catch lang null
const langCatch = (lang || []);
// Options Management
const { toFixed } = optIn;
const optOut = {};
@ -11,12 +25,11 @@ export default function localizeNumber (valIn, lang, optIn = {}) {
}
return val.toLocaleString(
// Catch null just incase
(lang || [])
// Strip beyond @ symbol to allow custom languages
.replace(/@(?:.+)$/, '')
// We use underscore, this mthd uses dash
.replace('_', '-'),
langCatch instanceof Array
// Must use map with array
? langCatch.map(each => langRes.reduce(langReduce, each))
// Parse to string as final backup
: langRes.reduce(langReduce, langCatch.toString()),
optOut,
);
}