-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split out copiers from utils, and use more defined copiers for JIT op…
…timizations
- Loading branch information
1 parent
b82035e
commit c303049
Showing
5 changed files
with
382 additions
and
334 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
type PlainObject = { | ||
[key: string]: any; | ||
[index: number]: any; | ||
}; | ||
|
||
import { createCache } from '../src/utils'; | ||
|
||
let copiers: typeof import('../src/copiers'); | ||
|
||
beforeEach(() => { | ||
jest.isolateModules(() => { | ||
copiers = require('../src/copiers'); | ||
}); | ||
}); | ||
|
||
describe('copyObjectLoose', () => { | ||
it('will create an object clone when property symbols are not supported', () => { | ||
const original = Object.getOwnPropertySymbols; | ||
|
||
jest.isolateModules(() => { | ||
Object.getOwnPropertySymbols = undefined; | ||
copiers = require('../src/copiers'); | ||
}); | ||
|
||
const symbol = Symbol('quz'); | ||
const object = { | ||
bar: { baz: 'quz' }, | ||
foo: 'bar', | ||
[symbol]: 'blah', | ||
}; | ||
const handleCopy = jest.fn().mockImplementation((arg) => arg); | ||
const cache = createCache(); | ||
|
||
const result = copiers.copyObjectLoose( | ||
object, | ||
Object.getPrototypeOf(object), | ||
handleCopy, | ||
cache | ||
); | ||
|
||
Object.getOwnPropertySymbols = original; | ||
|
||
expect(result).not.toBe(object); | ||
expect(result).toEqual( | ||
Object.keys(object).reduce((clone: PlainObject, key): PlainObject => { | ||
clone[key] = object[key as keyof typeof object]; | ||
|
||
return clone; | ||
}, {}) | ||
); | ||
|
||
expect(handleCopy).toHaveBeenCalledTimes(Object.keys(object).length); | ||
}); | ||
|
||
it('will create an object clone when property symbols are supported', () => { | ||
const object = { | ||
bar: { baz: 'quz' }, | ||
[Symbol('quz')]: 'blah', | ||
}; | ||
const handleCopy = jest.fn().mockImplementation((arg) => arg); | ||
const cache = createCache(); | ||
|
||
const result = copiers.copyObjectLoose( | ||
object, | ||
Object.getPrototypeOf(object), | ||
handleCopy, | ||
cache | ||
); | ||
|
||
expect(result).not.toBe(object); | ||
expect(result).toEqual(object); | ||
|
||
expect(handleCopy).toHaveBeenCalledTimes( | ||
Object.keys(object).length + Object.getOwnPropertySymbols(object).length | ||
); | ||
}); | ||
}); | ||
|
||
describe('copyObjectStrict', () => { | ||
it('will create an object clone when property symbols are not supported', () => { | ||
const original = Object.getOwnPropertySymbols; | ||
|
||
jest.isolateModules(() => { | ||
Object.getOwnPropertySymbols = undefined; | ||
copiers = require('../src/copiers'); | ||
}); | ||
|
||
const object: PlainObject = { | ||
bar: { baz: 'quz' }, | ||
}; | ||
|
||
Object.defineProperty(object, 'foo', { | ||
value: 'bar', | ||
}); | ||
|
||
Object.defineProperty(object, Symbol('quz'), { | ||
enumerable: true, | ||
value: 'blah', | ||
}); | ||
|
||
const handleCopy = jest.fn().mockImplementation((arg) => arg); | ||
const cache = createCache(); | ||
|
||
const result = copiers.copyObjectStrict( | ||
object, | ||
Object.getPrototypeOf(object), | ||
handleCopy, | ||
cache | ||
); | ||
|
||
Object.getOwnPropertySymbols = original; | ||
|
||
expect(result).not.toBe(object); | ||
expect(result).toEqual( | ||
Object.keys(object).reduce( | ||
(clone: PlainObject, key: string): PlainObject => { | ||
clone[key] = object[key]; | ||
|
||
return clone; | ||
}, | ||
{} | ||
) | ||
); | ||
|
||
expect(handleCopy).toHaveBeenCalledTimes( | ||
Object.getOwnPropertyNames(object).length | ||
); | ||
}); | ||
|
||
it('will create an object clone when property symbols are not supported', () => { | ||
const object: PlainObject = { | ||
bar: { baz: 'quz' }, | ||
}; | ||
|
||
Object.defineProperty(object, 'foo', { | ||
value: 'bar', | ||
}); | ||
|
||
Object.defineProperty(object, Symbol('quz'), { | ||
enumerable: true, | ||
value: 'blah', | ||
}); | ||
|
||
const handleCopy = jest.fn().mockImplementation((arg) => arg); | ||
const cache = createCache(); | ||
|
||
const result = copiers.copyObjectStrict( | ||
object, | ||
Object.getPrototypeOf(object), | ||
handleCopy, | ||
cache | ||
); | ||
|
||
expect(result).not.toBe(object); | ||
expect(result).toEqual(object); | ||
|
||
expect(handleCopy).toHaveBeenCalledTimes( | ||
Object.getOwnPropertyNames(object).length + | ||
Object.getOwnPropertySymbols(object).length | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.