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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;