From e6dbb5a8e3879231e6d46d7ea598697eeb0f94c8 Mon Sep 17 00:00:00 2001 From: kukhariev Date: Wed, 18 Sep 2019 00:33:14 +0300 Subject: [PATCH] fix(utils): safer isNumber --- src/uploadx/src/utils.spec.ts | 15 ++++++++++++++- src/uploadx/src/utils.ts | 6 +++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/uploadx/src/utils.spec.ts b/src/uploadx/src/utils.spec.ts index 3f018220..a68573d5 100644 --- a/src/uploadx/src/utils.spec.ts +++ b/src/uploadx/src/utils.spec.ts @@ -1,4 +1,4 @@ -import { b64, resolveUrl } from './utils'; +import { b64, isNumber, resolveUrl } from './utils'; const _URL = window.URL; const base = 'http://www.example.com/upload'; const rel = '/upload?upload_id=12345'; @@ -49,3 +49,16 @@ describe('b64', () => { expect(b64.parse(encoded)).toEqual(data); }); }); + +describe('primitives', () => { + it('isNumber', () => { + expect(isNumber(NaN)).toBe(false); + expect(isNumber(null)).toBe(false); + expect(isNumber(false)).toBe(false); + expect(isNumber(true)).toBe(false); + expect(isNumber('NaN')).toBe(false); + expect(isNumber('')).toBe(false); + expect(isNumber(undefined)).toBe(false); + expect(isNumber(0)).toBe(true); + }); +}); diff --git a/src/uploadx/src/utils.ts b/src/uploadx/src/utils.ts index 1186a96a..2a2ec12e 100644 --- a/src/uploadx/src/utils.ts +++ b/src/uploadx/src/utils.ts @@ -33,11 +33,11 @@ export const pick = (obj: T, whitelist: K[]): Pick = return result; }; -export function isNumber(x: any): x is number { - return typeof x === 'number'; +export function isNumber(x: unknown): x is number { + return x === Number(x); } -export function isString(x: any): x is string { +export function isString(x: unknown): x is string { return typeof x === 'string'; }