Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add minimal
Object.assign
ponyfill (#379)
since we can not rely on it being present in all supported runtimes. Even though the interface is the same as `Object.assign`, it behaves slightly differently from the one provided by browsers (see tests). This was extracted from #338 to support development in #367
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
const { assign } = require('../../lib/conventions') | ||
|
||
describe('assign', () => { | ||
test.each([null, undefined, true, false, 0, NaN])( | ||
'should throw when `target` is `%s`', | ||
(target) => { | ||
expect(() => assign(target, {})).toThrow(TypeError) | ||
} | ||
) | ||
test('should return target', () => { | ||
const target = {} | ||
expect(assign(target, undefined)).toBe(target) | ||
}) | ||
test('should copy all enumerable fields from source to target', () => { | ||
const target = {} | ||
const source = { a: 'A', 0: 0 } | ||
|
||
assign(target, source) | ||
|
||
expect(target).toEqual(source) | ||
}) | ||
test('should not copy prototype properties to source', () => { | ||
const target = {} | ||
function Clazz(yes) { | ||
this.yes = yes | ||
} | ||
Clazz.prototype.dont = 5 | ||
Clazz.prototype.hasOwnProperty = () => true | ||
const source = new Clazz(1) | ||
|
||
assign(target, source) | ||
|
||
expect(target).toEqual({ yes: 1 }) | ||
}) | ||
test('should have no issue with null source', () => { | ||
const target = {} | ||
assign(target, null) | ||
}) | ||
test('should have no issue with undefined source', () => { | ||
const target = {} | ||
assign(target, undefined) | ||
}) | ||
test('should override existing keys', () => { | ||
const target = { key: 4, same: 'same' } | ||
const source = { key: undefined } | ||
|
||
assign(target, source) | ||
|
||
expect(target).toEqual({ key: undefined, same: 'same' }) | ||
}) | ||
}) |