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] Add isPrimitive function #7708

Merged
merged 11 commits into from
Feb 28, 2019
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- `[expect]`: Improve report when matcher fails, part 10 ([#7960](https://github.com/facebook/jest/pull/7960))
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
- `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935))
- `[jest-haste-map]` Add "skipPackageJson" option
- `[jest-haste-map]` Add `skipPackageJson` option ([#7778](https://github.com/facebook/jest/pull/7778))
- `[jest-get-type]` Add `isPrimitive` function ([#7708](https://github.com/facebook/jest/pull/7708))

### Fixes

Expand Down
14 changes: 3 additions & 11 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import util from 'util';
import chalk from 'chalk';
import pretty from 'pretty-format';
import getType from 'jest-get-type';
import {isPrimitive} from 'jest-get-type';
import {ErrorWithStack} from 'jest-util';

type Table = Array<Array<any>>;
Expand All @@ -24,13 +24,6 @@ const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const PRIMITIVES = new Set([
'string',
'number',
'boolean',
'null',
'undefined',
]);

export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
function eachBind(title: string, test: Function, timeout: number): void {
Expand Down Expand Up @@ -203,10 +196,9 @@ const getMatchingKeyPaths = title => (matches, key) =>
const replaceKeyPathWithValue = data => (title, match) => {
const keyPath = match.replace('$', '').split('.');
const value = getPath(data, keyPath);
const valueType = getType(value);

if (PRIMITIVES.has(valueType)) {
return title.replace(match, value);
if (isPrimitive(value)) {
return title.replace(match, String(value));
}
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import getType from '..';
import getType from '../getType';

describe('.getType()', () => {
test('null', () => expect(getType(null)).toBe('null'));
Expand Down
25 changes: 25 additions & 0 deletions packages/jest-get-type/src/__tests__/isPrimitive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import isPrimitive from '../isPrimitive';

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([{}, [], () => {}, /abc/, new Map(), new Set(), new Date()])(
'returns false when given non primitive value of: %s',
value => {
expect(isPrimitive(value)).toBe(false);
},
);
});
60 changes: 60 additions & 0 deletions packages/jest-get-type/src/getType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export type ValueType =
| 'array'
| 'boolean'
| 'function'
| 'null'
| 'number'
| 'object'
| 'regexp'
| 'map'
| 'set'
| 'date'
| 'string'
| 'symbol'
| 'undefined';

// get the type of a value with handling the edge cases like `typeof []`
// and `typeof null`
const getType = (value: unknown): ValueType => {
if (value === undefined) {
return 'undefined';
} else if (value === null) {
return 'null';
} else if (Array.isArray(value)) {
return 'array';
} else if (typeof value === 'boolean') {
return 'boolean';
} else if (typeof value === 'function') {
return 'function';
} else if (typeof value === 'number') {
return 'number';
} else if (typeof value === 'string') {
return 'string';
} else if (typeof value === 'object') {
if (value != null) {
if (value.constructor === RegExp) {
return 'regexp';
} else if (value.constructor === Map) {
return 'map';
} else if (value.constructor === Set) {
return 'set';
} else if (value.constructor === Date) {
return 'date';
}
}
return 'object';
} else if (typeof value === 'symbol') {
return 'symbol';
}

throw new Error(`value of unknown type: ${value}`);
};

export default getType;
55 changes: 6 additions & 49 deletions packages/jest-get-type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

type ValueType =
| 'array'
| 'boolean'
| 'function'
| 'null'
| 'number'
| 'object'
| 'regexp'
| 'map'
| 'set'
| 'date'
| 'string'
| 'symbol'
| 'undefined';
import getTypeFunc from './getType';
import isPrimitive from './isPrimitive';

// get the type of a value with handling the edge cases like `typeof []`
// and `typeof null`
const getType = (value: unknown): ValueType => {
if (value === undefined) {
return 'undefined';
} else if (value === null) {
return 'null';
} else if (Array.isArray(value)) {
return 'array';
} else if (typeof value === 'boolean') {
return 'boolean';
} else if (typeof value === 'function') {
return 'function';
} else if (typeof value === 'number') {
return 'number';
} else if (typeof value === 'string') {
return 'string';
} else if (typeof value === 'object') {
if (value != null) {
if (value.constructor === RegExp) {
return 'regexp';
} else if (value.constructor === Map) {
return 'map';
} else if (value.constructor === Set) {
return 'set';
} else if (value.constructor === Date) {
return 'date';
}
}
return 'object';
} else if (typeof value === 'symbol') {
return 'symbol';
}
function getType(value: unknown) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
return getTypeFunc(value);
}

throw new Error(`value of unknown type: ${value}`);
};
getType.isPrimitive = isPrimitive;

export = getType;
19 changes: 19 additions & 0 deletions packages/jest-get-type/src/isPrimitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import getType, {ValueType} from './getType';

const PRIMITIVES = new Set<ValueType>([
'string',
'number',
'boolean',
'null',
'undefined',
'symbol',
]);

export default (value: unknown): boolean => PRIMITIVES.has(getType(value));