From 7765559872440cb0fecb5ecda17c71c3787f2ffe Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 14 Jan 2025 19:01:43 +0000 Subject: [PATCH] pre-commit --- src/tests/transposeObjectArray.test.ts | 41 +++++++++++++++++++++----- src/transposeObjectArray.ts | 16 +++++----- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/tests/transposeObjectArray.test.ts b/src/tests/transposeObjectArray.test.ts index 850a961..7dc5b93 100644 --- a/src/tests/transposeObjectArray.test.ts +++ b/src/tests/transposeObjectArray.test.ts @@ -3,7 +3,10 @@ import { transposeObjectArray } from '../transposeObjectArray'; describe('transposeObjectArray', () => { it('should handle empty array', () => { - const result = transposeObjectArray({ objects: [], properties: ['id', 'name'] }); + const result = transposeObjectArray({ + objects: [], + properties: ['id', 'name'], + }); expect(result).to.deep.equal({}); }); @@ -12,7 +15,10 @@ describe('transposeObjectArray', () => { { id: 1, name: 'John', age: 25, city: 'NY' }, { id: 2, name: 'Jane', age: 30, city: 'LA' }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'name'] }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'name'], + }); expect(result).to.deep.equal({ id: [1, 2], name: ['John', 'Jane'], @@ -29,7 +35,10 @@ describe('transposeObjectArray', () => { { id: 2, age: 30 }, { id: 3, name: 'Bob', city: 'LA' }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'name'] }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'name'], + }); expect(result).to.deep.equal({ id: [1, 2, 3], name: ['John', undefined, 'Bob'], @@ -42,7 +51,10 @@ describe('transposeObjectArray', () => { { id: 1, active: true, count: 10, tags: ['a', 'b'] }, { id: 2, active: false, count: 20, tags: ['c'] }, ]; - const result = transposeObjectArray({ objects, properties: ['active', 'tags'] }); + const result = transposeObjectArray({ + objects, + properties: ['active', 'tags'], + }); expect(result).to.deep.equal({ active: [true, false], tags: [['a', 'b'], ['c']], @@ -58,7 +70,10 @@ describe('transposeObjectArray', () => { { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'name'] }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'name'], + }); expect(result).to.deep.equal({ id: [1, 2], name: ['John', 'Jane'], @@ -85,7 +100,10 @@ describe('transposeObjectArray', () => { { id: 1, name: null, age: 25 }, { id: 2, name: undefined, age: 30 }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'name'] }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'name'], + }); expect(result).to.deep.equal({ id: [1, 2], name: [null, undefined], @@ -98,7 +116,10 @@ describe('transposeObjectArray', () => { { id: 1, user: { name: 'John', age: 25 } }, { id: 2, user: { name: 'Jane', age: 30 } }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'user'] }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'user'], + }); expect(result).to.deep.equal({ id: [1, 2], user: [ @@ -130,7 +151,11 @@ describe('transposeObjectArray', () => { { id: 1, name: null, age: 25 }, { id: 2, name: undefined, age: 30 }, ]; - const result = transposeObjectArray({ objects, properties: ['id', 'name'], options: { includeOtherProperties: false } }); + const result = transposeObjectArray({ + objects, + properties: ['id', 'name'], + options: { includeOtherProperties: false }, + }); expect(result).to.deep.equal({ id: [1, 2], name: [null, undefined], diff --git a/src/transposeObjectArray.ts b/src/transposeObjectArray.ts index 99dfe22..b1f1382 100644 --- a/src/transposeObjectArray.ts +++ b/src/transposeObjectArray.ts @@ -56,19 +56,21 @@ type TransposedObjectArray = { * // rest: [{age: 25}, {age: 30}] * // } */ -export const transposeObjectArray = ( - { objects, properties, options = { includeOtherProperties: true } }: { - /** Array of objects to transpose */ - objects: T[]; +export const transposeObjectArray = ({ + objects, + properties, + options = { includeOtherProperties: true }, +}: { + /** Array of objects to transpose */ + objects: T[]; /** Array of property keys to transpose into arrays */ properties: K[]; /** Options for how to transpose the array */ options?: { /** Whether to include non-tranposed properties in the final result */ includeOtherProperties?: boolean; - } - } -): TransposedObjectArray => + }; +}): TransposedObjectArray => objects.reduce( (acc, item) => { const result = { ...acc } as TransposedObjectArray;