diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98d224f1..8e445adf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,7 +38,7 @@ jobs: run: npm install - name: Run tests - run: npm test + run: npm test:github" - name: Upload Allure report if: ${{ github.event.inputs.upload_report == 'true' }} diff --git a/functionsUnittests/objectFunctions/flattenObject.test.ts b/functionsUnittests/objectFunctions/flattenObject.test.ts new file mode 100644 index 00000000..233fa638 --- /dev/null +++ b/functionsUnittests/objectFunctions/flattenObject.test.ts @@ -0,0 +1,60 @@ +import { flattenObject } from '../../objectFunctions/flattenObject'; + +describe('flattenObject', () => { + // Test case 1: Flatten a simple object + it('1. should flatten a simple object', () => { + const obj = { a: 1, b: 2 }; + const result = flattenObject(obj); + const expected = { a: 1, b: 2 }; + expect(result).toEqual(expected); + }); + + // Test case 2: Flatten a nested object + 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 }; + 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 result = flattenObject(obj); + const expected = { 'a.0': 1, 'a.1': 2, 'a.2': 3, 'b.c': 4 }; + expect(result).toEqual(expected); + }); + + // Test case 4: Handle non-object input (number) + it('4. should throw a TypeError if input is a number', () => { + expect(() => flattenObject(42 as any)).toThrow(TypeError); + }); + + // Test case 5: Handle non-object input (string) + it('5. should throw a TypeError if input is a string', () => { + expect(() => flattenObject('string' as any)).toThrow(TypeError); + }); + + // Test case 6: Handle non-object input (boolean) + it('6. should throw a TypeError if input is a boolean', () => { + expect(() => flattenObject(true as any)).toThrow(TypeError); + }); + + // Test case 7: Handle non-object input (null) + it('7. should throw a TypeError if input is null', () => { + expect(() => flattenObject(null as any)).toThrow(TypeError); + }); + + // Test case 8: Handle non-object input (undefined) + it('8. should throw a TypeError if input is undefined', () => { + expect(() => flattenObject(undefined as any)).toThrow(TypeError); + }); + + // Test case 9: Flatten an object with nested arrays + it('9. should flatten an object with nested arrays', () => { + const obj = { a: { b: [1, 2, { c: 3 }] } }; + const result = flattenObject(obj); + const expected = { 'a.b.0': 1, 'a.b.1': 2, 'a.b.2.c': 3 }; + expect(result).toEqual(expected); + }); +}); diff --git a/functionsUnittests/objectFunctions/queryStringToObject.test.ts b/functionsUnittests/objectFunctions/queryStringToObject.test.ts new file mode 100644 index 00000000..4c08d0e8 --- /dev/null +++ b/functionsUnittests/objectFunctions/queryStringToObject.test.ts @@ -0,0 +1,52 @@ +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' }; + 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' }; + 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); + 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', () => { + 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', () => { + 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', () => { + 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', () => { + 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', () => { + expect(() => queryStringToObject(undefined as any)).toThrow(TypeError); + }); +}); diff --git a/objectFunctions/flattenObject.ts b/objectFunctions/flattenObject.ts new file mode 100644 index 00000000..4918321e --- /dev/null +++ b/objectFunctions/flattenObject.ts @@ -0,0 +1,22 @@ +/** + * Flattens a nested object. + * + * @param {Record} obj - The object to flatten. + * @param {string} [prefix=''] - The prefix for the keys (used for recursion). + * @returns {Record} - The flattened object. + * @throws {TypeError} - If the input is not an object or is null. + */ +export function flattenObject(obj: Record, prefix = ''): 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 newKey = prefix ? `${prefix}.${key}` : key; + if (typeof obj[key] === 'object' && obj[key] !== null) { + Object.assign(acc, flattenObject(obj[key], newKey)); + } else { + acc[newKey] = obj[key]; + } + return acc; + }, {} as Record); +} \ No newline at end of file diff --git a/objectFunctions/queryStringToObject.ts b/objectFunctions/queryStringToObject.ts new file mode 100644 index 00000000..a33ad67f --- /dev/null +++ b/objectFunctions/queryStringToObject.ts @@ -0,0 +1,13 @@ +/** + * Converts a query string into an object. + * + * @param {string} query - The query string to convert. + * @returns {Record} - An object representing the query string. + * @throws {TypeError} - If the input is not a string. + */ +export function queryStringToObject(query: string): Record { + if (typeof query !== 'string') { + throw new TypeError('Input must be a string'); + } + return Object.fromEntries(new URLSearchParams(query).entries()); +} \ No newline at end of file