Skip to content

Commit

Permalink
[jest-get-type] Simplify checking for primitive (#8416)
Browse files Browse the repository at this point in the history
* [jest-get-type] Simplify checking for primitive

* Update changelog
  • Loading branch information
Connormiha authored and scotthovestadt committed May 4, 2019
1 parent 13bf7c7 commit 845728f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
37 changes: 25 additions & 12 deletions packages/jest-get-type/src/__tests__/isPrimitive.test.ts
Expand Up @@ -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);
});
});
11 changes: 1 addition & 10 deletions packages/jest-get-type/src/index.ts
Expand Up @@ -20,15 +20,6 @@ type ValueType =
| 'symbol'
| 'undefined';

const PRIMITIVES = new Set<ValueType>([
'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 {
Expand Down Expand Up @@ -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;

0 comments on commit 845728f

Please sign in to comment.