diff --git a/src/tools/json-data-condenser/json-data-condenser.service.test.ts b/src/tools/json-data-condenser/json-data-condenser.service.test.ts index 5e14de43..37671a13 100644 --- a/src/tools/json-data-condenser/json-data-condenser.service.test.ts +++ b/src/tools/json-data-condenser/json-data-condenser.service.test.ts @@ -1,24 +1,26 @@ import { describe, expect, it } from 'vitest'; -import { condenseJsonStructures } from './json-data-condenser.service'; +import { type JsonValue, condenseJsonStructures } from './json-data-condenser.service'; + +const asJsonValue = (val: T): T => val; describe('condenseJsonStructures', () => { it('removes duplicate object structures in an array', () => { - const input = { + const input = asJsonValue({ users: [ - { id: 1, name: 'Alice' }, - { id: 2, name: 'Bob' }, - { id: 3, name: 'Charlie' }, + { id: 1, name: 'Alice', email: null }, + { id: 2, name: 'Bob', email: null }, + { id: 3, name: 'Charlie', email: null }, { id: 4, name: 'David', email: 'david@example.com' }, - { id: 5, name: 'Eve' }, + { id: 5, name: 'Eve', email: null }, ], status: 'active', - }; + }); const output = condenseJsonStructures(input); expect(output).toEqual({ users: [ - { id: 1, name: 'Alice' }, + { id: 1, name: 'Alice', email: null }, { id: 4, name: 'David', email: 'david@example.com' }, ], status: 'active', @@ -26,14 +28,14 @@ describe('condenseJsonStructures', () => { }); it('keeps all non-array values intact', () => { - const input = { + const input = asJsonValue({ status: 'ok', count: 5, success: true, metadata: { source: 'api', }, - }; + }); const output = condenseJsonStructures(input); @@ -41,7 +43,7 @@ describe('condenseJsonStructures', () => { }); it('keeps non-object array values', () => { - const input = [1, 2, 3, 'a', true, null]; + const input = asJsonValue([1, 2, 3, 'a', true, null]); const output = condenseJsonStructures(input); @@ -49,20 +51,20 @@ describe('condenseJsonStructures', () => { }); it('recursively condenses nested object arrays', () => { - const input = { + const input = asJsonValue({ groups: [ { name: 'Group A', users: [ - { id: 1, name: 'Alice' }, - { id: 2, name: 'Bob' }, - { id: 3, name: 'Charlie' }, + { id: 1, name: 'Alice', email: null }, + { id: 2, name: 'Bob', email: null }, + { id: 3, name: 'Charlie', email: null }, { id: 4, name: 'David', email: 'david@example.com' }, - { id: 5, name: 'Eve' }, + { id: 5, name: 'Eve', email: null }, ], }, ], - }; + }); const output = condenseJsonStructures(input); @@ -71,7 +73,7 @@ describe('condenseJsonStructures', () => { { name: 'Group A', users: [ - { id: 1, name: 'Alice' }, + { id: 1, name: 'Alice', email: null }, { id: 4, name: 'David', email: 'david@example.com' }, ], }, diff --git a/src/tools/json-data-condenser/json-data-condenser.service.ts b/src/tools/json-data-condenser/json-data-condenser.service.ts index 56bdff0d..10f4c146 100644 --- a/src/tools/json-data-condenser/json-data-condenser.service.ts +++ b/src/tools/json-data-condenser/json-data-condenser.service.ts @@ -1,4 +1,4 @@ -type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; +export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; function getKeySignature(obj: Record): string { // Create a normalized signature string of sorted keys @@ -6,6 +6,12 @@ function getKeySignature(obj: Record): string { } export function condenseJsonStructures(data: JsonValue): JsonValue { + // Handle primitive values + if (typeof data !== 'object' || data === null) { + return data; + } + + // Handle arrays if (Array.isArray(data)) { const seenSignatures = new Set(); const result: JsonValue[] = []; @@ -27,13 +33,10 @@ export function condenseJsonStructures(data: JsonValue): JsonValue { return result; } - if (typeof data === 'object' && data !== null) { - const result: Record = {}; - for (const key in data) { - result[key] = condenseJsonStructures(data[key]); - } - return result; + // Handle plain objects + const result: Record = {}; + for (const key in data) { + result[key] = condenseJsonStructures(data[key]); } - - return data; + return result; }