From 69cfc0e417731abd74dd75d8cbec91db6e149497 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Fri, 3 May 2019 20:26:23 +0300 Subject: [PATCH 1/2] [jest-get-type] Simplify checking for primitive --- .../src/__tests__/isPrimitive.test.ts | 37 +++++++++++++------ packages/jest-get-type/src/index.ts | 11 +----- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/jest-get-type/src/__tests__/isPrimitive.test.ts b/packages/jest-get-type/src/__tests__/isPrimitive.test.ts index d3e9a7175af5..1428cbc3e435 100644 --- a/packages/jest-get-type/src/__tests__/isPrimitive.test.ts +++ b/packages/jest-get-type/src/__tests__/isPrimitive.test.ts @@ -9,17 +9,30 @@ import {isPrimitive} from '..'; describe('.isPrimitive()', () => { - test.each([null, undefined, 100, 'hello world', true, Symbol.for('a')])( - 'returns true when given primitive value of: %s', - primitive => { - expect(isPrimitive(primitive)).toBe(true); - }, - ); + test.each([ + null, + undefined, + 100, + 'hello world', + true, + Symbol.for('a'), + 0, + NaN, + Infinity, + ])('returns true when given primitive value of: %s', primitive => { + expect(isPrimitive(primitive)).toBe(true); + }); - test.each([{}, [], () => {}, /abc/, new Map(), new Set(), new Date()])( - 'returns false when given non primitive value of: %s', - value => { - expect(isPrimitive(value)).toBe(false); - }, - ); + test.each([ + {}, + [], + () => {}, + /abc/, + new Map(), + new Set(), + new Date(), + Object.create(null), + ])('returns false when given non primitive value of: %j', value => { + expect(isPrimitive(value)).toBe(false); + }); }); diff --git a/packages/jest-get-type/src/index.ts b/packages/jest-get-type/src/index.ts index cfea04eaf48a..fde928fcc337 100644 --- a/packages/jest-get-type/src/index.ts +++ b/packages/jest-get-type/src/index.ts @@ -20,15 +20,6 @@ type ValueType = | 'symbol' | 'undefined'; -const PRIMITIVES = new Set([ - 'string', - 'number', - 'boolean', - 'null', - 'undefined', - 'symbol', -]); - // get the type of a value with handling the edge cases like `typeof []` // and `typeof null` function getType(value: unknown): ValueType { @@ -66,6 +57,6 @@ function getType(value: unknown): ValueType { throw new Error(`value of unknown type: ${value}`); } -getType.isPrimitive = (value: unknown) => PRIMITIVES.has(getType(value)); +getType.isPrimitive = (value: unknown) => Object(value) !== value; export = getType; From 1ec9cbc590d0ed44b9f39695db03dc9f34fbebec Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Sat, 4 May 2019 12:12:31 +0300 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b949443570c1..f15799a36f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ - `[jest-runtime]` Fix module registry memory leak ([#8282](https://github.com/facebook/jest/pull/8282)) - `[jest-resolve]` optimize resolve module path ([#8388](https://github.com/facebook/jest/pull/8388)) - `[jest-resolve]` cache current directory ([#8412](https://github.com/facebook/jest/pull/8412)) +- `[jest-get-type]` Simplify checking for primitive ([#8416](https://github.com/facebook/jest/pull/8416)) ## 24.7.1