-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move getType from jest-matcher-utils to separate package
- Loading branch information
Showing
17 changed files
with
118 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "jest-get-type", | ||
"description": "A utility function to get the type of a value", | ||
"version": "20.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"main": "build/index.js", | ||
"browser": "build-es5/index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const getType = require('..'); | ||
|
||
describe('.getType()', () => { | ||
test('null', () => expect(getType(null)).toBe('null')); | ||
test('undefined', () => expect(getType(undefined)).toBe('undefined')); | ||
test('object', () => expect(getType({})).toBe('object')); | ||
test('array', () => expect(getType([])).toBe('array')); | ||
test('number', () => expect(getType(1)).toBe('number')); | ||
test('string', () => expect(getType('oi')).toBe('string')); | ||
test('function', () => expect(getType(() => {})).toBe('function')); | ||
test('boolean', () => expect(getType(true)).toBe('boolean')); | ||
test('symbol', () => expect(getType(Symbol.for('a'))).toBe('symbol')); | ||
test('regexp', () => expect(getType(/abc/)).toBe('regexp')); | ||
test('map', () => expect(getType(new Map())).toBe('map')); | ||
test('set', () => expect(getType(new Set())).toBe('set')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
export type ValueType = | ||
| 'array' | ||
| 'boolean' | ||
| 'function' | ||
| 'null' | ||
| 'number' | ||
| 'object' | ||
| 'regexp' | ||
| 'map' | ||
| 'set' | ||
| 'string' | ||
| 'symbol' | ||
| 'undefined'; | ||
|
||
// get the type of a value with handling the edge cases like `typeof []` | ||
// and `typeof null` | ||
const getType = (value: any): ValueType => { | ||
if (typeof 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.constructor === RegExp) { | ||
return 'regexp'; | ||
} else if (value.constructor === Map) { | ||
return 'map'; | ||
} else if (value.constructor === Set) { | ||
return 'set'; | ||
} | ||
return 'object'; | ||
// $FlowFixMe https://github.com/facebook/flow/issues/1015 | ||
} else if (typeof value === 'symbol') { | ||
return 'symbol'; | ||
} | ||
|
||
throw new Error(`value of unknown type: ${value}`); | ||
}; | ||
|
||
module.exports = getType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters