Skip to content

Commit 845728f

Browse files
Connormihascotthovestadt
authored andcommittedMay 4, 2019
[jest-get-type] Simplify checking for primitive (#8416)
* [jest-get-type] Simplify checking for primitive * Update changelog
1 parent 13bf7c7 commit 845728f

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- `[jest-runtime]` Fix module registry memory leak ([#8282](https://github.com/facebook/jest/pull/8282))
4242
- `[jest-resolve]` optimize resolve module path ([#8388](https://github.com/facebook/jest/pull/8388))
4343
- `[jest-resolve]` cache current directory ([#8412](https://github.com/facebook/jest/pull/8412))
44+
- `[jest-get-type]` Simplify checking for primitive ([#8416](https://github.com/facebook/jest/pull/8416))
4445

4546
## 24.7.1
4647

‎packages/jest-get-type/src/__tests__/isPrimitive.test.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@
99
import {isPrimitive} from '..';
1010

1111
describe('.isPrimitive()', () => {
12-
test.each([null, undefined, 100, 'hello world', true, Symbol.for('a')])(
13-
'returns true when given primitive value of: %s',
14-
primitive => {
15-
expect(isPrimitive(primitive)).toBe(true);
16-
},
17-
);
12+
test.each([
13+
null,
14+
undefined,
15+
100,
16+
'hello world',
17+
true,
18+
Symbol.for('a'),
19+
0,
20+
NaN,
21+
Infinity,
22+
])('returns true when given primitive value of: %s', primitive => {
23+
expect(isPrimitive(primitive)).toBe(true);
24+
});
1825

19-
test.each([{}, [], () => {}, /abc/, new Map(), new Set(), new Date()])(
20-
'returns false when given non primitive value of: %s',
21-
value => {
22-
expect(isPrimitive(value)).toBe(false);
23-
},
24-
);
26+
test.each([
27+
{},
28+
[],
29+
() => {},
30+
/abc/,
31+
new Map(),
32+
new Set(),
33+
new Date(),
34+
Object.create(null),
35+
])('returns false when given non primitive value of: %j', value => {
36+
expect(isPrimitive(value)).toBe(false);
37+
});
2538
});

‎packages/jest-get-type/src/index.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ type ValueType =
2020
| 'symbol'
2121
| 'undefined';
2222

23-
const PRIMITIVES = new Set<ValueType>([
24-
'string',
25-
'number',
26-
'boolean',
27-
'null',
28-
'undefined',
29-
'symbol',
30-
]);
31-
3223
// get the type of a value with handling the edge cases like `typeof []`
3324
// and `typeof null`
3425
function getType(value: unknown): ValueType {
@@ -66,6 +57,6 @@ function getType(value: unknown): ValueType {
6657
throw new Error(`value of unknown type: ${value}`);
6758
}
6859

69-
getType.isPrimitive = (value: unknown) => PRIMITIVES.has(getType(value));
60+
getType.isPrimitive = (value: unknown) => Object(value) !== value;
7061

7162
export = getType;

0 commit comments

Comments
 (0)
Please sign in to comment.