1
0
Fork 0
mirror of https://github.com/CorentinTh/it-tools.git synced 2025-08-09 15:35:19 +02:00

Updated condense json function and fixed issues found in typecheck

This commit is contained in:
Bryan Liao 2025-07-07 16:25:00 -07:00
parent fb6257b088
commit fe5c934798
2 changed files with 32 additions and 27 deletions

View file

@ -1,24 +1,26 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { condenseJsonStructures } from './json-data-condenser.service'; import { type JsonValue, condenseJsonStructures } from './json-data-condenser.service';
const asJsonValue = <T extends JsonValue>(val: T): T => val;
describe('condenseJsonStructures', () => { describe('condenseJsonStructures', () => {
it('removes duplicate object structures in an array', () => { it('removes duplicate object structures in an array', () => {
const input = { const input = asJsonValue({
users: [ users: [
{ id: 1, name: 'Alice' }, { id: 1, name: 'Alice', email: null },
{ id: 2, name: 'Bob' }, { id: 2, name: 'Bob', email: null },
{ id: 3, name: 'Charlie' }, { id: 3, name: 'Charlie', email: null },
{ id: 4, name: 'David', email: 'david@example.com' }, { id: 4, name: 'David', email: 'david@example.com' },
{ id: 5, name: 'Eve' }, { id: 5, name: 'Eve', email: null },
], ],
status: 'active', status: 'active',
}; });
const output = condenseJsonStructures(input); const output = condenseJsonStructures(input);
expect(output).toEqual({ expect(output).toEqual({
users: [ users: [
{ id: 1, name: 'Alice' }, { id: 1, name: 'Alice', email: null },
{ id: 4, name: 'David', email: 'david@example.com' }, { id: 4, name: 'David', email: 'david@example.com' },
], ],
status: 'active', status: 'active',
@ -26,14 +28,14 @@ describe('condenseJsonStructures', () => {
}); });
it('keeps all non-array values intact', () => { it('keeps all non-array values intact', () => {
const input = { const input = asJsonValue({
status: 'ok', status: 'ok',
count: 5, count: 5,
success: true, success: true,
metadata: { metadata: {
source: 'api', source: 'api',
}, },
}; });
const output = condenseJsonStructures(input); const output = condenseJsonStructures(input);
@ -41,7 +43,7 @@ describe('condenseJsonStructures', () => {
}); });
it('keeps non-object array values', () => { 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); const output = condenseJsonStructures(input);
@ -49,20 +51,20 @@ describe('condenseJsonStructures', () => {
}); });
it('recursively condenses nested object arrays', () => { it('recursively condenses nested object arrays', () => {
const input = { const input = asJsonValue({
groups: [ groups: [
{ {
name: 'Group A', name: 'Group A',
users: [ users: [
{ id: 1, name: 'Alice' }, { id: 1, name: 'Alice', email: null },
{ id: 2, name: 'Bob' }, { id: 2, name: 'Bob', email: null },
{ id: 3, name: 'Charlie' }, { id: 3, name: 'Charlie', email: null },
{ id: 4, name: 'David', email: 'david@example.com' }, { id: 4, name: 'David', email: 'david@example.com' },
{ id: 5, name: 'Eve' }, { id: 5, name: 'Eve', email: null },
], ],
}, },
], ],
}; });
const output = condenseJsonStructures(input); const output = condenseJsonStructures(input);
@ -71,7 +73,7 @@ describe('condenseJsonStructures', () => {
{ {
name: 'Group A', name: 'Group A',
users: [ users: [
{ id: 1, name: 'Alice' }, { id: 1, name: 'Alice', email: null },
{ id: 4, name: 'David', email: 'david@example.com' }, { id: 4, name: 'David', email: 'david@example.com' },
], ],
}, },

View file

@ -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, any>): string { function getKeySignature(obj: Record<string, any>): string {
// Create a normalized signature string of sorted keys // Create a normalized signature string of sorted keys
@ -6,6 +6,12 @@ function getKeySignature(obj: Record<string, any>): string {
} }
export function condenseJsonStructures(data: JsonValue): JsonValue { export function condenseJsonStructures(data: JsonValue): JsonValue {
// Handle primitive values
if (typeof data !== 'object' || data === null) {
return data;
}
// Handle arrays
if (Array.isArray(data)) { if (Array.isArray(data)) {
const seenSignatures = new Set<string>(); const seenSignatures = new Set<string>();
const result: JsonValue[] = []; const result: JsonValue[] = [];
@ -27,13 +33,10 @@ export function condenseJsonStructures(data: JsonValue): JsonValue {
return result; return result;
} }
if (typeof data === 'object' && data !== null) { // Handle plain objects
const result: Record<string, JsonValue> = {}; const result: Record<string, JsonValue> = {};
for (const key in data) { for (const key in data) {
result[key] = condenseJsonStructures(data[key]); result[key] = condenseJsonStructures(data[key]);
}
return result;
} }
return result;
return data;
} }