diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46d7a986..748ba93d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,7 @@ jobs: run: npm install - name: Run tests - run: npm test:github" + run: npm test:github - name: Upload Allure report if: ${{ github.event.inputs.upload_report == 'true' }} diff --git a/functionsUnittests/objectFunctions/applyDefaults.test.ts b/functionsUnittests/objectFunctions/applyDefaults.test.ts index 021ccc1c..1b019831 100644 --- a/functionsUnittests/objectFunctions/applyDefaults.test.ts +++ b/functionsUnittests/objectFunctions/applyDefaults.test.ts @@ -28,53 +28,71 @@ describe('applyDefaults', () => { expect(result).toEqual(expected); }); - // Test case 4: Handle non-object input for the first parameter (number) - it('4. should throw a TypeError if the first input is a number', () => { + // Test case 4: Handle empty defaults + it('4. should return the input object if defaults are empty', () => { + const obj = { a: 1, b: 2 }; + const defaults = {}; + const result = applyDefaults(obj, defaults); + const expected = { a: 1, b: 2 }; + expect(result).toEqual(expected); + }); + + // Test case 5: Handle both input objects being empty + it('5. should return an empty object if both inputs are empty', () => { + const obj = {}; + const defaults = {}; + const result = applyDefaults(obj, defaults); + const expected = {}; + expect(result).toEqual(expected); + }); + + // Test case 6: Handle non-object input for the first parameter (number) + it('6. should throw a TypeError if the first input is a number', () => { expect(() => applyDefaults(42 as any, { a: 1 })).toThrow(TypeError); }); - // Test case 5: Handle non-object input for the first parameter (string) - it('5. should throw a TypeError if the first input is a string', () => { + // Test case 7: Handle non-object input for the first parameter (string) + it('7. should throw a TypeError if the first input is a string', () => { expect(() => applyDefaults('string' as any, { a: 1 })).toThrow(TypeError); }); - // Test case 6: Handle non-object input for the first parameter (boolean) - it('6. should throw a TypeError if the first input is a boolean', () => { + // Test case 8: Handle non-object input for the first parameter (boolean) + it('8. should throw a TypeError if the first input is a boolean', () => { expect(() => applyDefaults(true as any, { a: 1 })).toThrow(TypeError); }); - // Test case 7: Handle non-object input for the first parameter (null) - it('7. should throw a TypeError if the first input is null', () => { + // Test case 9: Handle non-object input for the first parameter (null) + it('9. should throw a TypeError if the first input is null', () => { expect(() => applyDefaults(null as any, { a: 1 })).toThrow(TypeError); }); - // Test case 8: Handle non-object input for the first parameter (undefined) - it('8. should throw a TypeError if the first input is undefined', () => { + // Test case 10: Handle non-object input for the first parameter (undefined) + it('10. should throw a TypeError if the first input is undefined', () => { expect(() => applyDefaults(undefined as any, { a: 1 })).toThrow(TypeError); }); - // Test case 9: Handle non-object input for the second parameter (number) - it('9. should throw a TypeError if the second input is a number', () => { + // Test case 11: Handle non-object input for the second parameter (number) + it('11. should throw a TypeError if the second input is a number', () => { expect(() => applyDefaults({ a: 1 }, 42 as any)).toThrow(TypeError); }); - // Test case 10: Handle non-object input for the second parameter (string) - it('10. should throw a TypeError if the second input is a string', () => { + // Test case 12: Handle non-object input for the second parameter (string) + it('12. should throw a TypeError if the second input is a string', () => { expect(() => applyDefaults({ a: 1 }, 'string' as any)).toThrow(TypeError); }); - // Test case 11: Handle non-object input for the second parameter (boolean) - it('11. should throw a TypeError if the second input is a boolean', () => { + // Test case 13: Handle non-object input for the second parameter (boolean) + it('13. should throw a TypeError if the second input is a boolean', () => { expect(() => applyDefaults({ a: 1 }, true as any)).toThrow(TypeError); }); - // Test case 12: Handle non-object input for the second parameter (null) - it('12. should throw a TypeError if the second input is null', () => { + // Test case 14: Handle non-object input for the second parameter (null) + it('14. should throw a TypeError if the second input is null', () => { expect(() => applyDefaults({ a: 1 }, null as any)).toThrow(TypeError); }); - // Test case 13: Handle non-object input for the second parameter (undefined) - it('13. should throw a TypeError if the second input is undefined', () => { + // Test case 15: Handle non-object input for the second parameter (undefined) + it('15. should throw a TypeError if the second input is undefined', () => { expect(() => applyDefaults({ a: 1 }, undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/deepEqual.test.ts b/functionsUnittests/objectFunctions/deepEqual.test.ts index 978fe0c7..188fc197 100644 --- a/functionsUnittests/objectFunctions/deepEqual.test.ts +++ b/functionsUnittests/objectFunctions/deepEqual.test.ts @@ -1,129 +1,86 @@ import { deepEqual } from '../../objectFunctions/deepEqual'; describe('deepEqual', () => { - // Test case 1: Compare two identical simple objects - it('1. should return true for two identical simple objects', () => { + // Test case 1: Compare two simple objects + it('1. should return true for two equal simple objects', () => { const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 2 }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + expect(deepEqual(obj1, obj2)).toBe(true); }); // Test case 2: Compare two different simple objects it('2. should return false for two different simple objects', () => { const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 3 }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(deepEqual(obj1, obj2)).toBe(false); }); - // Test case 3: Compare two identical nested objects - it('3. should return true for two identical nested objects', () => { + // Test case 3: Compare two nested objects + it('3. should return true for two equal nested objects', () => { const obj1 = { a: 1, b: { c: 2, d: 3 } }; const obj2 = { a: 1, b: { c: 2, d: 3 } }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + expect(deepEqual(obj1, obj2)).toBe(true); }); // Test case 4: Compare two different nested objects it('4. should return false for two different nested objects', () => { const obj1 = { a: 1, b: { c: 2, d: 3 } }; const obj2 = { a: 1, b: { c: 2, d: 4 } }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(deepEqual(obj1, obj2)).toBe(false); }); - // Test case 5: Compare two identical arrays - it('5. should return true for two identical arrays', () => { + // Test case 5: Compare two arrays + it('5. should return true for two equal arrays', () => { const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3]; - const result = deepEqual(arr1, arr2); - const expected = true; - expect(result).toBe(expected); + expect(deepEqual(arr1, arr2)).toBe(true); }); // Test case 6: Compare two different arrays it('6. should return false for two different arrays', () => { const arr1 = [1, 2, 3]; const arr2 = [1, 2, 4]; - const result = deepEqual(arr1, arr2); - const expected = false; - expect(result).toBe(expected); + expect(deepEqual(arr1, arr2)).toBe(false); }); - // Test case 7: Compare two identical objects with arrays - it('7. should return true for two identical objects with arrays', () => { - const obj1 = { a: [1, 2, 3], b: 'test' }; - const obj2 = { a: [1, 2, 3], b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + // Test case 7: Compare objects with various data types + it('7. should return true for objects with equal various data types', () => { + const obj1 = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const obj2 = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + expect(deepEqual(obj1, obj2)).toBe(true); }); - // Test case 8: Compare two different objects with arrays - it('8. should return false for two different objects with arrays', () => { - const obj1 = { a: [1, 2, 3], b: 'test' }; - const obj2 = { a: [1, 2, 4], b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + // Test case 8: Compare objects with symbols + it('8. should return true for objects with equal symbols', () => { + const sym1 = Symbol('sym1'); + const sym2 = Symbol('sym2'); + const obj1 = { [sym1]: 1, [sym2]: 2 }; + const obj2 = { [sym1]: 1, [sym2]: 2 }; + expect(deepEqual(obj1, obj2)).toBe(true); }); - // Test case 9: Compare two identical objects with functions - it('9. should return true for two identical objects with functions', () => { - const obj1 = { a: () => 1, b: 'test' }; - const obj2 = { a: () => 1, b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + // Test case 9: Handle non-object input (number) + it('9. should return false if one input is a number', () => { + expect(deepEqual(42 as any, { a: 1 })).toBe(false); }); - // Test case 10: Compare two different objects with functions - it('10. should return false for two different objects with functions', () => { - const obj1 = { a: () => 1, b: 'test' }; - const obj2 = { a: () => 2, b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + // Test case 10: Handle non-object input (string) + it('10. should return false if one input is a string', () => { + expect(deepEqual('string' as any, { a: 1 })).toBe(false); }); - // Test case 11: Compare two identical objects with symbols - it('11. should return true for two identical objects with symbols', () => { - const obj1 = { a: Symbol('a'), b: 'test' }; - const obj2 = { a: Symbol('a'), b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + // Test case 11: Handle non-object input (boolean) + it('11. should return false if one input is a boolean', () => { + expect(deepEqual(true as any, { a: 1 })).toBe(false); }); - // Test case 12: Compare two different objects with symbols - it('12. should return false for two different objects with symbols', () => { - const obj1 = { a: Symbol('a'), b: 'test' }; - const obj2 = { a: Symbol('b'), b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + // Test case 12: Handle non-object input (null) + it('12. should return false if one input is null', () => { + expect(deepEqual(null as any, { a: 1 })).toBe(false); }); - // Test case 13: Compare two identical objects with BigInt values - it('13. should return true for two identical objects with BigInt values', () => { - const obj1 = { a: BigInt(1), b: 'test' }; - const obj2 = { a: BigInt(1), b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); - }); - - // Test case 14: Compare two different objects with BigInt values - it('14. should return false for two different objects with BigInt values', () => { - const obj1 = { a: BigInt(1), b: 'test' }; - const obj2 = { a: BigInt(2), b: 'test' }; - const result = deepEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + // Test case 13: Handle non-object input (undefined) + it('13. should return false if one input is undefined', () => { + expect(deepEqual(undefined as any, { a: 1 })).toBe(false); }); }); diff --git a/functionsUnittests/objectFunctions/deepFreeze.test.ts b/functionsUnittests/objectFunctions/deepFreeze.test.ts index 5a835324..1a143105 100644 --- a/functionsUnittests/objectFunctions/deepFreeze.test.ts +++ b/functionsUnittests/objectFunctions/deepFreeze.test.ts @@ -4,51 +4,61 @@ describe('deepFreeze', () => { // Test case 1: Deep freeze a simple object it('1. should deep freeze a simple object', () => { const obj = { a: 1, b: 2 }; - const result = deepFreeze(obj); - expect(Object.isFrozen(result)).toBe(true); - expect(() => { (result as any).a = 3; }).toThrow(TypeError); + const frozenObj = deepFreeze(obj); + expect(Object.isFrozen(frozenObj)).toBe(true); + expect(() => { frozenObj.a = 2; }).toThrow(TypeError); }); // Test case 2: Deep freeze a nested object it('2. should deep freeze a nested object', () => { const obj = { a: 1, b: { c: 2, d: 3 } }; - const result = deepFreeze(obj); - expect(Object.isFrozen(result)).toBe(true); - expect(Object.isFrozen(result.b)).toBe(true); - expect(() => { (result.b as any).c = 4; }).toThrow(TypeError); + const frozenObj = deepFreeze(obj); + expect(Object.isFrozen(frozenObj)).toBe(true); + expect(Object.isFrozen(frozenObj.b)).toBe(true); + expect(() => { frozenObj.b.c = 3; }).toThrow(TypeError); }); // Test case 3: Deep freeze an array it('3. should deep freeze an array', () => { const arr = [1, 2, { a: 3 }]; - const result = deepFreeze(arr); - expect(Object.isFrozen(result)).toBe(true); - expect(Object.isFrozen(result[2])).toBe(true); - expect(() => { (result[2] as any).a = 4; }).toThrow(TypeError); + const frozenArr = deepFreeze(arr); + expect(Object.isFrozen(frozenArr)).toBe(true); + expect(Object.isFrozen(frozenArr[2])).toBe(true); + expect(() => { frozenArr[0] = 4; }).toThrow(TypeError); }); - // Test case 4: Handle non-object input (number) - it('4. should throw a TypeError if input is a number', () => { + // Test case 4: Deep freeze an object with various data types + it('4. should deep freeze an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const frozenObj = deepFreeze(obj); + expect(Object.isFrozen(frozenObj)).toBe(true); + expect(Object.isFrozen(frozenObj.f)).toBe(true); + expect(Object.isFrozen(frozenObj.g)).toBe(true); + expect(() => { frozenObj.a = 2; }).toThrow(TypeError); + }); + + // Test case 5: Handle non-object input (number) + it('5. should throw a TypeError if input is a number', () => { expect(() => deepFreeze(42 as any)).toThrow(TypeError); }); - // Test case 5: Handle non-object input (string) - it('5. should throw a TypeError if input is a string', () => { + // Test case 6: Handle non-object input (string) + it('6. should throw a TypeError if input is a string', () => { expect(() => deepFreeze('string' as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (boolean) - it('6. should throw a TypeError if input is a boolean', () => { + // Test case 7: Handle non-object input (boolean) + it('7. should throw a TypeError if input is a boolean', () => { expect(() => deepFreeze(true as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (null) - it('7. should throw a TypeError if input is null', () => { + // Test case 8: Handle non-object input (null) + it('8. should throw a TypeError if input is null', () => { expect(() => deepFreeze(null as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should throw a TypeError if input is undefined', () => { + // Test case 9: Handle non-object input (undefined) + it('9. should throw a TypeError if input is undefined', () => { expect(() => deepFreeze(undefined as any)).toThrow(TypeError); }); -}); +}); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/deepMerge.test.ts b/functionsUnittests/objectFunctions/deepMerge.test.ts index d8d41bfb..3dbc3f0b 100644 --- a/functionsUnittests/objectFunctions/deepMerge.test.ts +++ b/functionsUnittests/objectFunctions/deepMerge.test.ts @@ -2,79 +2,99 @@ import { deepMerge } from '../../objectFunctions/deepMerge'; describe('deepMerge', () => { // Test case 1: Deep merge two simple objects - it('1. should deep merge two simple objects', () => { - const obj1 = { a: 1, b: 2 }; - const obj2 = { b: 3, c: 4 }; - const result = deepMerge(obj1, obj2); + it('1. should deeply merge two simple objects', () => { + const target = { a: 1, b: 2 }; + const source = { b: 3, c: 4 }; + const result = deepMerge(target, source); const expected = { a: 1, b: 3, c: 4 }; expect(result).toEqual(expected); }); // Test case 2: Deep merge nested objects - it('2. should deep merge nested objects', () => { - const obj1 = { a: 1, b: { c: 2 } }; - const obj2 = { b: { d: 3 }, e: 4 }; - const result = deepMerge(obj1, obj2); - const expected = { a: 1, b: { c: 2, d: 3 }, e: 4 }; + it('2. should deeply merge nested objects', () => { + const target = { a: 1, b: { x: 2, y: 3 } }; + const source = { b: { y: 4, z: 5 }, c: 6 }; + const result = deepMerge(target, source); + const expected = { a: 1, b: { x: 2, y: 4, z: 5 }, c: 6 }; expect(result).toEqual(expected); }); - // Test case 3: Deep merge arrays - it('3. should deep merge arrays', () => { - const obj1 = { a: [1, 2] }; - const obj2 = { a: [3, 4] }; - const result = deepMerge(obj1, obj2); - const expected = { a: [1, 2, 3, 4] }; + // Test case 3: Deep merge with arrays + it('3. should deeply merge objects with arrays', () => { + const target = { a: [1, 2], b: 2 }; + const source = { a: [3, 4], c: 3 }; + const result = deepMerge(target, source); + const expected = { a: [1, 2, 3, 4], b: 2, c: 3 }; expect(result).toEqual(expected); }); - // Test case 4: Handle non-object input (target is number) - it('4. should throw a TypeError if target is a number', () => { - expect(() => deepMerge(42 as any, { a: 1 })).toThrow(TypeError); + // Test case 4: Deep merge with various data types + it('4. should deeply merge objects with various data types', () => { + const target = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const source = { a: 2, b: 'new string', c: false, d: 'not null', e: 'defined', f: [4, 5], g: { i: 5 } }; + const result = deepMerge(target, source); + const expected = { a: 2, b: 'new string', c: false, d: 'not null', e: 'defined', f: [1, 2, 3, 4, 5], g: { h: 4, i: 5 } }; + expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (source is number) - it('5. should throw a TypeError if source is a number', () => { - expect(() => deepMerge({ a: 1 }, 42 as any)).toThrow(TypeError); + // Test case 5: Deep merge with symbols + it('5. should deeply merge objects with symbols', () => { + const sym1 = Symbol('sym1'); + const sym2 = Symbol('sym2'); + const target = { [sym1]: 1 }; + const source = { [sym2]: 2 }; + const result = deepMerge(target, source); + const expected = { [sym1]: 1, [sym2]: 2 }; + expect(result).toEqual(expected); }); - // Test case 6: Handle non-object input (target is string) - it('6. should throw a TypeError if target is a string', () => { - expect(() => deepMerge('string' as any, { a: 1 })).toThrow(TypeError); + // Test case 6: Handle non-object input for the target (number) + it('6. should throw a TypeError if the target is a number', () => { + expect(() => deepMerge(42 as any, { a: 1 })).toThrow(TypeError); }); - // Test case 7: Handle non-object input (source is string) - it('7. should throw a TypeError if source is a string', () => { - expect(() => deepMerge({ a: 1 }, 'string' as any)).toThrow(TypeError); + // Test case 7: Handle non-object input for the target (string) + it('7. should throw a TypeError if the target is a string', () => { + expect(() => deepMerge('string' as any, { a: 1 })).toThrow(TypeError); }); - // Test case 8: Handle non-object input (target is boolean) - it('8. should throw a TypeError if target is a boolean', () => { + // Test case 8: Handle non-object input for the target (boolean) + it('8. should throw a TypeError if the target is a boolean', () => { expect(() => deepMerge(true as any, { a: 1 })).toThrow(TypeError); }); - // Test case 9: Handle non-object input (source is boolean) - it('9. should throw a TypeError if source is a boolean', () => { - expect(() => deepMerge({ a: 1 }, true as any)).toThrow(TypeError); + // Test case 9: Handle non-object input for the target (null) + it('9. should throw a TypeError if the target is null', () => { + expect(() => deepMerge(null as any, { a: 1 })).toThrow(TypeError); }); - // Test case 10: Handle non-object input (target is null) - it('10. should throw a TypeError if target is null', () => { - expect(() => deepMerge(null as any, { a: 1 })).toThrow(TypeError); + // Test case 10: Handle non-object input for the target (undefined) + it('10. should throw a TypeError if the target is undefined', () => { + expect(() => deepMerge(undefined as any, { a: 1 })).toThrow(TypeError); }); - // Test case 11: Handle non-object input (source is null) - it('11. should throw a TypeError if source is null', () => { - expect(() => deepMerge({ a: 1 }, null as any)).toThrow(TypeError); + // Test case 11: Handle non-object input for the source (number) + it('11. should throw a TypeError if the source is a number', () => { + expect(() => deepMerge({ a: 1 }, 42 as any)).toThrow(TypeError); }); - // Test case 12: Handle non-object input (target is undefined) - it('12. should throw a TypeError if target is undefined', () => { - expect(() => deepMerge(undefined as any, { a: 1 })).toThrow(TypeError); + // Test case 12: Handle non-object input for the source (string) + it('12. should throw a TypeError if the source is a string', () => { + expect(() => deepMerge({ a: 1 }, 'string' as any)).toThrow(TypeError); + }); + + // Test case 13: Handle non-object input for the source (boolean) + it('13. should throw a TypeError if the source is a boolean', () => { + expect(() => deepMerge({ a: 1 }, true as any)).toThrow(TypeError); + }); + + // Test case 14: Handle non-object input for the source (null) + it('14. should throw a TypeError if the source is null', () => { + expect(() => deepMerge({ a: 1 }, null as any)).toThrow(TypeError); }); - // Test case 13: Handle non-object input (source is undefined) - it('13. should throw a TypeError if source is undefined', () => { + // Test case 15: Handle non-object input for the source (undefined) + it('15. should throw a TypeError if the source is undefined', () => { expect(() => deepMerge({ a: 1 }, undefined as any)).toThrow(TypeError); }); -}); \ No newline at end of file +}); diff --git a/functionsUnittests/objectFunctions/flattenObject.test.ts b/functionsUnittests/objectFunctions/flattenObject.test.ts index 362f50fc..0bb5539d 100644 --- a/functionsUnittests/objectFunctions/flattenObject.test.ts +++ b/functionsUnittests/objectFunctions/flattenObject.test.ts @@ -13,48 +13,56 @@ describe('flattenObject', () => { it('2. should flatten a nested object', () => { const obj = { a: 1, b: { c: 2, d: 3 } }; const result = flattenObject(obj); - const expected = { a: 1, 'b.c': 2, 'b.d': 3 }; + const expected = { 'a': 1, 'b.c': 2, 'b.d': 3 }; expect(result).toEqual(expected); }); // Test case 3: Flatten an object with arrays it('3. should flatten an object with arrays', () => { - const obj = { a: [1, 2, 3], b: { c: 4 } }; + const obj = { a: 1, b: [2, 3] }; const result = flattenObject(obj); - const expected = { 'a.0': 1, 'a.1': 2, 'a.2': 3, 'b.c': 4 }; + const expected = { 'a': 1, 'b[0]': 2, 'b[1]': 3 }; expect(result).toEqual(expected); }); - // Test case 4: Flatten an object with nested arrays - it('4. should flatten an object with nested arrays', () => { - const obj = { a: { b: [1, 2, { c: 3 }] } }; + // Test case 4: Flatten an object with various data types + it('4. should flatten an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; const result = flattenObject(obj); - const expected = { 'a.b.0': 1, 'a.b.1': 2, 'a.b.2.c': 3 }; + const expected = { 'a': 1, 'b': 'string', 'c': true, 'd': null, 'e': undefined, 'f[0]': 1, 'f[1]': 2, 'f[2]': 3, 'g.h': 4 }; expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 5: Flatten an object with nested arrays + it('5. should flatten an object with nested arrays', () => { + const obj = { a: [1, [2, 3]], b: { c: [4, 5] } }; + const result = flattenObject(obj); + const expected = { 'a[0]': 1, 'a[1][0]': 2, 'a[1][1]': 3, 'b.c[0]': 4, 'b.c[1]': 5 }; + expect(result).toEqual(expected); + }); + + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { expect(() => flattenObject(42 as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { expect(() => flattenObject('string' as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { expect(() => flattenObject(true as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { expect(() => flattenObject(null as any)).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { expect(() => flattenObject(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/flipObject.test.ts b/functionsUnittests/objectFunctions/flipObject.test.ts index f794876e..1b42284b 100644 --- a/functionsUnittests/objectFunctions/flipObject.test.ts +++ b/functionsUnittests/objectFunctions/flipObject.test.ts @@ -3,58 +3,66 @@ import { flipObject } from '../../objectFunctions/flipObject'; describe('flipObject', () => { // Test case 1: Flip a simple object it('1. should flip a simple object', () => { - const obj = { a: 1, b: 2 }; + const obj = { a: 1, b: 2, c: 3 }; const result = flipObject(obj); - const expected = { "1": 'a', "2": 'b' }; + const expected = { '1': 'a', '2': 'b', '3': 'c' }; expect(result).toEqual(expected); }); - // Test case 2: Flip an object with string values - it('2. should flip an object with string values', () => { - const obj = { a: 'x', b: 'y' }; + // Test case 2: Flip an object with arrays as values + it('2. should flip an object with arrays as values', () => { + const obj = { a: [1, 2], b: [3, 4] }; const result = flipObject(obj); - const expected = { x: 'a', y: 'b' }; + const expected = { '1,2': 'a', '3,4': 'b' }; expect(result).toEqual(expected); }); - // Test case 3: Flip an object with mixed types of values - it('3. should flip an object with mixed types of values', () => { - const obj = { a: 1, b: 'x', c: true }; + // Test case 3: Flip an object with non-unique values + it('3. should handle non-unique values by keeping the last key', () => { + const obj = { a: 1, b: 1, c: 2 }; const result = flipObject(obj); - const expected = { "1": 'a', x: 'b', "true": 'c' }; + const expected = { '1': 'b', '2': 'c' }; expect(result).toEqual(expected); }); - // Test case 4: Handle an empty object - it('4. should handle an empty object', () => { - const obj = {}; + // Test case 4: Flip an object with nested objects + it('4. should handle nested objects by converting them to strings', () => { + const obj = { a: { b: 1 }, c: { d: 2 } }; const result = flipObject(obj); - const expected = {}; + const expected = { '[object Object]': 'c' }; // Last key wins expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 5: Flip an object with various data types + it('5. should flip an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = flipObject(obj); + const expected = { '1': 'a', 'string': 'b', 'true': 'c', 'null': 'd', 'undefined': 'e', '1,2,3': 'f', '[object Object]': 'g' }; + expect(result).toEqual(expected); + }); + + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { expect(() => flipObject(42 as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { expect(() => flipObject('string' as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { expect(() => flipObject(true as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { expect(() => flipObject(null as any)).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { expect(() => flipObject(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/getDeepEqualityHash.test.ts b/functionsUnittests/objectFunctions/getDeepEqualityHash.test.ts index 386d804d..463a6171 100644 --- a/functionsUnittests/objectFunctions/getDeepEqualityHash.test.ts +++ b/functionsUnittests/objectFunctions/getDeepEqualityHash.test.ts @@ -2,51 +2,80 @@ import { getDeepEqualityHash } from '../../objectFunctions/getDeepEqualityHash'; describe('getDeepEqualityHash', () => { // Test case 1: Generate hash for a simple object - it('1. should generate hash for a simple object', () => { + it('1. should generate a hash for a simple object', () => { const obj = { a: 1, b: 2 }; const result = getDeepEqualityHash(obj); - const expected = getDeepEqualityHash({ a: 1, b: 2 }); - expect(result).toBe(expected); + expect(typeof result).toBe('number'); }); // Test case 2: Generate hash for a nested object - it('2. should generate hash for a nested object', () => { + it('2. should generate a hash for a nested object', () => { const obj = { a: 1, b: { c: 2, d: 3 } }; const result = getDeepEqualityHash(obj); - const expected = getDeepEqualityHash({ a: 1, b: { c: 2, d: 3 } }); - expect(result).toBe(expected); + expect(typeof result).toBe('number'); }); // Test case 3: Generate hash for an array - it('3. should generate hash for an array', () => { + it('3. should generate a hash for an array', () => { const arr = [1, 2, { a: 3 }]; const result = getDeepEqualityHash(arr); - const expected = getDeepEqualityHash([1, 2, { a: 3 }]); - expect(result).toBe(expected); + expect(typeof result).toBe('number'); }); - // Test case 4: Handle non-object input (number) - it('4. should throw a TypeError if input is a number', () => { - expect(() => getDeepEqualityHash(42 as any)).toThrow(TypeError); + // Test case 4: Generate hash for an object with various data types + it('4. should generate a hash for an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = getDeepEqualityHash(obj); + expect(typeof result).toBe('number'); + }); + + // Test case 5: Generate hash for an object with a date + it('5. should generate a hash for an object with a date', () => { + const obj = { a: new Date() }; + const result = getDeepEqualityHash(obj); + expect(typeof result).toBe('number'); + }); + + // Test case 6: Generate hash for an object with a regex + it('6. should generate a hash for an object with a regex', () => { + const obj = { a: /test/gi }; + const result = getDeepEqualityHash(obj); + expect(typeof result).toBe('number'); + }); + + // Test case 7: Generate hash for an object with a function + it('7. should generate a hash for an object with a function', () => { + const func = () => {}; + const obj = { a: func }; + const result = getDeepEqualityHash(obj); + expect(typeof result).toBe('number'); + }); + + // Test case 8: Generate hash for an object with a symbol + it('8. should generate a hash for an object with a symbol', () => { + const sym = Symbol('test'); + const obj = { a: sym }; + const result = getDeepEqualityHash(obj); + expect(typeof result).toBe('number'); }); - // Test case 5: Handle non-object input (string) - it('5. should throw a TypeError if input is a string', () => { + // Test case 9: Handle non-object input (string) + it('9. should throw a TypeError if input is a string', () => { expect(() => getDeepEqualityHash('string' as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (boolean) - it('6. should throw a TypeError if input is a boolean', () => { + // Test case 10: Handle non-object input (boolean) + it('10. should throw a TypeError if input is a boolean', () => { expect(() => getDeepEqualityHash(true as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (null) - it('7. should throw a TypeError if input is null', () => { + // Test case 11: Handle non-object input (null) + it('11. should throw a TypeError if input is null', () => { expect(() => getDeepEqualityHash(null as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should throw a TypeError if input is undefined', () => { + // Test case 12: Handle non-object input (undefined) + it('12. should throw a TypeError if input is undefined', () => { expect(() => getDeepEqualityHash(undefined as any)).toThrow(TypeError); }); }); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/getNestedValue.test.ts b/functionsUnittests/objectFunctions/getNestedValue.test.ts index 157c4073..f40131f5 100644 --- a/functionsUnittests/objectFunctions/getNestedValue.test.ts +++ b/functionsUnittests/objectFunctions/getNestedValue.test.ts @@ -1,68 +1,74 @@ import { getNestedValue } from '../../objectFunctions/getNestedValue'; describe('getNestedValue', () => { - // Test case 1: Retrieve a nested value from an object - it('1. should retrieve a nested value from an object', () => { - const obj = { a: { b: { c: 42 } } }; - const result = getNestedValue(obj, 'a.b.c'); - const expected = 42; + // Test case 1: Get a nested value from a simple object + it('1. should get a nested value from a simple object', () => { + const obj = { a: { b: { c: 3 } } }; + const result = getNestedValue(obj, 'a.b.c'); + const expected = 3; expect(result).toBe(expected); }); - // Test case 2: Return undefined for a non-existent path - it('2. should return undefined for a non-existent path', () => { - const obj = { a: { b: { c: 42 } } }; - const result = getNestedValue(obj, 'a.b.d'); - const expected = undefined; + // Test case 2: Get a nested value from a nested object + it('2. should get a nested value from a nested object', () => { + const obj = { a: { b: { c: { d: 4 } } } }; + const result = getNestedValue(obj, 'a.b.c.d'); + const expected = 4; expect(result).toBe(expected); }); - // Test case 3: Handle an empty path - it('3. should return the object itself for an empty path', () => { - const obj = { a: 1 }; - const result = getNestedValue(obj, ''); - const expected = obj; + // Test case 3: Get a nested value from an array + it('3. should get a nested value from an array', () => { + const obj = { a: [{ b: 2 }, { c: 3 }] }; + const result = getNestedValue(obj, 'a.1.c'); + const expected = 3; expect(result).toBe(expected); }); - // Test case 4: Handle a path with an array index - it('4. should retrieve a value from an array within an object', () => { - const obj = { a: { b: [1, 2, 3] } }; - const result = getNestedValue(obj, 'a.b.1'); - const expected = 2; + // Test case 4: Get a nested value from an object with various data types + it('4. should get a nested value from an object with various data types', () => { + const obj = { a: { b: { c: true, d: 'string', e: null, f: undefined, g: [1, 2, 3], h: { i: 4 } } } }; + const result = getNestedValue(obj, 'a.b.h.i'); + const expected = 4; expect(result).toBe(expected); }); - // Test case 5: Handle a path with a non-existent array index - it('5. should return undefined for a non-existent array index', () => { - const obj = { a: { b: [1, 2, 3] } }; - const result = getNestedValue(obj, 'a.b.5'); - const expected = undefined; - expect(result).toBe(expected); + // Test case 5: Return undefined for a non-existent nested value + it('5. should return undefined for a non-existent nested value', () => { + const obj = { a: { b: { c: 3 } } }; + const result = getNestedValue(obj, 'a.b.d'); + expect(result).toBeUndefined(); + }); + + // Test case 6: Return undefined for an empty path + it('6. should return undefined for an empty path', () => { + const obj = { a: { b: { c: 3 } } }; + const result = getNestedValue(obj, ''); + expect(result).toBeUndefined(); }); - // Test case 6: Handle non-object input (number) - it('6. should throw a TypeError if input is a number', () => { + // Test case 7: Handle non-object input (number) + it('7. should throw a TypeError if input is a number', () => { expect(() => getNestedValue(42 as any, 'a')).toThrow(TypeError); }); - // Test case 7: Handle non-object input (string) - it('7. should throw a TypeError if input is a string', () => { + // Test case 8: Handle non-object input (string) + it('8. should throw a TypeError if input is a string', () => { expect(() => getNestedValue('string' as any, 'a')).toThrow(TypeError); }); - // Test case 8: Handle non-object input (boolean) - it('8. should throw a TypeError if input is a boolean', () => { + // Test case 9: Handle non-object input (boolean) + it('9. should throw a TypeError if input is a boolean', () => { expect(() => getNestedValue(true as any, 'a')).toThrow(TypeError); }); - // Test case 9: Handle non-object input (null) - it('9. should throw a TypeError if input is null', () => { + // Test case 10: Handle non-object input (null) + it('10. should throw a TypeError if input is null', () => { expect(() => getNestedValue(null as any, 'a')).toThrow(TypeError); }); - // Test case 10: Handle non-object input (undefined) - it('10. should throw a TypeError if input is undefined', () => { + // Test case 11: Handle non-object input (undefined) + it('11. should throw a TypeError if input is undefined', () => { expect(() => getNestedValue(undefined as any, 'a')).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/getObjectDifference.test.ts b/functionsUnittests/objectFunctions/getObjectDifference.test.ts index 656a2e3e..40814231 100644 --- a/functionsUnittests/objectFunctions/getObjectDifference.test.ts +++ b/functionsUnittests/objectFunctions/getObjectDifference.test.ts @@ -1,17 +1,17 @@ import { getObjectDifference } from '../../objectFunctions/getObjectDifference'; describe('getObjectDifference', () => { - // Test case 1: Get difference between two objects with different values - it('1. should return the difference between two objects with different values', () => { + // Test case 1: Compute difference between two simple objects + it('1. should compute difference between two simple objects', () => { const obj1 = { a: 1, b: 2, c: 3 }; - const obj2 = { a: 1, b: 3, c: 3 }; + const obj2 = { a: 1, b: 4, d: 5 }; const result = getObjectDifference(obj1, obj2); - const expected = { b: 2 }; + const expected = { b: 4, c: undefined, d: 5 }; expect(result).toEqual(expected); }); - // Test case 2: Get difference between two identical objects - it('2. should return an empty object for two identical objects', () => { + // Test case 2: Compute difference when objects are identical + it('2. should return an empty object when objects are identical', () => { const obj1 = { a: 1, b: 2, c: 3 }; const obj2 = { a: 1, b: 2, c: 3 }; const result = getObjectDifference(obj1, obj2); @@ -19,62 +19,80 @@ describe('getObjectDifference', () => { expect(result).toEqual(expected); }); - // Test case 3: Get difference between two objects with different keys - it('3. should return the difference for objects with different keys', () => { - const obj1 = { a: 1, b: 2 } as { [key: string]: number }; - const obj2 = { a: 1, c: 3 } as { [key: string]: number }; + // Test case 3: Compute difference with nested objects + it('3. should compute difference with nested objects', () => { + const obj1 = { a: 1, b: { x: 2, y: 3 }, c: 3 }; + const obj2 = { a: 1, b: { x: 2, y: 4 }, d: 5 }; const result = getObjectDifference(obj1, obj2); - const expected = { b: 2 }; + const expected = { b: { x: 2, y: 4 }, c: undefined, d: 5 }; expect(result).toEqual(expected); }); - // Test case 4: Handle non-object input (number) - it('4. should throw a TypeError if the first input is a number', () => { + // Test case 4: Compute difference with arrays + it('4. should compute difference with arrays', () => { + const obj1 = { a: [1, 2, 3], b: 2 }; + const obj2 = { a: [1, 2, 4], b: 2 }; + const result = getObjectDifference(obj1, obj2); + const expected = { a: [1, 2, 4] }; + expect(result).toEqual(expected); + }); + + // Test case 5: Compute difference with various data types + it('5. should compute difference with various data types', () => { + const obj1 = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const obj2 = { a: 2, b: 'new string', c: false, d: 'not null', e: 'defined', f: [4, 5], g: { i: 5 } }; + const result = getObjectDifference(obj1, obj2); + const expected = { a: 2, b: 'new string', c: false, d: 'not null', e: 'defined', f: [4, 5], g: { i: 5 } }; + expect(result).toEqual(expected); + }); + + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if the first input is a number', () => { expect(() => getObjectDifference(42 as any, { a: 1 })).toThrow(TypeError); }); - // Test case 5: Handle non-object input (string) - it('5. should throw a TypeError if the first input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if the first input is a string', () => { expect(() => getObjectDifference('string' as any, { a: 1 })).toThrow(TypeError); }); - // Test case 6: Handle non-object input (boolean) - it('6. should throw a TypeError if the first input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if the first input is a boolean', () => { expect(() => getObjectDifference(true as any, { a: 1 })).toThrow(TypeError); }); - // Test case 7: Handle non-object input (null) - it('7. should throw a TypeError if the first input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if the first input is null', () => { expect(() => getObjectDifference(null as any, { a: 1 })).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should throw a TypeError if the first input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if the first input is undefined', () => { expect(() => getObjectDifference(undefined as any, { a: 1 })).toThrow(TypeError); }); - // Test case 9: Handle non-object input for the second parameter (number) - it('9. should throw a TypeError if the second input is a number', () => { + // Test case 11: Handle non-object input for the second parameter (number) + it('11. should throw a TypeError if the second input is a number', () => { expect(() => getObjectDifference({ a: 1 }, 42 as any)).toThrow(TypeError); }); - // Test case 10: Handle non-object input for the second parameter (string) - it('10. should throw a TypeError if the second input is a string', () => { + // Test case 12: Handle non-object input for the second parameter (string) + it('12. should throw a TypeError if the second input is a string', () => { expect(() => getObjectDifference({ a: 1 }, 'string' as any)).toThrow(TypeError); }); - // Test case 11: Handle non-object input for the second parameter (boolean) - it('11. should throw a TypeError if the second input is a boolean', () => { + // Test case 13: Handle non-object input for the second parameter (boolean) + it('13. should throw a TypeError if the second input is a boolean', () => { expect(() => getObjectDifference({ a: 1 }, true as any)).toThrow(TypeError); }); - // Test case 12: Handle non-object input for the second parameter (null) - it('12. should throw a TypeError if the second input is null', () => { + // Test case 14: Handle non-object input for the second parameter (null) + it('14. should throw a TypeError if the second input is null', () => { expect(() => getObjectDifference({ a: 1 }, null as any)).toThrow(TypeError); }); - // Test case 13: Handle non-object input for the second parameter (undefined) - it('13. should throw a TypeError if the second input is undefined', () => { + // Test case 15: Handle non-object input for the second parameter (undefined) + it('15. should throw a TypeError if the second input is undefined', () => { expect(() => getObjectDifference({ a: 1 }, undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/hasKey.test.ts b/functionsUnittests/objectFunctions/hasKey.test.ts index a5d55342..77b801ad 100644 --- a/functionsUnittests/objectFunctions/hasKey.test.ts +++ b/functionsUnittests/objectFunctions/hasKey.test.ts @@ -1,60 +1,84 @@ import { hasKey } from '../../objectFunctions/hasKey'; describe('hasKey', () => { - // Test case 1: Check if an object has a specific key + // Test case 1: Check if a simple object has a key it('1. should return true if the object has the key', () => { - const obj = { a: 1, b: 2 }; - const result = hasKey(obj, 'a'); - const expected = true; - expect(result).toBe(expected); + const obj = { a: 1, b: 2, c: 3 }; + const result = hasKey(obj, 'b'); + expect(result).toBe(true); }); - // Test case 2: Check if an object does not have a specific key + // Test case 2: Check if a simple object does not have a key it('2. should return false if the object does not have the key', () => { - const obj = { a: 1, b: 2 }; - const result = hasKey(obj, 'c' as keyof typeof obj); - const expected = false; - expect(result).toBe(expected); + const obj = { a: 1, b: 2, c: 3 }; + const result = hasKey(obj, 'd'); + expect(result).toBe(false); }); - // Test case 3: Check if an object has a key with undefined value - it('3. should return true if the object has the key with undefined value', () => { - const obj = { a: undefined }; - const result = hasKey(obj, 'a'); - const expected = true; - expect(result).toBe(expected); + // Test case 3: Check if a nested object has a key + it('3. should return true if a nested object has the key', () => { + const obj = { a: 1, b: { c: 2, d: 3 } }; + const result = hasKey(obj, 'b'); + expect(result).toBe(true); }); - // Test case 4: Check if an object has a key with null value - it('4. should return true if the object has the key with null value', () => { - const obj = { a: null }; - const result = hasKey(obj, 'a'); - const expected = true; - expect(result).toBe(expected); + // Test case 4: Check if a nested object does not have a key + it('4. should return false if a nested object does not have the key', () => { + const obj = { a: 1, b: { c: 2, d: 3 } }; + const result = hasKey(obj, 'c'); + expect(result).toBe(false); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 5: Check if an array has a key + it('5. should return true if an array has the key', () => { + const arr = [1, 2, 3]; + const result = hasKey(arr, '0'); + expect(result).toBe(true); + }); + + // Test case 6: Check if an array does not have a key + it('6. should return false if an array does not have the key', () => { + const arr = [1, 2, 3]; + const result = hasKey(arr, '3'); + expect(result).toBe(false); + }); + + // Test case 7: Check if an object with various data types has a key + it('7. should return true if an object with various data types has the key', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = hasKey(obj, 'f'); + expect(result).toBe(true); + }); + + // Test case 8: Check if an object with various data types does not have a key + it('8. should return false if an object with various data types does not have the key', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = hasKey(obj, 'z'); + expect(result).toBe(false); + }); + + // Test case 9: Handle non-object input (number) + it('9. should throw a TypeError if input is a number', () => { expect(() => hasKey(42 as any, 'a')).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 10: Handle non-object input (string) + it('10. should throw a TypeError if input is a string', () => { expect(() => hasKey('string' as any, 'a')).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 11: Handle non-object input (boolean) + it('11. should throw a TypeError if input is a boolean', () => { expect(() => hasKey(true as any, 'a')).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 12: Handle non-object input (null) + it('12. should throw a TypeError if input is null', () => { expect(() => hasKey(null as any, 'a')).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 13: Handle non-object input (undefined) + it('13. should throw a TypeError if input is undefined', () => { expect(() => hasKey(undefined as any, 'a')).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/isEmptyObject.test.ts b/functionsUnittests/objectFunctions/isEmptyObject.test.ts deleted file mode 100644 index aa6935f0..00000000 --- a/functionsUnittests/objectFunctions/isEmptyObject.test.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { isEmptyObject } from '../../objectFunctions/isEmptyObject'; - -describe('isEmptyObject', () => { - // Test case 1: Check an empty object - it('1. should return true for an empty object', () => { - const obj = {}; - const result = isEmptyObject(obj); - const expected = true; - expect(result).toBe(expected); - }); - - // Test case 2: Check a non-empty object - it('2. should return false for a non-empty object', () => { - const obj = { a: 1 }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 3: Handle an object with undefined values - it('3. should return false for an object with undefined values', () => { - const obj = { a: undefined }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 4: Handle an object with null values - it('4. should return false for an object with null values', () => { - const obj = { a: null }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 5: Handle an object with mixed types of values - it('5. should return false for an object with mixed types of values', () => { - const obj = { a: 1, b: 'string', c: true, d: null, e: undefined }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 6: Handle an object with nested objects - it('6. should return false for an object with nested objects', () => { - const obj = { a: { b: 1 }, c: { d: 2 } }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 7: Handle an object with arrays - it('7. should return false for an object with arrays', () => { - const obj = { a: [1, 2, 3], b: [4, 5, 6] }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 8: Handle an object with functions - it('8. should return false for an object with functions', () => { - const obj = { a: () => {}, b: function() {} }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 9: Handle an object with symbols - it('9. should return false for an object with symbols', () => { - const obj = { a: Symbol('a'), b: Symbol('b') }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 10: Handle an object with BigInt values - it('10. should return false for an object with BigInt values', () => { - const obj = { a: BigInt(1), b: BigInt(2) }; - const result = isEmptyObject(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 11: Handle non-object input (number) - it('11. should throw a TypeError if input is a number', () => { - expect(() => isEmptyObject(42 as any)).toThrow(TypeError); - }); - - // Test case 12: Handle non-object input (string) - it('12. should throw a TypeError if input is a string', () => { - expect(() => isEmptyObject('string' as any)).toThrow(TypeError); - }); - - // Test case 13: Handle non-object input (boolean) - it('13. should throw a TypeError if input is a boolean', () => { - expect(() => isEmptyObject(true as any)).toThrow(TypeError); - }); - - // Test case 14: Handle non-object input (null) - it('14. should throw a TypeError if input is null', () => { - expect(() => isEmptyObject(null as any)).toThrow(TypeError); - }); - - // Test case 15: Handle non-object input (undefined) - it('15. should throw a TypeError if input is undefined', () => { - expect(() => isEmptyObject(undefined as any)).toThrow(TypeError); - }); -}); diff --git a/functionsUnittests/objectFunctions/isObjectEmpty.test.ts b/functionsUnittests/objectFunctions/isObjectEmpty.test.ts index 1d3ef037..3e3986fb 100644 --- a/functionsUnittests/objectFunctions/isObjectEmpty.test.ts +++ b/functionsUnittests/objectFunctions/isObjectEmpty.test.ts @@ -1,108 +1,63 @@ import { isObjectEmpty } from '../../objectFunctions/isObjectEmpty'; describe('isObjectEmpty', () => { - // Test case 1: Check an empty object + // Test case 1: Check if an empty object is empty it('1. should return true for an empty object', () => { const obj = {}; const result = isObjectEmpty(obj); - const expected = true; - expect(result).toBe(expected); + expect(result).toBe(true); }); - // Test case 2: Check a non-empty object + // Test case 2: Check if a non-empty object is empty it('2. should return false for a non-empty object', () => { const obj = { a: 1 }; const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 3: Handle an object with undefined values - it('3. should return false for an object with undefined values', () => { - const obj = { a: undefined }; + // Test case 3: Check if an object with nested properties is empty + it('3. should return false for an object with nested properties', () => { + const obj = { a: { b: 1 } }; const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 4: Handle an object with null values - it('4. should return false for an object with null values', () => { - const obj = { a: null }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 5: Handle an object with mixed types of values - it('5. should return false for an object with mixed types of values', () => { - const obj = { a: 1, b: 'string', c: true, d: null, e: undefined }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 6: Handle an object with nested objects - it('6. should return false for an object with nested objects', () => { - const obj = { a: { b: 1 }, c: { d: 2 } }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 7: Handle an object with arrays - it('7. should return false for an object with arrays', () => { - const obj = { a: [1, 2, 3], b: [4, 5, 6] }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 8: Handle an object with functions - it('8. should return false for an object with functions', () => { - const obj = { a: () => {}, b: function() {} }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 9: Handle an object with symbols - it('9. should return false for an object with symbols', () => { - const obj = { a: Symbol('a'), b: Symbol('b') }; - const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); + // Test case 4: Check if an array is empty + it('4. should return false for an array', () => { + const arr = [1, 2, 3]; + const result = isObjectEmpty(arr); + expect(result).toBe(false); }); - // Test case 10: Handle an object with BigInt values - it('10. should return false for an object with BigInt values', () => { - const obj = { a: BigInt(1), b: BigInt(2) }; + // Test case 5: Check if an object with various data types is empty + it('5. should return false for an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; const result = isObjectEmpty(obj); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 11: Handle non-object input (number) - it('11. should throw a TypeError if input is a number', () => { + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { expect(() => isObjectEmpty(42 as any)).toThrow(TypeError); }); - // Test case 12: Handle non-object input (string) - it('12. should throw a TypeError if input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { expect(() => isObjectEmpty('string' as any)).toThrow(TypeError); }); - // Test case 13: Handle non-object input (boolean) - it('13. should throw a TypeError if input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { expect(() => isObjectEmpty(true as any)).toThrow(TypeError); }); - // Test case 14: Handle non-object input (null) - it('14. should throw a TypeError if input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { expect(() => isObjectEmpty(null as any)).toThrow(TypeError); }); - // Test case 15: Handle non-object input (undefined) - it('15. should throw a TypeError if input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { expect(() => isObjectEmpty(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/keysToCamelCase.test.ts b/functionsUnittests/objectFunctions/keysToCamelCase.test.ts index a04626a0..2edc0f07 100644 --- a/functionsUnittests/objectFunctions/keysToCamelCase.test.ts +++ b/functionsUnittests/objectFunctions/keysToCamelCase.test.ts @@ -1,75 +1,76 @@ import { keysToCamelCase } from '../../objectFunctions/keysToCamelCase'; describe('keysToCamelCase', () => { - // Test case 1: Convert keys of a simple object + // Test case 1: Convert keys of a simple object to camelCase it('1. should convert keys of a simple object to camelCase', () => { - const obj = { first_name: 'John', last_name: 'Doe' }; + const obj = { 'first_name': 'John', 'last_name': 'Doe' }; const result = keysToCamelCase(obj); const expected = { firstName: 'John', lastName: 'Doe' }; expect(result).toEqual(expected); }); - // Test case 2: Convert keys of a nested object + // Test case 2: Convert keys of a nested object to camelCase it('2. should convert keys of a nested object to camelCase', () => { - const obj = { user_info: { first_name: 'John', last_name: 'Doe' } }; + const obj = { 'user_info': { 'first_name': 'John', 'last_name': 'Doe' } }; const result = keysToCamelCase(obj); const expected = { userInfo: { firstName: 'John', lastName: 'Doe' } }; expect(result).toEqual(expected); }); - // Test case 3: Convert keys of an array of objects + // Test case 3: Convert keys of an array of objects to camelCase it('3. should convert keys of an array of objects to camelCase', () => { - const arr = [{ first_name: 'John' }, { last_name: 'Doe' }]; + const arr = [{ 'first_name': 'John' }, { 'last_name': 'Doe' }]; const result = keysToCamelCase(arr); const expected = [{ firstName: 'John' }, { lastName: 'Doe' }]; expect(result).toEqual(expected); }); - // Test case 4: Handle non-object input (number) - it('4. should return the input if it is a number', () => { - const input = 42; - const result = keysToCamelCase(input); - const expected = 42; - expect(result).toBe(expected); + // Test case 4: Convert keys of an object with various data types to camelCase + it('4. should convert keys of an object with various data types to camelCase', () => { + const obj = { 'first_name': 'John', 'age_years': 30, 'is_active': true, 'address_info': { 'street_name': 'Main St' } }; + const result = keysToCamelCase(obj); + const expected = { firstName: 'John', ageYears: 30, isActive: true, addressInfo: { streetName: 'Main St' } }; + expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (string) - it('5. should return the input if it is a string', () => { - const input = 'string'; - const result = keysToCamelCase(input); - const expected = 'string'; - expect(result).toBe(expected); + // Test case 5: Convert keys of an object with nested arrays to camelCase + it('5. should convert keys of an object with nested arrays to camelCase', () => { + const obj = { 'user_list': [{ 'first_name': 'John' }, { 'last_name': 'Doe' }] }; + const result = keysToCamelCase(obj); + const expected = { userList: [{ firstName: 'John' }, { lastName: 'Doe' }] }; + expect(result).toEqual(expected); }); - // Test case 6: Handle non-object input (boolean) - it('6. should return the input if it is a boolean', () => { - const input = true; - const result = keysToCamelCase(input); - const expected = true; - expect(result).toBe(expected); + // Test case 6: Handle keys that are numbers + it('6. should handle keys that are numbers', () => { + const obj = { 1: 'one', 2: 'two' }; + const result = keysToCamelCase(obj); + const expected = { 1: 'one', 2: 'two' }; + expect(result).toEqual(expected); }); - // Test case 7: Handle non-object input (null) - it('7. should return the input if it is null', () => { - const input = null; - const result = keysToCamelCase(input); - const expected = null; - expect(result).toBe(expected); + // Test case 7: Handle non-object input (number) + it('7. should throw a TypeError if input is a number', () => { + expect(() => keysToCamelCase(42 as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should return the input if it is undefined', () => { - const input = undefined; - const result = keysToCamelCase(input); - const expected = undefined; - expect(result).toBe(expected); + // Test case 8: Handle non-object input (string) + it('8. should throw a TypeError if input is a string', () => { + expect(() => keysToCamelCase('string' as any)).toThrow(TypeError); }); - // Test case 9: Convert keys with mixed cases - it('9. should convert keys with mixed cases to camelCase', () => { - const obj = { 'first-name': 'John', 'Last_Name': 'Doe' }; - const result = keysToCamelCase(obj); - const expected = { firstName: 'John', lastName: 'Doe' }; - expect(result).toEqual(expected); + // Test case 9: Handle non-object input (boolean) + it('9. should throw a TypeError if input is a boolean', () => { + expect(() => keysToCamelCase(true as any)).toThrow(TypeError); + }); + + // Test case 10: Handle non-object input (null) + it('10. should throw a TypeError if input is null', () => { + expect(() => keysToCamelCase(null as any)).toThrow(TypeError); + }); + + // Test case 11: Handle non-object input (undefined) + it('11. should throw a TypeError if input is undefined', () => { + expect(() => keysToCamelCase(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/keysToSnakeCase.test.ts b/functionsUnittests/objectFunctions/keysToSnakeCase.test.ts index 8741a322..6b605769 100644 --- a/functionsUnittests/objectFunctions/keysToSnakeCase.test.ts +++ b/functionsUnittests/objectFunctions/keysToSnakeCase.test.ts @@ -1,7 +1,7 @@ import { keysToSnakeCase } from '../../objectFunctions/keysToSnakeCase'; describe('keysToSnakeCase', () => { - // Test case 1: Convert keys of a simple object + // Test case 1: Convert keys of a simple object to snake_case it('1. should convert keys of a simple object to snake_case', () => { const obj = { firstName: 'John', lastName: 'Doe' }; const result = keysToSnakeCase(obj); @@ -9,7 +9,7 @@ describe('keysToSnakeCase', () => { expect(result).toEqual(expected); }); - // Test case 2: Convert keys of a nested object + // Test case 2: Convert keys of a nested object to snake_case it('2. should convert keys of a nested object to snake_case', () => { const obj = { userInfo: { firstName: 'John', lastName: 'Doe' } }; const result = keysToSnakeCase(obj); @@ -17,7 +17,7 @@ describe('keysToSnakeCase', () => { expect(result).toEqual(expected); }); - // Test case 3: Convert keys of an array of objects + // Test case 3: Convert keys of an array of objects to snake_case it('3. should convert keys of an array of objects to snake_case', () => { const arr = [{ firstName: 'John' }, { lastName: 'Doe' }]; const result = keysToSnakeCase(arr); @@ -25,51 +25,52 @@ describe('keysToSnakeCase', () => { expect(result).toEqual(expected); }); - // Test case 4: Handle non-object input (number) - it('4. should return the input if it is a number', () => { - const input = 42; - const result = keysToSnakeCase(input); - const expected = 42; - expect(result).toBe(expected); + // Test case 4: Convert keys of an object with various data types to snake_case + it('4. should convert keys of an object with various data types to snake_case', () => { + const obj = { firstName: 'John', ageYears: 30, isActive: true, addressInfo: { streetName: 'Main St' } }; + const result = keysToSnakeCase(obj); + const expected = { first_name: 'John', age_years: 30, is_active: true, address_info: { street_name: 'Main St' } }; + expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (string) - it('5. should return the input if it is a string', () => { - const input = 'string'; - const result = keysToSnakeCase(input); - const expected = 'string'; - expect(result).toBe(expected); + // Test case 5: Convert keys of an object with nested arrays to snake_case + it('5. should convert keys of an object with nested arrays to snake_case', () => { + const obj = { userList: [{ firstName: 'John' }, { lastName: 'Doe' }] }; + const result = keysToSnakeCase(obj); + const expected = { user_list: [{ first_name: 'John' }, { last_name: 'Doe' }] }; + expect(result).toEqual(expected); }); - // Test case 6: Handle non-object input (boolean) - it('6. should return the input if it is a boolean', () => { - const input = true; - const result = keysToSnakeCase(input); - const expected = true; - expect(result).toBe(expected); + // Test case 6: Handle keys that are numbers + it('6. should handle keys that are numbers', () => { + const obj = { 1: 'one', 2: 'two' }; + const result = keysToSnakeCase(obj); + const expected = { 1: 'one', 2: 'two' }; + expect(result).toEqual(expected); }); - // Test case 7: Handle non-object input (null) - it('7. should return the input if it is null', () => { - const input = null; - const result = keysToSnakeCase(input); - const expected = null; - expect(result).toBe(expected); + // Test case 7: Handle non-object input (number) + it('7. should throw a TypeError if input is a number', () => { + expect(() => keysToSnakeCase(42 as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should return the input if it is undefined', () => { - const input = undefined; - const result = keysToSnakeCase(input); - const expected = undefined; - expect(result).toBe(expected); + // Test case 8: Handle non-object input (string) + it('8. should throw a TypeError if input is a string', () => { + expect(() => keysToSnakeCase('string' as any)).toThrow(TypeError); }); - // Test case 9: Convert keys with mixed cases - it('9. should convert keys with mixed cases to snake_case', () => { - const obj = { firstName: 'John', LastName: 'Doe' }; - const result = keysToSnakeCase(obj); - const expected = { first_name: 'John', last_name: 'Doe' }; - expect(result).toEqual(expected); + // Test case 9: Handle non-object input (boolean) + it('9. should throw a TypeError if input is a boolean', () => { + expect(() => keysToSnakeCase(true as any)).toThrow(TypeError); + }); + + // Test case 10: Handle non-object input (null) + it('10. should throw a TypeError if input is null', () => { + expect(() => keysToSnakeCase(null as any)).toThrow(TypeError); + }); + + // Test case 11: Handle non-object input (undefined) + it('11. should throw a TypeError if input is undefined', () => { + expect(() => keysToSnakeCase(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/objectToQueryString.test.ts b/functionsUnittests/objectFunctions/objectToQueryString.test.ts index 579c334f..5297a9b4 100644 --- a/functionsUnittests/objectFunctions/objectToQueryString.test.ts +++ b/functionsUnittests/objectFunctions/objectToQueryString.test.ts @@ -1,60 +1,76 @@ import { objectToQueryString } from '../../objectFunctions/objectToQueryString'; describe('objectToQueryString', () => { - // Test case 1: Serialize a simple object - it('1. should serialize a simple object', () => { - const obj = { name: 'John', age: 30 }; + // Test case 1: Convert a simple object to a query string + it('1. should convert a simple object to a query string', () => { + const obj = { name: 'John Doe', age: 30 }; const result = objectToQueryString(obj); - const expected = '?name=John&age=30'; + const expected = 'name=John%20Doe&age=30'; expect(result).toBe(expected); }); - // Test case 2: Serialize an object with special characters - it('2. should serialize an object with special characters', () => { - const obj = { name: 'John Doe', city: 'New York' }; + // Test case 2: Convert an object with special characters to a query string + it('2. should convert an object with special characters to a query string', () => { + const obj = { name: 'John Doe', city: 'New York', 'special&char': 'value' }; const result = objectToQueryString(obj); - const expected = '?name=John%20Doe&city=New%20York'; + const expected = 'name=John%20Doe&city=New%20York&special%26char=value'; expect(result).toBe(expected); }); - // Test case 3: Serialize an object with array values - it('3. should serialize an object with array values', () => { - const obj = { name: 'John', hobbies: ['reading', 'swimming'] }; + // Test case 3: Convert an object with various data types to a query string + it('3. should convert an object with various data types to a query string', () => { + const obj = { name: 'John Doe', age: 30, active: true, nullValue: null, undefinedValue: undefined }; const result = objectToQueryString(obj); - const expected = '?name=John&hobbies=reading%2Cswimming'; + const expected = 'name=John%20Doe&age=30&active=true&nullValue=null&undefinedValue=undefined'; expect(result).toBe(expected); }); - // Test case 4: Serialize an empty object - it('4. should serialize an empty object', () => { + // Test case 4: Convert an object with nested objects to a query string + it('4. should convert an object with nested objects to a query string', () => { + const obj = { user: { name: 'John Doe', age: 30 }, active: true }; + const result = objectToQueryString(obj); + const expected = 'user=%5Bobject%20Object%5D&active=true'; + expect(result).toBe(expected); + }); + + // Test case 5: Convert an object with arrays to a query string + it('5. should convert an object with arrays to a query string', () => { + const obj = { names: ['John', 'Doe'], ages: [30, 25] }; + const result = objectToQueryString(obj); + const expected = 'names=John%2CDoe&ages=30%2C25'; + expect(result).toBe(expected); + }); + + // Test case 6: Handle empty object + it('6. should return an empty string if the input object is empty', () => { const obj = {}; const result = objectToQueryString(obj); - const expected = '?'; + const expected = ''; expect(result).toBe(expected); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 7: Handle non-object input (number) + it('7. should throw a TypeError if input is a number', () => { expect(() => objectToQueryString(42 as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 8: Handle non-object input (string) + it('8. should throw a TypeError if input is a string', () => { expect(() => objectToQueryString('string' as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 9: Handle non-object input (boolean) + it('9. should throw a TypeError if input is a boolean', () => { expect(() => objectToQueryString(true as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 10: Handle non-object input (null) + it('10. should throw a TypeError if input is null', () => { expect(() => objectToQueryString(null as any)).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 11: Handle non-object input (undefined) + it('11. should throw a TypeError if input is undefined', () => { expect(() => objectToQueryString(undefined as any)).toThrow(TypeError); }); -}); +}); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/omitKeys.test.ts b/functionsUnittests/objectFunctions/omitKeys.test.ts index 51a8ad35..eaaf7e87 100644 --- a/functionsUnittests/objectFunctions/omitKeys.test.ts +++ b/functionsUnittests/objectFunctions/omitKeys.test.ts @@ -1,92 +1,84 @@ import { omitKeys } from '../../objectFunctions/omitKeys'; describe('omitKeys', () => { - // Test case 1: Omit a single key from the object - it('1. should omit a single key from the object', () => { + // Test case 1: Omit specified keys from a simple object + it('1. should omit specified keys from a simple object', () => { const obj = { a: 1, b: 2, c: 3 }; - const result = omitKeys(obj, ['b']); - const expected = { a: 1, c: 3 }; - expect(result).toEqual(expected); - }); - - // Test case 2: Omit multiple keys from the object - it('2. should omit multiple keys from the object', () => { - const obj = { a: 1, b: 2, c: 3, d: 4 }; - const result = omitKeys(obj, ['b', 'd']); - const expected = { a: 1, c: 3 }; + const result = omitKeys(obj, ['b', 'c']); + const expected = { a: 1 }; expect(result).toEqual(expected); }); - // Test case 3: Return the same object if no keys are omitted - it('3. should return the same object if no keys are omitted', () => { + // Test case 2: Omit keys that do not exist in the object + it('2. should return the original object if keys to omit do not exist', () => { const obj = { a: 1, b: 2, c: 3 }; - const result = omitKeys(obj, []); - const expected = obj; + const result = omitKeys(obj, ['d', 'e'] as any); + const expected = { a: 1, b: 2, c: 3 }; expect(result).toEqual(expected); }); - // Test case 4: Return an empty object if all keys are omitted - it('4. should return an empty object if all keys are omitted', () => { - const obj = { a: 1, b: 2 }; - const result = omitKeys(obj, ['a', 'b']); - const expected = {}; + // Test case 3: Omit keys from a nested object + it('3. should omit keys from a nested object', () => { + const obj = { a: 1, b: { x: 2, y: 3 }, c: 3 }; + const result = omitKeys(obj, ['b']); + const expected = { a: 1, c: 3 }; expect(result).toEqual(expected); }); - // Test case 5: Handle non-existent keys gracefully - it('5. should handle non-existent keys gracefully', () => { - const obj = { a: 1, b: 2 }; - const result = omitKeys(obj, ['c'] as unknown as (keyof typeof obj)[]); - const expected = obj; + // Test case 4: Omit keys from an object with various data types + it('4. should omit keys from an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = omitKeys(obj, ['b', 'd', 'f']); + const expected = { a: 1, c: true, e: undefined, g: { h: 4 } }; expect(result).toEqual(expected); }); - // Test case 6: Handle an empty object - it('6. should handle an empty object', () => { - const obj = {}; - const result = omitKeys(obj, ['a'] as unknown as (keyof typeof obj)[]); + // Test case 5: Omit all keys from an object + it('5. should return an empty object if all keys are omitted', () => { + const obj = { a: 1, b: 2, c: 3 }; + const result = omitKeys(obj, ['a', 'b', 'c']); const expected = {}; expect(result).toEqual(expected); }); - // Test case 7: Handle objects with different types of values - it('7. should handle objects with different types of values', () => { - const obj = { a: 1, b: 'string', c: true, d: null, e: undefined }; - const result = omitKeys(obj, ['b', 'd']); - const expected = { a: 1, c: true, e: undefined }; + // Test case 6: Handle empty object + it('6. should return an empty object if the input object is empty', () => { + const obj = {}; + const result = omitKeys(obj, ['a', 'b'] as any); + const expected = {}; expect(result).toEqual(expected); }); - // Test case 8: Handle nested objects - it('8. should handle nested objects', () => { - const obj = { a: { x: 1 }, b: { y: 2 }, c: 3 }; - const result = omitKeys(obj, ['a', 'b']); - const expected = { c: 3 }; + // Test case 7: Handle empty keysToOmit array + it('7. should return the original object if keysToOmit is empty', () => { + const obj = { a: 1, b: 2, c: 3 }; + const result = omitKeys(obj, []); + const expected = { a: 1, b: 2, c: 3 }; expect(result).toEqual(expected); }); - // Test case 9: Handle non-object input (number) - it('9. should throw a TypeError if input is a number', () => { + // Test case 8: Handle non-object input (number) + it('8. should throw a TypeError if input is a number', () => { expect(() => omitKeys(42 as any, ['a'])).toThrow(TypeError); }); - // Test case 10: Handle non-object input (string) - it('10. should throw a TypeError if input is a string', () => { + // Test case 9: Handle non-object input (string) + it('9. should throw a TypeError if input is a string', () => { expect(() => omitKeys('string' as any, ['a'])).toThrow(TypeError); }); - // Test case 11: Handle non-object input (boolean) - it('11. should throw a TypeError if input is a boolean', () => { + // Test case 10: Handle non-object input (boolean) + it('10. should throw a TypeError if input is a boolean', () => { expect(() => omitKeys(true as any, ['a'])).toThrow(TypeError); }); - // Test case 12: Handle non-object input (null) - it('12. should throw a TypeError if input is null', () => { + // Test case 11: Handle non-object input (null) + it('11. should throw a TypeError if input is null', () => { expect(() => omitKeys(null as any, ['a'])).toThrow(TypeError); }); - // Test case 13: Handle non-object input (undefined) - it('13. should throw a TypeError if input is undefined', () => { + // Test case 12: Handle non-object input (undefined) + it('12. should throw a TypeError if input is undefined', () => { expect(() => omitKeys(undefined as any, ['a'])).toThrow(TypeError); }); -}); +}); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/pickKeys.test.ts b/functionsUnittests/objectFunctions/pickKeys.test.ts index 4060bb01..dcacda5e 100644 --- a/functionsUnittests/objectFunctions/pickKeys.test.ts +++ b/functionsUnittests/objectFunctions/pickKeys.test.ts @@ -1,59 +1,59 @@ import { pickKeys } from '../../objectFunctions/pickKeys'; describe('pickKeys', () => { - // Test case 1: Pick a single key from the object - it('1. should pick a single key from the object', () => { + // Test case 1: Pick specified keys from a simple object + it('1. should pick specified keys from a simple object', () => { const obj = { a: 1, b: 2, c: 3 }; - const result = pickKeys(obj, ['b']); - const expected = { b: 2 }; + const result = pickKeys(obj, ['b', 'c']); + const expected = { b: 2, c: 3 }; expect(result).toEqual(expected); }); - // Test case 2: Pick multiple keys from the object - it('2. should pick multiple keys from the object', () => { - const obj = { a: 1, b: 2, c: 3, d: 4 }; - const result = pickKeys(obj, ['b', 'd']); - const expected = { b: 2, d: 4 }; + // Test case 2: Pick keys that do not exist in the object + it('2. should return an empty object if keys to pick do not exist', () => { + const obj = { a: 1, b: 2, c: 3 }; + const result = pickKeys(obj, ['d', 'e'] as any); + const expected = {}; expect(result).toEqual(expected); }); - // Test case 3: Return an empty object if no keys are picked - it('3. should return an empty object if no keys are picked', () => { - const obj = { a: 1, b: 2, c: 3 }; - const result = pickKeys(obj, []); - const expected = {}; + // Test case 3: Pick keys from a nested object + it('3. should pick keys from a nested object', () => { + const obj = { a: 1, b: { x: 2, y: 3 }, c: 3 }; + const result = pickKeys(obj, ['b']); + const expected = { b: { x: 2, y: 3 } }; expect(result).toEqual(expected); }); - // Test case 4: Handle non-existent keys gracefully - it('4. should handle non-existent keys gracefully', () => { - const obj = { a: 1, b: 2 }; - const result = pickKeys(obj, ['c'] as unknown as (keyof typeof obj)[]); - const expected = {}; + // Test case 4: Pick keys from an object with various data types + it('4. should pick keys from an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 } }; + const result = pickKeys(obj, ['b', 'd', 'f']); + const expected = { b: 'string', d: null, f: [1, 2, 3] }; expect(result).toEqual(expected); }); - // Test case 5: Handle an empty object - it('5. should handle an empty object', () => { - const obj = {}; - const result = pickKeys(obj, ['a'] as (keyof typeof obj)[]); - const expected = {}; + // Test case 5: Pick all keys from an object + it('5. should return the original object if all keys are picked', () => { + const obj = { a: 1, b: 2, c: 3 }; + const result = pickKeys(obj, ['a', 'b', 'c']); + const expected = { a: 1, b: 2, c: 3 }; expect(result).toEqual(expected); }); - // Test case 6: Handle objects with different types of values - it('6. should handle objects with different types of values', () => { - const obj = { a: 1, b: 'string', c: true, d: null, e: undefined }; - const result = pickKeys(obj, ['b', 'd']); - const expected = { b: 'string', d: null }; + // Test case 6: Handle empty object + it('6. should return an empty object if the input object is empty', () => { + const obj = {}; + const result = pickKeys(obj, ['a', 'b'] as any); + const expected = {}; expect(result).toEqual(expected); }); - // Test case 7: Handle nested objects - it('7. should handle nested objects', () => { - const obj = { a: { x: 1 }, b: { y: 2 }, c: 3 }; - const result = pickKeys(obj, ['a', 'b']); - const expected = { a: { x: 1 }, b: { y: 2 } }; + // Test case 7: Handle empty keysToPick array + it('7. should return an empty object if keysToPick is empty', () => { + const obj = { a: 1, b: 2, c: 3 }; + const result = pickKeys(obj, []); + const expected = {}; expect(result).toEqual(expected); }); diff --git a/functionsUnittests/objectFunctions/queryStringToObject.test.ts b/functionsUnittests/objectFunctions/queryStringToObject.test.ts index 4c08d0e8..9b27dffe 100644 --- a/functionsUnittests/objectFunctions/queryStringToObject.test.ts +++ b/functionsUnittests/objectFunctions/queryStringToObject.test.ts @@ -1,52 +1,60 @@ import { queryStringToObject } from '../../objectFunctions/queryStringToObject'; describe('queryStringToObject', () => { - // Test case 1: Convert a simple query string - it('1. should convert a simple query string', () => { - const query = 'name=John&age=30'; - const result = queryStringToObject(query); - const expected = { name: 'John', age: '30' }; + // Test case 1: Convert a simple query string to an object + it('1. should convert a simple query string to an object', () => { + const queryString = 'name=John%20Doe&age=30'; + const result = queryStringToObject(queryString); + const expected = { name: 'John Doe', age: '30' }; expect(result).toEqual(expected); }); - // Test case 2: Convert a query string with special characters - it('2. should convert a query string with special characters', () => { - const query = 'name=John%20Doe&city=New%20York'; - const result = queryStringToObject(query); - const expected = { name: 'John Doe', city: 'New York' }; + // Test case 2: Convert a query string with special characters to an object + it('2. should convert a query string with special characters to an object', () => { + const queryString = 'name=John%20Doe&city=New%20York&special%26char=value'; + const result = queryStringToObject(queryString); + const expected = { name: 'John Doe', city: 'New York', 'special&char': 'value' }; expect(result).toEqual(expected); }); - // Test case 3: Convert an empty query string - it('3. should convert an empty query string', () => { - const query = ''; - const result = queryStringToObject(query); + // Test case 3: Convert a query string with various data types to an object + it('3. should convert a query string with various data types to an object', () => { + const queryString = 'name=John%20Doe&age=30&active=true&nullValue=null&undefinedValue=undefined'; + const result = queryStringToObject(queryString); + const expected = { name: 'John Doe', age: '30', active: 'true', nullValue: 'null', undefinedValue: 'undefined' }; + expect(result).toEqual(expected); + }); + + // Test case 4: Convert an empty query string to an object + it('4. should return an empty object for an empty query string', () => { + const queryString = ''; + const result = queryStringToObject(queryString); const expected = {}; expect(result).toEqual(expected); }); - // Test case 4: Handle non-string input (number) - it('4. should throw a TypeError if input is a number', () => { + // Test case 5: Handle non-string input (number) + it('5. should throw a TypeError if input is a number', () => { expect(() => queryStringToObject(42 as any)).toThrow(TypeError); }); - // Test case 5: Handle non-string input (object) - it('5. should throw a TypeError if input is an object', () => { + // Test case 6: Handle non-string input (object) + it('6. should throw a TypeError if input is an object', () => { expect(() => queryStringToObject({} as any)).toThrow(TypeError); }); - // Test case 6: Handle non-string input (boolean) - it('6. should throw a TypeError if input is a boolean', () => { + // Test case 7: Handle non-string input (boolean) + it('7. should throw a TypeError if input is a boolean', () => { expect(() => queryStringToObject(true as any)).toThrow(TypeError); }); - // Test case 7: Handle non-string input (null) - it('7. should throw a TypeError if input is null', () => { + // Test case 8: Handle non-string input (null) + it('8. should throw a TypeError if input is null', () => { expect(() => queryStringToObject(null as any)).toThrow(TypeError); }); - // Test case 8: Handle non-string input (undefined) - it('8. should throw a TypeError if input is undefined', () => { + // Test case 9: Handle non-string input (undefined) + it('9. should throw a TypeError if input is undefined', () => { expect(() => queryStringToObject(undefined as any)).toThrow(TypeError); }); -}); +}); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/removeEmptyValues.test.ts b/functionsUnittests/objectFunctions/removeEmptyValues.test.ts index ce63415f..1e5d1dc9 100644 --- a/functionsUnittests/objectFunctions/removeEmptyValues.test.ts +++ b/functionsUnittests/objectFunctions/removeEmptyValues.test.ts @@ -3,58 +3,66 @@ import { removeEmptyValues } from '../../objectFunctions/removeEmptyValues'; describe('removeEmptyValues', () => { // Test case 1: Remove empty values from a simple object it('1. should remove empty values from a simple object', () => { - const obj = { a: 1, b: null, c: undefined, d: 2 }; + const obj = { a: 1, b: null, c: '', d: undefined, e: 'value' }; const result = removeEmptyValues(obj); - const expected = { a: 1, d: 2 }; + const expected = { a: 1, e: 'value' }; expect(result).toEqual(expected); }); // Test case 2: Remove empty values from a nested object it('2. should remove empty values from a nested object', () => { - const obj = { a: { b: null, c: 2 }, d: undefined, e: 3 }; + const obj = { a: 1, b: { x: null, y: 2 }, c: '', d: undefined, e: 'value' }; const result = removeEmptyValues(obj); - const expected = { a: { c: 2 }, e: 3 }; + const expected = { a: 1, b: { y: 2 }, e: 'value' }; expect(result).toEqual(expected); }); - // Test case 3: Handle an object with no empty values - it('3. should handle an object with no empty values', () => { - const obj = { a: 1, b: 2, c: 3 }; + // Test case 3: Remove empty values from an object with various data types + it('3. should remove empty values from an object with various data types', () => { + const obj = { a: 1, b: 'string', c: true, d: null, e: undefined, f: [1, 2, 3], g: { h: 4 }, i: '' }; const result = removeEmptyValues(obj); - const expected = { a: 1, b: 2, c: 3 }; + const expected = { a: 1, b: 'string', c: true, f: [1, 2, 3], g: { h: 4 } }; expect(result).toEqual(expected); }); - // Test case 4: Handle an empty object - it('4. should handle an empty object', () => { + // Test case 4: Remove empty values from an object with nested arrays + it('4. should remove empty values from an object with nested arrays', () => { + const obj = { a: 1, b: [null, 2, '', undefined, 'value'] }; + const result = removeEmptyValues(obj); + const expected = { a: 1, b: [2, 'value'] }; + expect(result).toEqual(expected); + }); + + // Test case 5: Handle empty object + it('5. should return an empty object if the input object is empty', () => { const obj = {}; const result = removeEmptyValues(obj); const expected = {}; expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { expect(() => removeEmptyValues(42 as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { expect(() => removeEmptyValues('string' as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { expect(() => removeEmptyValues(true as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { expect(() => removeEmptyValues(null as any)).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { expect(() => removeEmptyValues(undefined as any)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/setNestedValue.test.ts b/functionsUnittests/objectFunctions/setNestedValue.test.ts index 903501d0..11114747 100644 --- a/functionsUnittests/objectFunctions/setNestedValue.test.ts +++ b/functionsUnittests/objectFunctions/setNestedValue.test.ts @@ -1,68 +1,68 @@ import { setNestedValue } from '../../objectFunctions/setNestedValue'; describe('setNestedValue', () => { - // Test case 1: Set a nested value in an object - it('1. should set a nested value in an object', () => { - const obj = { a: { b: { c: 42 } } }; - setNestedValue(obj, 'a.b.c', 100); - const expected = { a: { b: { c: 100 } } }; + // Test case 1: Set a nested value in a simple object + it('1. should set a nested value in a simple object', () => { + const obj = { a: { b: { c: 3 } } }; + setNestedValue(obj, 'a.b.c', 4); + const expected = { a: { b: { c: 4 } } }; expect(obj).toEqual(expected); }); - // Test case 2: Create nested structure if it does not exist - it('2. should create nested structure if it does not exist', () => { - const obj = {}; - setNestedValue(obj, 'a.b.c', 100); - const expected = { a: { b: { c: 100 } } }; + // Test case 2: Set a nested value in a nested object + it('2. should set a nested value in a nested object', () => { + const obj = { a: { b: { c: { d: 4 } } } }; + setNestedValue(obj, 'a.b.c.d', 5); + const expected = { a: { b: { c: { d: 5 } } } }; expect(obj).toEqual(expected); }); - // Test case 3: Overwrite existing value - it('3. should overwrite existing value', () => { - const obj = { a: { b: { c: 42 } } }; - setNestedValue(obj, 'a.b.c', 100); - const expected = { a: { b: { c: 100 } } }; + // Test case 3: Set a nested value in an array + it('3. should set a nested value in an array', () => { + const obj = { a: [{ b: 2 }, { c: 3 }] }; + setNestedValue(obj, 'a.1.c', 4); + const expected = { a: [{ b: 2 }, { c: 4 }] }; expect(obj).toEqual(expected); }); - // Test case 4: Handle non-object input (number) - it('4. should throw a TypeError if input is a number', () => { - expect(() => setNestedValue(42 as any, 'a.b.c', 100)).toThrow(TypeError); + // Test case 4: Set a nested value in an object with various data types + it('4. should set a nested value in an object with various data types', () => { + const obj = { a: { b: { c: true, d: 'string', e: null, f: undefined, g: [1, 2, 3], h: { i: 4 } } } }; + setNestedValue(obj, 'a.b.h.i', 5); + const expected = { a: { b: { c: true, d: 'string', e: null, f: undefined, g: [1, 2, 3], h: { i: 5 } } } }; + expect(obj).toEqual(expected); }); - // Test case 5: Handle non-object input (string) - it('5. should throw a TypeError if input is a string', () => { - expect(() => setNestedValue('string' as any, 'a.b.c', 100)).toThrow(TypeError); + // Test case 5: Create a nested path if it does not exist + it('5. should create a nested path if it does not exist', () => { + const obj = { a: 1 }; + setNestedValue(obj, 'b.c.d', 2); + const expected = { a: 1, b: { c: { d: 2 } } }; + expect(obj).toEqual(expected); }); - // Test case 6: Handle non-object input (boolean) - it('6. should throw a TypeError if input is a boolean', () => { - expect(() => setNestedValue(true as any, 'a.b.c', 100)).toThrow(TypeError); + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { + expect(() => setNestedValue(42 as any, 'a.b.c', 4)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (null) - it('7. should throw a TypeError if input is null', () => { - expect(() => setNestedValue(null as any, 'a.b.c', 100)).toThrow(TypeError); + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { + expect(() => setNestedValue('string' as any, 'a.b.c', 4)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (undefined) - it('8. should throw a TypeError if input is undefined', () => { - expect(() => setNestedValue(undefined as any, 'a.b.c', 100)).toThrow(TypeError); + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { + expect(() => setNestedValue(true as any, 'a.b.c', 4)).toThrow(TypeError); }); - // Test case 9: Set a value in an array within an object - it('9. should set a value in an array within an object', () => { - const obj = { a: { b: [1, 2, 3] } }; - setNestedValue(obj, 'a.b.1', 100); - const expected = { a: { b: [1, 100, 3] } }; - expect(obj).toEqual(expected); + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { + expect(() => setNestedValue(null as any, 'a.b.c', 4)).toThrow(TypeError); }); - // Test case 10: Create nested structure with arrays if it does not exist - it('10. should create nested structure with arrays if it does not exist', () => { - const obj = {}; - setNestedValue(obj, 'a.b.0.c', 100); - const expected = { a: { b: [{ c: 100 }] } }; - expect(obj).toEqual(expected); + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { + expect(() => setNestedValue(undefined as any, 'a.b.c', 4)).toThrow(TypeError); }); }); diff --git a/functionsUnittests/objectFunctions/shallowEqual.test.ts b/functionsUnittests/objectFunctions/shallowEqual.test.ts index be69e018..9ae91242 100644 --- a/functionsUnittests/objectFunctions/shallowEqual.test.ts +++ b/functionsUnittests/objectFunctions/shallowEqual.test.ts @@ -1,115 +1,101 @@ import { shallowEqual } from '../../objectFunctions/shallowEqual'; describe('shallowEqual', () => { - // Test case 1: Compare two equal objects - it('1. should return true for two equal objects', () => { + // Test case 1: Compare two identical simple objects + it('1. should return true for two identical simple objects', () => { const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 2 }; const result = shallowEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + expect(result).toBe(true); }); - // Test case 2: Compare two different objects - it('2. should return false for two different objects', () => { + // Test case 2: Compare two different simple objects + it('2. should return false for two different simple objects', () => { const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 3 }; const result = shallowEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 3: Compare objects with different keys - it('3. should return false for objects with different keys', () => { + // Test case 3: Compare two objects with different keys + it('3. should return false for two objects with different keys', () => { const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, c: 2 }; const result = shallowEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 4: Compare objects with different number of keys - it('4. should return false for objects with different number of keys', () => { - const obj1 = { a: 1, b: 2 }; - const obj2 = { a: 1 }; - const result = shallowEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); - }); - - // Test case 5: Handle empty objects - it('5. should return true for two empty objects', () => { - const obj1 = {}; - const obj2 = {}; + // Test case 4: Compare two nested objects + it('4. should return false for two nested objects', () => { + const obj1 = { a: 1, b: { x: 2, y: 3 } }; + const obj2 = { a: 1, b: { x: 2, y: 3 } }; const result = shallowEqual(obj1, obj2); - const expected = true; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 6: Handle empty object and non-empty object - it('6. should return false for an empty object and a non-empty object', () => { - const obj1 = {}; - const obj2 = { a: 1 }; + // Test case 5: Compare two objects with various data types + it('5. should return true for two objects with various data types that are equal', () => { + const obj1 = { a: 1, b: 'string', c: true, d: null, e: undefined }; + const obj2 = { a: 1, b: 'string', c: true, d: null, e: undefined }; const result = shallowEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(true); }); - it('7. should return false for a non-empty object and an empty object', () => { - const obj1 = { a: 1 }; - const obj2 = {}; + // Test case 6: Compare two objects with arrays + it('6. should return false for two objects with arrays', () => { + const obj1 = { a: [1, 2, 3], b: 2 }; + const obj2 = { a: [1, 2, 3], b: 2 }; const result = shallowEqual(obj1, obj2); - const expected = false; - expect(result).toBe(expected); + expect(result).toBe(false); }); - // Test case 8: Handle non-object input (number) - it('8. should throw a TypeError if the first input is a number', () => { + // Test case 7: Handle non-object input (number) + it('7. should throw a TypeError if the first input is a number', () => { expect(() => shallowEqual(42 as any, { a: 1 })).toThrow(TypeError); }); - // Test case 9: Handle non-object input (string) - it('9. should throw a TypeError if the first input is a string', () => { + // Test case 8: Handle non-object input (string) + it('8. should throw a TypeError if the first input is a string', () => { expect(() => shallowEqual('string' as any, { a: 1 })).toThrow(TypeError); }); - // Test case 10: Handle non-object input (boolean) - it('10. should throw a TypeError if the first input is a boolean', () => { + // Test case 9: Handle non-object input (boolean) + it('9. should throw a TypeError if the first input is a boolean', () => { expect(() => shallowEqual(true as any, { a: 1 })).toThrow(TypeError); }); - // Test case 11: Handle non-object input (null) - it('11. should throw a TypeError if the first input is null', () => { + // Test case 10: Handle non-object input (null) + it('10. should throw a TypeError if the first input is null', () => { expect(() => shallowEqual(null as any, { a: 1 })).toThrow(TypeError); }); - // Test case 12: Handle non-object input (undefined) - it('12. should throw a TypeError if the first input is undefined', () => { + // Test case 11: Handle non-object input (undefined) + it('11. should throw a TypeError if the first input is undefined', () => { expect(() => shallowEqual(undefined as any, { a: 1 })).toThrow(TypeError); }); - // Test case 13: Handle non-object input for the second parameter (number) - it('13. should throw a TypeError if the second input is a number', () => { + // Test case 12: Handle non-object input for the second parameter (number) + it('12. should throw a TypeError if the second input is a number', () => { expect(() => shallowEqual({ a: 1 }, 42 as any)).toThrow(TypeError); }); - // Test case 14: Handle non-object input for the second parameter (string) - it('14. should throw a TypeError if the second input is a string', () => { + // Test case 13: Handle non-object input for the second parameter (string) + it('13. should throw a TypeError if the second input is a string', () => { expect(() => shallowEqual({ a: 1 }, 'string' as any)).toThrow(TypeError); }); - // Test case 15: Handle non-object input for the second parameter (boolean) - it('15. should throw a TypeError if the second input is a boolean', () => { + // Test case 14: Handle non-object input for the second parameter (boolean) + it('14. should throw a TypeError if the second input is a boolean', () => { expect(() => shallowEqual({ a: 1 }, true as any)).toThrow(TypeError); }); - // Test case 16: Handle non-object input for the second parameter (null) - it('16. should throw a TypeError if the second input is null', () => { + // Test case 15: Handle non-object input for the second parameter (null) + it('15. should throw a TypeError if the second input is null', () => { expect(() => shallowEqual({ a: 1 }, null as any)).toThrow(TypeError); }); - // Test case 17: Handle non-object input for the second parameter (undefined) - it('17. should throw a TypeError if the second input is undefined', () => { + // Test case 16: Handle non-object input for the second parameter (undefined) + it('16. should throw a TypeError if the second input is undefined', () => { expect(() => shallowEqual({ a: 1 }, undefined as any)).toThrow(TypeError); }); }); \ No newline at end of file diff --git a/functionsUnittests/objectFunctions/unflattenObject.test.ts b/functionsUnittests/objectFunctions/unflattenObject.test.ts index ce84ed14..11279fd9 100644 --- a/functionsUnittests/objectFunctions/unflattenObject.test.ts +++ b/functionsUnittests/objectFunctions/unflattenObject.test.ts @@ -3,58 +3,66 @@ import { unflattenObject } from '../../objectFunctions/unflattenObject'; describe('unflattenObject', () => { // Test case 1: Unflatten a simple object it('1. should unflatten a simple object', () => { - const obj = { 'a': 1, 'b': 2 }; + const obj = { 'a.b.c': 1, 'a.b.d': 2, 'e': 3 }; const result = unflattenObject(obj); - const expected = { a: 1, b: 2 }; + const expected = { a: { b: { c: 1, d: 2 } }, e: 3 }; expect(result).toEqual(expected); }); - // Test case 2: Unflatten a nested object - it('2. should unflatten a nested object', () => { - const obj = { 'a': 1, 'b.c': 2, 'b.d': 3 }; + // Test case 2: Unflatten an object with nested arrays + it('2. should unflatten an object with nested arrays', () => { + const obj = { 'a.0.b': 1, 'a.1.c': 2 }; const result = unflattenObject(obj); - const expected = { a: 1, b: { c: 2, d: 3 } }; + const expected = { a: [{ b: 1 }, { c: 2 }] }; expect(result).toEqual(expected); }); - // Test case 3: Unflatten an object with arrays - it('3. should unflatten an object with arrays', () => { - const obj = { 'a.0': 1, 'a.1': 2, 'a.2': 3, 'b.c': 4 }; + // Test case 3: Unflatten an object with various data types + it('3. should unflatten an object with various data types', () => { + const obj = { 'a.b.c': true, 'a.b.d': 'string', 'a.b.e': null, 'a.b.f': undefined, 'a.b.g': [1, 2, 3], 'a.b.h.i': 4 }; const result = unflattenObject(obj); - const expected = { a: [1, 2, 3], b: { c: 4 } }; + const expected = { a: { b: { c: true, d: 'string', e: null, f: undefined, g: [1, 2, 3], h: { i: 4 } } } }; expect(result).toEqual(expected); }); - // Test case 4: Unflatten an object with nested arrays - it('4. should unflatten an object with nested arrays', () => { - const obj = { 'a.b.0': 1, 'a.b.1': 2, 'a.b.2.c': 3 }; + // Test case 4: Unflatten an object with empty values + it('4. should unflatten an object with empty values', () => { + const obj = { 'a.b.c': '', 'a.b.d': null, 'a.b.e': undefined }; const result = unflattenObject(obj); - const expected = { a: { b: [1, 2, { c: 3 }] } }; + const expected = { a: { b: { c: '', d: null, e: undefined } } }; expect(result).toEqual(expected); }); - // Test case 5: Handle non-object input (number) - it('5. should throw a TypeError if input is a number', () => { + // Test case 5: Handle empty object + it('5. should return an empty object if the input object is empty', () => { + const obj = {}; + const result = unflattenObject(obj); + const expected = {}; + expect(result).toEqual(expected); + }); + + // Test case 6: Handle non-object input (number) + it('6. should throw a TypeError if input is a number', () => { expect(() => unflattenObject(42 as any)).toThrow(TypeError); }); - // Test case 6: Handle non-object input (string) - it('6. should throw a TypeError if input is a string', () => { + // Test case 7: Handle non-object input (string) + it('7. should throw a TypeError if input is a string', () => { expect(() => unflattenObject('string' as any)).toThrow(TypeError); }); - // Test case 7: Handle non-object input (boolean) - it('7. should throw a TypeError if input is a boolean', () => { + // Test case 8: Handle non-object input (boolean) + it('8. should throw a TypeError if input is a boolean', () => { expect(() => unflattenObject(true as any)).toThrow(TypeError); }); - // Test case 8: Handle non-object input (null) - it('8. should throw a TypeError if input is null', () => { + // Test case 9: Handle non-object input (null) + it('9. should throw a TypeError if input is null', () => { expect(() => unflattenObject(null as any)).toThrow(TypeError); }); - // Test case 9: Handle non-object input (undefined) - it('9. should throw a TypeError if input is undefined', () => { + // Test case 10: Handle non-object input (undefined) + it('10. should throw a TypeError if input is undefined', () => { expect(() => unflattenObject(undefined as any)).toThrow(TypeError); }); }); diff --git a/objectFunctions/getObjectDifference.ts b/objectFunctions/getObjectDifference.ts index 218d242c..ba9d933e 100644 --- a/objectFunctions/getObjectDifference.ts +++ b/objectFunctions/getObjectDifference.ts @@ -1,16 +1,34 @@ /** - * Returns the difference between two objects. + * Computes the difference between two objects. * - * @param {T} obj1 - The first object to compare. - * @param {T} obj2 - The second object to compare. - * @returns {Partial} - An object containing the properties that differ between the two objects. - * @throws {TypeError} - If either input is not an object or is null. + * This function returns an object containing the properties that are different between + * the two input objects. If a property exists in both objects but has different values, + * it will be included in the result. + * + * @param {Record} obj1 - The first object. + * @param {Record} obj2 - The second object. + * @returns {Record} - An object containing the differences. + * @throws {TypeError} - If either input is not a non-null object. + * + * @example + * const obj1 = { a: 1, b: 2, c: 3 }; + * const obj2 = { a: 1, b: 4, d: 5 }; + * const result = getObjectDifference(obj1, obj2); + * // result: { b: 4, c: undefined, d: 5 } */ -export function getObjectDifference>(obj1: T, obj2: T): Partial { +export function getObjectDifference(obj1: Record, obj2: Record): Record { if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) { throw new TypeError('Both inputs must be non-null objects'); } - return Object.fromEntries( - Object.entries(obj1).filter(([key, value]) => obj2[key] !== value) - ) as Partial; + + const diff: Record = {}; + + const allKeys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]); + allKeys.forEach(key => { + if (obj1[key] !== obj2[key]) { + diff[key] = obj2[key]; + } + }); + + return diff; } \ No newline at end of file diff --git a/objectFunctions/hasKey.ts b/objectFunctions/hasKey.ts index ec66de8e..bbc2150d 100644 --- a/objectFunctions/hasKey.ts +++ b/objectFunctions/hasKey.ts @@ -1,12 +1,19 @@ /** * Checks if an object has a specific key. * - * @param {T} obj - The object to check. - * @param {keyof T} key - The key to check for. + * This function returns true if the object has the specified key as its own property. + * + * @param {Record} obj - The object to check. + * @param {string} key - The key to check for. * @returns {boolean} - True if the object has the key, false otherwise. - * @throws {TypeError} - If the input is not an object or is null. + * @throws {TypeError} - If the input object is not a non-null object. + * + * @example + * const obj = { a: 1, b: 2 }; + * const result = hasKey(obj, 'a'); + * // result: true */ -export function hasKey>(obj: T, key: keyof T): boolean { +export function hasKey(obj: Record, key: string): boolean { if (typeof obj !== 'object' || obj === null) { throw new TypeError('Input must be a non-null object'); } diff --git a/objectFunctions/isEmptyObject.ts b/objectFunctions/isEmptyObject.ts deleted file mode 100644 index 5e3e1bfd..00000000 --- a/objectFunctions/isEmptyObject.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Checks if an object is empty. - * - * @param {Record} obj - The object to check. - * @returns {boolean} - True if the object is empty, false otherwise. - * @throws {TypeError} - If the input is not an object or is null. - */ -export function isEmptyObject(obj: Record): boolean { - if (typeof obj !== 'object' || obj === null) { - throw new TypeError('Input must be a non-null object'); - } - return Object.keys(obj).length === 0; -} \ No newline at end of file diff --git a/objectFunctions/isObjectEmpty.ts b/objectFunctions/isObjectEmpty.ts index 6a2543e6..527bc9da 100644 --- a/objectFunctions/isObjectEmpty.ts +++ b/objectFunctions/isObjectEmpty.ts @@ -1,9 +1,16 @@ /** * Checks if an object is empty. * + * This function returns true if the object has no own enumerable properties. + * * @param {Record} obj - The object to check. * @returns {boolean} - True if the object is empty, false otherwise. - * @throws {TypeError} - If the input is not an object. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = {}; + * const result = isObjectEmpty(obj); + * // result: true */ export function isObjectEmpty(obj: Record): boolean { if (typeof obj !== 'object' || obj === null) { diff --git a/objectFunctions/keysToCamelCase.ts b/objectFunctions/keysToCamelCase.ts index ac95fe2a..7dec50ea 100644 --- a/objectFunctions/keysToCamelCase.ts +++ b/objectFunctions/keysToCamelCase.ts @@ -1,9 +1,16 @@ /** * Converts the keys of an object to camelCase. * + * This function recursively converts the keys of an object or array to camelCase. + * * @param {any} obj - The object to convert. * @returns {any} - A new object with keys converted to camelCase. * @throws {TypeError} - If the input is not an object or is null. + * + * @example + * const obj = { 'first_name': 'John', 'last_name': 'Doe' }; + * const result = keysToCamelCase(obj); + * // result: { firstName: 'John', lastName: 'Doe' } */ export function keysToCamelCase(obj: any): any { if (Array.isArray(obj)) { @@ -11,10 +18,13 @@ export function keysToCamelCase(obj: any): any { } else if (obj !== null && typeof obj === 'object') { return Object.fromEntries( Object.entries(obj).map(([key, value]) => [ - key.replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '').replace('_', '')), + typeof key === 'string' ? key.replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '').replace('_', '')) : key, keysToCamelCase(value) ]) ); } + if (typeof obj !== 'object' || obj === null) { + throw new TypeError('Input must be a non-null object'); + } return obj; } \ No newline at end of file diff --git a/objectFunctions/keysToSnakeCase.ts b/objectFunctions/keysToSnakeCase.ts index e07dbbf9..280636fc 100644 --- a/objectFunctions/keysToSnakeCase.ts +++ b/objectFunctions/keysToSnakeCase.ts @@ -1,9 +1,16 @@ /** * Converts the keys of an object to snake_case. * + * This function recursively converts the keys of an object or array to snake_case. + * * @param {any} obj - The object to convert. * @returns {any} - A new object with keys converted to snake_case. * @throws {TypeError} - If the input is not an object or is null. + * + * @example + * const obj = { firstName: 'John', lastName: 'Doe' }; + * const result = keysToSnakeCase(obj); + * // result: { first_name: 'John', last_name: 'Doe' } */ export function keysToSnakeCase(obj: any): any { if (Array.isArray(obj)) { @@ -11,10 +18,13 @@ export function keysToSnakeCase(obj: any): any { } else if (obj !== null && typeof obj === 'object') { return Object.fromEntries( Object.entries(obj).map(([key, value]) => [ - key.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`), + typeof key === 'string' ? key.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) : key, keysToSnakeCase(value) ]) ); } + if (typeof obj !== 'object' || obj === null) { + throw new TypeError('Input must be a non-null object'); + } return obj; } \ No newline at end of file diff --git a/objectFunctions/objectToQueryString.ts b/objectFunctions/objectToQueryString.ts index 6609b3ec..aa9e20bf 100644 --- a/objectFunctions/objectToQueryString.ts +++ b/objectFunctions/objectToQueryString.ts @@ -1,18 +1,22 @@ /** - * Serializes an object into a query string. + * Converts an object to a query string. * - * @param {Record} obj - The object to serialize. - * @returns {string} - A query string representing the object. - * @throws {TypeError} - If the input is not an object or is null. + * This function takes an object and converts it to a URL-encoded query string. + * + * @param {Record} obj - The object to convert. + * @returns {string} - A URL-encoded query string. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = { name: 'John Doe', age: 30 }; + * const result = objectToQueryString(obj); + * // result: 'name=John%20Doe&age=30' */ export function objectToQueryString(obj: Record): string { if (typeof obj !== 'object' || obj === null) { throw new TypeError('Input must be a non-null object'); } - return '?' + Object.keys(obj) - .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`) + return Object.entries(obj) + .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) .join('&'); -} - -// Example usage: -// objectToQueryString({ name: 'John', age: 30 }); // "?name=John&age=30" \ No newline at end of file +} \ No newline at end of file diff --git a/objectFunctions/omitKeys.ts b/objectFunctions/omitKeys.ts index 554a1e33..9d8a5f65 100644 --- a/objectFunctions/omitKeys.ts +++ b/objectFunctions/omitKeys.ts @@ -1,10 +1,17 @@ /** * Omits specified keys from an object. * + * This function creates a new object by omitting the specified keys from the input object. + * * @param {T} obj - The object to omit keys from. * @param {Array} keysToOmit - The keys to omit from the object. * @returns {Partial} - A new object with the specified keys omitted. - * @throws {TypeError} - If the input is not an object or is null. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = { a: 1, b: 2, c: 3 }; + * const result = omitKeys(obj, ['b', 'c']); + * // result: { a: 1 } */ export function omitKeys>(obj: T, keysToOmit: (keyof T)[]): Partial { if (typeof obj !== 'object' || obj === null) { diff --git a/objectFunctions/pickKeys.ts b/objectFunctions/pickKeys.ts index 471f3bdd..1ba3007f 100644 --- a/objectFunctions/pickKeys.ts +++ b/objectFunctions/pickKeys.ts @@ -1,10 +1,17 @@ /** * Picks specified keys from an object. * + * This function creates a new object by picking the specified keys from the input object. + * * @param {T} obj - The object to pick keys from. * @param {Array} keysToPick - The keys to pick from the object. * @returns {Partial} - A new object with the specified keys picked. - * @throws {TypeError} - If the input is not an object or is null. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = { a: 1, b: 2, c: 3 }; + * const result = pickKeys(obj, ['b', 'c']); + * // result: { b: 2, c: 3 } */ export function pickKeys>(obj: T, keysToPick: (keyof T)[]): Partial { if (typeof obj !== 'object' || obj === null) { diff --git a/objectFunctions/queryStringToObject.ts b/objectFunctions/queryStringToObject.ts index a33ad67f..c9664268 100644 --- a/objectFunctions/queryStringToObject.ts +++ b/objectFunctions/queryStringToObject.ts @@ -1,13 +1,26 @@ /** - * Converts a query string into an object. + * Converts a query string to an object. * - * @param {string} query - The query string to convert. - * @returns {Record} - An object representing the query string. + * This function takes a URL-encoded query string and converts it to an object. + * + * @param {string} queryString - The query string to convert. + * @returns {Record} - An object representing the query string. * @throws {TypeError} - If the input is not a string. + * + * @example + * const queryString = 'name=John%20Doe&age=30'; + * const result = queryStringToObject(queryString); + * // result: { name: 'John Doe', age: '30' } */ -export function queryStringToObject(query: string): Record { - if (typeof query !== 'string') { +export function queryStringToObject(queryString: string): Record { + if (typeof queryString !== 'string') { throw new TypeError('Input must be a string'); } - return Object.fromEntries(new URLSearchParams(query).entries()); + return queryString + .split('&') + .map(param => param.split('=')) + .reduce((acc, [key, value]) => { + acc[decodeURIComponent(key)] = decodeURIComponent(value); + return acc; + }, {} as Record); } \ No newline at end of file diff --git a/objectFunctions/removeEmptyValues.ts b/objectFunctions/removeEmptyValues.ts index 76e8bd33..e3d1fa8b 100644 --- a/objectFunctions/removeEmptyValues.ts +++ b/objectFunctions/removeEmptyValues.ts @@ -1,15 +1,20 @@ /** - * Removes properties with empty values (undefined or null) from an object. + * Removes empty values from an object. * - * @param {T} obj - The object to remove empty values from. - * @returns {Partial} - A new object with empty values removed. - * @throws {TypeError} - If the input is not an object or is null. + * This function creates a new object by removing properties with empty values (null, undefined, empty string) from the input object. + * + * @param {Record} obj - The object to remove empty values from. + * @returns {Partial>} - A new object with empty values removed. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = { a: 1, b: null, c: '', d: undefined, e: 'value' }; + * const result = removeEmptyValues(obj); + * // result: { a: 1, e: 'value' } */ -export function removeEmptyValues>(obj: T): Partial { +export function removeEmptyValues(obj: Record): Partial> { if (typeof obj !== 'object' || obj === null) { throw new TypeError('Input must be a non-null object'); } - return Object.fromEntries( - Object.entries(obj).filter(([_, value]) => value !== undefined && value !== null) - ) as Partial; + return Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== null && value !== undefined && value !== '')); } \ No newline at end of file diff --git a/objectFunctions/setNestedValue.ts b/objectFunctions/setNestedValue.ts index 7ee2425f..3260b79f 100644 --- a/objectFunctions/setNestedValue.ts +++ b/objectFunctions/setNestedValue.ts @@ -1,25 +1,30 @@ /** - * Sets the value at a given path within an object. + * Sets a nested value within an object. + * + * This function sets a value at a specified path within an object. If the path does not exist, it will be created. * * @param {Record} obj - The object to set the value in. * @param {string} path - The path to the value, represented as a dot-separated string. - * @param {any} value - The value to set at the given path. - * @throws {TypeError} - If the input object is not an object or is null. + * @param {any} value - The value to set. + * @returns {void} + * @throws {TypeError} - If the input object is not a non-null object. + * + * @example + * const obj = { a: { b: { c: 3 } } }; + * setNestedValue(obj, 'a.b.c', 4); + * // obj: { a: { b: { c: 4 } } } */ export function setNestedValue(obj: Record, path: string, value: any): void { if (typeof obj !== 'object' || obj === null) { throw new TypeError('Input must be a non-null object'); } - const keys = path.split('.'); - let current: any = obj; - - keys.slice(0, -1).forEach((key) => { - if (!current[key] || typeof current[key] !== 'object') { - current[key] = {}; + let current = obj; + for (let i = 0; i < keys.length - 1; i++) { + if (typeof current[keys[i]] !== 'object' || current[keys[i]] === null) { + current[keys[i]] = {}; } - current = current[key]; - }); - + current = current[keys[i]]; + } current[keys[keys.length - 1]] = value; } \ No newline at end of file diff --git a/objectFunctions/shallowEqual.ts b/objectFunctions/shallowEqual.ts index 72f63d37..6f785122 100644 --- a/objectFunctions/shallowEqual.ts +++ b/objectFunctions/shallowEqual.ts @@ -1,10 +1,18 @@ /** * Performs a shallow comparison between two objects to determine if they are equal. * + * This function compares the own enumerable properties of two objects to determine if they are shallowly equal. + * * @param {Record} obj1 - The first object to compare. * @param {Record} obj2 - The second object to compare. * @returns {boolean} - True if the objects are shallowly equal, false otherwise. - * @throws {TypeError} - If either input is not an object or is null. + * @throws {TypeError} - If either input is not a non-null object. + * + * @example + * const obj1 = { a: 1, b: 2 }; + * const obj2 = { a: 1, b: 2 }; + * const result = shallowEqual(obj1, obj2); + * // result: true */ export function shallowEqual(obj1: Record, obj2: Record): boolean { if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) { @@ -17,4 +25,4 @@ export function shallowEqual(obj1: Record, obj2: Record obj1[key] === obj2[key]); -} +} \ No newline at end of file diff --git a/objectFunctions/unflattenObject.ts b/objectFunctions/unflattenObject.ts index 0139857a..ab748e77 100644 --- a/objectFunctions/unflattenObject.ts +++ b/objectFunctions/unflattenObject.ts @@ -1,19 +1,39 @@ /** - * Unflattens a flattened object. + * Converts a flattened object into a nested object. * - * @param {Record} obj - The flattened object to unflatten. - * @returns {Record} - The unflattened object. - * @throws {TypeError} - If the input is not an object or is null. + * This function takes an object with flattened keys (dot-separated) and converts it into a nested object. + * + * @param {Record} obj - The flattened object to convert. + * @returns {Record} - A nested object. + * @throws {TypeError} - If the input is not a non-null object. + * + * @example + * const obj = { 'a.b.c': 1, 'a.b.d': 2, 'e': 3 }; + * const result = unflattenObject(obj); + * // result: { a: { b: { c: 1, d: 2 } }, e: 3 } */ export function unflattenObject(obj: Record): Record { if (typeof obj !== 'object' || obj === null) { throw new TypeError('Input must be a non-null object'); } - return Object.keys(obj).reduce((acc, key) => { + + const result: Record = {}; + + for (const [key, value] of Object.entries(obj)) { const keys = key.split('.'); - keys.reduce((nested, k, i) => { - return (nested[k] = i === keys.length - 1 ? obj[key] : nested[k] || {}); - }, acc); - return acc; - }, {} as Record); -} \ No newline at end of file + let current = result; + + for (let i = 0; i < keys.length; i++) { + if (i === keys.length - 1) { + current[keys[i]] = value; + } else { + if (!current[keys[i]]) { + current[keys[i]] = {}; + } + current = current[keys[i]]; + } + } + } + + return result; +}