From 5c63849f4ea66599f7d3a084be46c45c9f8b298a Mon Sep 17 00:00:00 2001 From: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com> Date: Mon, 20 May 2024 19:13:14 +0200 Subject: [PATCH] perf: use Array.isArray() (#2193) --- src/LanguageUtils.js | 2 +- src/ResourceStore.js | 5 +---- src/Translator.js | 10 +++------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/LanguageUtils.js b/src/LanguageUtils.js index afc71df1..e88136c6 100644 --- a/src/LanguageUtils.js +++ b/src/LanguageUtils.js @@ -118,7 +118,7 @@ class LanguageUtil { if (!fallbacks) return []; if (typeof fallbacks === 'function') fallbacks = fallbacks(code); if (typeof fallbacks === 'string') fallbacks = [fallbacks]; - if (Object.prototype.toString.apply(fallbacks) === '[object Array]') return fallbacks; + if (Array.isArray(fallbacks)) return fallbacks; if (!code) return fallbacks.default || []; diff --git a/src/ResourceStore.js b/src/ResourceStore.js index f6b0612a..c5ee6756 100644 --- a/src/ResourceStore.js +++ b/src/ResourceStore.js @@ -87,10 +87,7 @@ class ResourceStore extends EventEmitter { addResources(lng, ns, resources, options = { silent: false }) { /* eslint no-restricted-syntax: 0 */ for (const m in resources) { - if ( - typeof resources[m] === 'string' || - Object.prototype.toString.apply(resources[m]) === '[object Array]' - ) + if (typeof resources[m] === 'string' || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], { silent: true }); } if (!options.silent) this.emit('added', lng, ns, resources); diff --git a/src/Translator.js b/src/Translator.js index 5c6e6cb9..7c3876cf 100644 --- a/src/Translator.js +++ b/src/Translator.js @@ -160,7 +160,7 @@ class Translator extends EventEmitter { res && handleAsObject && noObject.indexOf(resType) < 0 && - !(typeof joinArrays === 'string' && resType === '[object Array]') + !(typeof joinArrays === 'string' && Array.isArray(res)) ) { if (!options.returnObjects && !this.options.returnObjects) { if (!this.options.returnedObjectHandler) { @@ -180,7 +180,7 @@ class Translator extends EventEmitter { // if we got a separator we loop over children - else we just return object as is // as having it set to false means no hierarchy so no lookup for nested values if (keySeparator) { - const resTypeIsArray = resType === '[object Array]'; + const resTypeIsArray = Array.isArray(res); const copy = resTypeIsArray ? [] : {}; // apply child translation on a copy /* eslint no-restricted-syntax: 0 */ @@ -197,11 +197,7 @@ class Translator extends EventEmitter { } res = copy; } - } else if ( - handleAsObjectInI18nFormat && - typeof joinArrays === 'string' && - resType === '[object Array]' - ) { + } else if (handleAsObjectInI18nFormat && typeof joinArrays === 'string' && Array.isArray(res)) { // array special treatment res = res.join(joinArrays); if (res) res = this.extendTranslation(res, keys, options, lastKey);