-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kitify): Add assign utility and tests (#10)
- Loading branch information
Showing
3 changed files
with
63 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,38 @@ | ||
import assign from '../assign' | ||
|
||
describe('assign()', () => { | ||
it('should return an object', () => { | ||
expect(assign({}, {})).toEqual({}) | ||
expect(assign({}, {}, {})).toEqual({}) | ||
}) | ||
|
||
it('should return an object with properties from all input objects', () => { | ||
const target = { a: 1 } | ||
const source1 = { b: 2 } | ||
const source2 = { c: 3 } | ||
const result = assign(target, source1, source2) | ||
expect(result).toEqual({ a: 1, b: 2, c: 3 }) | ||
}) | ||
|
||
it('should overwrite properties in target object with properties from input objects', () => { | ||
const target = { a: 1, b: 2 } | ||
const source1 = { b: 3, c: 4 } | ||
const source2 = { c: 5 } | ||
const result = assign(target, source1, source2) | ||
expect(result).toEqual({ a: 1, b: 3, c: 5 }) | ||
}) | ||
|
||
it('should throw an error if target input is undefined or null', () => { | ||
expect(() => assign(undefined as any, { a: 1 })).toThrow(TypeError) | ||
expect(() => assign(null as any, { a: 1 })).toThrow(TypeError) | ||
}) | ||
|
||
it('should change source object or not', () => { | ||
const target = {} | ||
const source1 = { a: 1 } | ||
expect(assign(target, source1) === target).toBe(true) | ||
expect(assign({}, target, source1) === target).toBe(false) | ||
expect(assign(target, source1) === source1).toBe(false) | ||
expect(assign(target, source1)).toEqual(source1) | ||
}) | ||
}) |
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,20 @@ | ||
interface Assign { | ||
<T extends {}, U>(target: T, source: U): T & U | ||
<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V | ||
<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W | ||
} | ||
|
||
const assign: Assign = | ||
Object.assign || | ||
function (target: Record<keyof any, any>, ...sources: any[]) { | ||
for (let i = 1, len = sources.length; i < len; i++) { | ||
const source = sources[i] | ||
for (const key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key] | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default assign |