Skip to content

Commit

Permalink
fix(utils): safer isNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Sep 17, 2019
1 parent 2e036ed commit e6dbb5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/uploadx/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});
6 changes: 3 additions & 3 deletions src/uploadx/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export const pick = <T, K extends keyof T>(obj: T, whitelist: K[]): Pick<T, K> =
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';
}

Expand Down

0 comments on commit e6dbb5a

Please sign in to comment.