Skip to content

Commit

Permalink
Simpify getting object type name (#7159)
Browse files Browse the repository at this point in the history
* Simpify getting object type name

* Add info to changelog
  • Loading branch information
Connormiha authored and thymikee committed Oct 15, 2018
1 parent 3abfa18 commit acb26a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
- `[docs]` Removed useless expect.assertions in `TestingAsyncCode.md` ([#7131](https://github.com/facebook/jest/pull/7131))
- `[docs]` Remove references to `@providesModule` which isn't supported anymore ([#7147](https://github.com/facebook/jest/pull/7147))

### Performance

- `[jest-mock]` Improve `getType` function performance. ([#7159](https://github.com/facebook/jest/pull/7159))

## 23.6.0

### Features
Expand Down
69 changes: 42 additions & 27 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,36 @@ function matchArity(fn: any, length: number): any {
return mockConstructor;
}

function isA(typeName: string, value: any): boolean {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
function getObjectType(value: any): string {
return Object.prototype.toString.apply(value).slice(8, -1);
}

function getType(ref?: any): string | null {
const typeName = getObjectType(ref);
if (
isA('Function', ref) ||
isA('AsyncFunction', ref) ||
isA('GeneratorFunction', ref)
typeName === 'Function' ||
typeName === 'AsyncFunction' ||
typeName === 'GeneratorFunction'
) {
return 'function';
} else if (Array.isArray(ref)) {
return 'array';
} else if (isA('Object', ref)) {
} else if (typeName === 'Object') {
return 'object';
} else if (
isA('Number', ref) ||
isA('String', ref) ||
isA('Boolean', ref) ||
isA('Symbol', ref)
typeName === 'Number' ||
typeName === 'String' ||
typeName === 'Boolean' ||
typeName === 'Symbol'
) {
return 'constant';
} else if (isA('Map', ref) || isA('WeakMap', ref) || isA('Set', ref)) {
} else if (
typeName === 'Map' ||
typeName === 'WeakMap' ||
typeName === 'Set'
) {
return 'collection';
} else if (isA('RegExp', ref)) {
} else if (typeName === 'RegExp') {
return 'regexp';
} else if (ref === undefined) {
return 'undefined';
Expand All @@ -209,21 +214,31 @@ function getType(ref?: any): string | null {
}

function isReadonlyProp(object: any, prop: string): boolean {
return (
((prop === 'arguments' ||
prop === 'caller' ||
prop === 'callee' ||
prop === 'name' ||
prop === 'length') &&
(isA('Function', object) ||
isA('AsyncFunction', object) ||
isA('GeneratorFunction', object))) ||
((prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
prop === 'multiline') &&
isA('RegExp', object))
);
if (
prop === 'arguments' ||
prop === 'caller' ||
prop === 'callee' ||
prop === 'name' ||
prop === 'length'
) {
const typeName = getObjectType(object);
return (
typeName === 'Function' ||
typeName === 'AsyncFunction' ||
typeName === 'GeneratorFunction'
);
}

if (
prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
prop === 'multiline'
) {
return getObjectType(object) === 'RegExp';
}

return false;
}

class ModuleMockerClass {
Expand Down

0 comments on commit acb26a9

Please sign in to comment.