From 1dcf72a8b5324ae58026baea66bedba7eb872cd2 Mon Sep 17 00:00:00 2001 From: Mariner Date: Mon, 23 Dec 2024 20:42:34 +0800 Subject: [PATCH] feat(kitify): Add assign utility and tests (#10) --- libs/kitify/config.mjs | 5 +++ .../src/object/__tests__/assign.test.ts | 38 +++++++++++++++++++ libs/kitify/src/object/assign.ts | 20 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 libs/kitify/src/object/__tests__/assign.test.ts create mode 100644 libs/kitify/src/object/assign.ts diff --git a/libs/kitify/config.mjs b/libs/kitify/config.mjs index 5561c8b..abd2060 100644 --- a/libs/kitify/config.mjs +++ b/libs/kitify/config.mjs @@ -13,6 +13,11 @@ export default { input: 'src/type/isType.ts', name: 'isType', }, + //==> object + { + input: 'src/object/assign.ts', + name: 'assign', + }, //==> dom { input: 'src/dom/detectMouseDirection.ts', diff --git a/libs/kitify/src/object/__tests__/assign.test.ts b/libs/kitify/src/object/__tests__/assign.test.ts new file mode 100644 index 0000000..599a599 --- /dev/null +++ b/libs/kitify/src/object/__tests__/assign.test.ts @@ -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) + }) +}) diff --git a/libs/kitify/src/object/assign.ts b/libs/kitify/src/object/assign.ts new file mode 100644 index 0000000..de61712 --- /dev/null +++ b/libs/kitify/src/object/assign.ts @@ -0,0 +1,20 @@ +interface Assign { + (target: T, source: U): T & U + (target: T, source1: U, source2: V): T & U & V + (target: T, source1: U, source2: V, source3: W): T & U & V & W +} + +const assign: Assign = + Object.assign || + function (target: Record, ...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