Skip to content

Commit

Permalink
perf: use Array.isArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
VIKTORVAV99 committed May 20, 2024
1 parent 20011af commit c4b4d50
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/LanguageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [];

Expand Down
5 changes: 1 addition & 4 deletions src/ResourceStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 */
Expand All @@ -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);
Expand Down

0 comments on commit c4b4d50

Please sign in to comment.