Skip to content

Commit

Permalink
change the 'exists' method to check optional instead of 'resolve'
Browse files Browse the repository at this point in the history
  • Loading branch information
raccoonback committed Aug 4, 2021
1 parent 194b2ab commit f57c097
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import baseLogger from './logger.js';
import EventEmitter from './EventEmitter.js';
import postProcessor from './postProcessor.js';
import * as utils from './utils.js';
import { isOptional } from './utils.js';

const checkedLoadedFor = {};

Expand Down Expand Up @@ -39,6 +40,10 @@ class Translator extends EventEmitter {
}

exists(key, options = { interpolation: {} }) {
if (isOptional(key)) {
return false;
}

const resolved = this.resolve(key, options);
return resolved && resolved.res !== undefined;
}
Expand Down Expand Up @@ -84,7 +89,7 @@ class Translator extends EventEmitter {
if (!options) options = {};

// non valid keys handling
if (keys === undefined || keys === null /* || keys === ''*/) return '';
if (isOptional(keys) /* || keys === ''*/) return '';
if (!Array.isArray(keys)) keys = [String(keys)];

// separators
Expand Down Expand Up @@ -364,8 +369,7 @@ class Translator extends EventEmitter {
let usedLng;
let usedNS;

if (!keys) keys = [];
else if (typeof keys === 'string') keys = [keys];
if (typeof keys === 'string') keys = [keys];

// forEach possible key
keys.forEach(k => {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ export const isIE10 =
window.navigator &&
window.navigator.userAgent &&
window.navigator.userAgent.indexOf('MSIE') > -1;

export function isOptional(str) {
return str === undefined || str === null;
}

0 comments on commit f57c097

Please sign in to comment.