Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}
Expand Down
60 changes: 60 additions & 0 deletions functionsUnittests/objectFunctions/flattenObject.test.ts
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 30 in functionsUnittests/objectFunctions/flattenObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/flattenObject.test.ts#L30

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 35 in functionsUnittests/objectFunctions/flattenObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/flattenObject.test.ts#L35

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 40 in functionsUnittests/objectFunctions/flattenObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/flattenObject.test.ts#L40

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 45 in functionsUnittests/objectFunctions/flattenObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/flattenObject.test.ts#L45

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 50 in functionsUnittests/objectFunctions/flattenObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/flattenObject.test.ts#L50

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);
});
});
52 changes: 52 additions & 0 deletions functionsUnittests/objectFunctions/queryStringToObject.test.ts
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 30 in functionsUnittests/objectFunctions/queryStringToObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/queryStringToObject.test.ts#L30

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 35 in functionsUnittests/objectFunctions/queryStringToObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/queryStringToObject.test.ts#L35

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 40 in functionsUnittests/objectFunctions/queryStringToObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/queryStringToObject.test.ts#L40

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 45 in functionsUnittests/objectFunctions/queryStringToObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/queryStringToObject.test.ts#L45

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});

// 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);

Check warning on line 50 in functionsUnittests/objectFunctions/queryStringToObject.test.ts

View check run for this annotation

codefactor.io / CodeFactor

functionsUnittests/objectFunctions/queryStringToObject.test.ts#L50

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
});
});
22 changes: 22 additions & 0 deletions objectFunctions/flattenObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Flattens a nested object.
*
* @param {Record<string, any>} obj - The object to flatten.
* @param {string} [prefix=''] - The prefix for the keys (used for recursion).
* @returns {Record<string, any>} - The flattened object.
* @throws {TypeError} - If the input is not an object or is null.
*/
export function flattenObject(obj: Record<string, any>, prefix = ''): Record<string, any> {

Check warning on line 9 in objectFunctions/flattenObject.ts

View check run for this annotation

codefactor.io / CodeFactor

objectFunctions/flattenObject.ts#L9

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

Check warning on line 9 in objectFunctions/flattenObject.ts

View check run for this annotation

codefactor.io / CodeFactor

objectFunctions/flattenObject.ts#L9

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
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<string, any>);

Check warning on line 21 in objectFunctions/flattenObject.ts

View check run for this annotation

codefactor.io / CodeFactor

objectFunctions/flattenObject.ts#L21

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
}
13 changes: 13 additions & 0 deletions objectFunctions/queryStringToObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Converts a query string into an object.
*
* @param {string} query - The query string to convert.
* @returns {Record<string, string>} - An object representing the query string.
* @throws {TypeError} - If the input is not a string.
*/
export function queryStringToObject(query: string): Record<string, string> {
if (typeof query !== 'string') {
throw new TypeError('Input must be a string');
}
return Object.fromEntries(new URLSearchParams(query).entries());
}
Loading