Skip to content

Commit

Permalink
fix: no instanceof Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Feb 29, 2024
1 parent 93a9953 commit 14bc93c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ export function calcLocales(

return requestedLocales;
}

export function isPromise(x: unknown): x is Promise<unknown> {
return typeof x === 'object' && x !== null && 'then' in x;
}
10 changes: 5 additions & 5 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { match } from '@formatjs/intl-localematcher';
import { Cache } from './cache';
import { flattenDict } from './flattenDict';
import { arrEquals } from './helpers';
import { arrEquals, isPromise } from './helpers';
import { CreateTranslatorOptions, Dict, FlatDict, MaybePromise } from './types';

export class Store<D extends Dict = any, ProvidedArgs extends string = never> {
Expand All @@ -22,7 +22,7 @@ export class Store<D extends Dict = any, ProvidedArgs extends string = never> {
try {
dict = this.options.dicts(locale);

if (dict instanceof Promise) {
if (isPromise(dict)) {
dict = dict.catch(() => {
console.warn(`Failed to load dictionary for locale "${locale}"`);
return null;
Expand All @@ -39,7 +39,7 @@ export class Store<D extends Dict = any, ProvidedArgs extends string = never> {
if (dict instanceof Function) dict = dict();
}

if (dict instanceof Promise) {
if (isPromise(dict)) {
entry = dict.then((resolvedDict) => {
const flatDict = resolvedDict && flattenDict(resolvedDict);
if (this.dicts.get(locale) === entry) {
Expand All @@ -53,7 +53,7 @@ export class Store<D extends Dict = any, ProvidedArgs extends string = never> {

this.dicts.set(locale, entry);

if (entry instanceof Promise) {
if (isPromise(entry)) {
entry.then(() => this.notify());
} else {
this.notify();
Expand All @@ -65,7 +65,7 @@ export class Store<D extends Dict = any, ProvidedArgs extends string = never> {
loadAll(...locales: string[]): MaybePromise<FlatDict[]> {
const dicts = locales.map((locale) => this.load(locale));

if (dicts.some((dict) => dict instanceof Promise)) {
if (dicts.some(isPromise)) {
return Promise.all(dicts).then((dicts) => dicts.filter(Boolean) as FlatDict[]);
}

Expand Down
9 changes: 5 additions & 4 deletions src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Cache } from './cache';
import { customDateTimeFormat, customDateTimeFormatRange } from './intlHelpers';
import { mapPotentialArray } from './mapPotentialArray';
import { FlatDict, ICUArgument, ICUDateArgument } from './types';
import { isPromise } from './helpers';

export function translate<F = never>({
dicts,
Expand Down Expand Up @@ -37,11 +38,11 @@ export function translate<F = never>({
dicts = dicts.slice(0, 1);
}

const dict = dicts.find((dict) => dict instanceof Promise || id in dict);
const dict = dicts.find((dict) => isPromise(dict) || id in dict);

if (dict instanceof Promise) {
if (isPromise(dict)) {
return mapPotentialArray(
sourceDict && !(sourceDict instanceof Promise)
sourceDict && !isPromise(sourceDict)
? translate<string>({
dicts: [sourceDict],
sourceDict,
Expand All @@ -66,7 +67,7 @@ export function translate<F = never>({
if (!template) {
if (fallback instanceof Function) {
const sourceTranslation =
sourceDict && !(sourceDict instanceof Promise)
sourceDict && !isPromise(sourceDict)
? translate<string>({
dicts: [sourceDict],
sourceDict,
Expand Down

0 comments on commit 14bc93c

Please sign in to comment.