Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-get-type] Simplify checking for primitive #8416

Merged
merged 2 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;