From 967cd1f47f5c4f70255d865eea9f283d48a5e253 Mon Sep 17 00:00:00 2001 From: Helcostr Date: Fri, 28 May 2021 14:37:02 -0700 Subject: [PATCH] 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 --- website/client/src/filters/localizeNumber.js | 25 +++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/website/client/src/filters/localizeNumber.js b/website/client/src/filters/localizeNumber.js index c4e6fd03e9..4e7e63d7df 100644 --- a/website/client/src/filters/localizeNumber.js +++ b/website/client/src/filters/localizeNumber.js @@ -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, ); }