From 1c860ab073034b963cce7b39dac6b84803e624f3 Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 14 Jan 2025 18:59:42 +0000 Subject: [PATCH] add includeOtherProperties to transposeObjectArray --- package.json | 2 +- src/tests/transposeObjectArray.test.ts | 48 ++++++++++++++++---------- src/transposeObjectArray.ts | 27 ++++++++++----- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index c353fe6..5d0362d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Transcend Inc.", "name": "@transcend-io/type-utils", "description": "Small package containing useful typescript utilities.", - "version": "1.8.0", + "version": "1.8.1", "homepage": "https://github.com/transcend-io/type-utils", "repository": { "type": "git", diff --git a/src/tests/transposeObjectArray.test.ts b/src/tests/transposeObjectArray.test.ts index 3419f1d..0fdf88f 100644 --- a/src/tests/transposeObjectArray.test.ts +++ b/src/tests/transposeObjectArray.test.ts @@ -1,18 +1,18 @@ import { expect } from 'chai'; import { transposeObjectArray } from '../transposeObjectArray'; -describe('transposeObjectArray', () => { +describe.only('transposeObjectArray', () => { it('should handle empty array', () => { - const result = transposeObjectArray([], ['id', 'name']); + const result = transposeObjectArray({objects: [], properties: ['id', 'name']}); expect(result).to.deep.equal({}); }); it('should extract multiple properties from array of objects', () => { - const items = [ + const objects = [ { id: 1, name: 'John', age: 25, city: 'NY' }, { id: 2, name: 'Jane', age: 30, city: 'LA' }, ]; - const result = transposeObjectArray(items, ['id', 'name']); + const result = transposeObjectArray({objects, properties: ['id', 'name']}); expect(result).to.deep.equal({ id: [1, 2], name: ['John', 'Jane'], @@ -24,12 +24,12 @@ describe('transposeObjectArray', () => { }); it('should handle objects with missing properties', () => { - const items = [ + const objects = [ { id: 1, name: 'John', age: 25 }, { id: 2, age: 30 }, { id: 3, name: 'Bob', city: 'LA' }, ]; - const result = transposeObjectArray(items, ['id', 'name']); + const result = transposeObjectArray({objects, properties: ['id', 'name']}); expect(result).to.deep.equal({ id: [1, 2, 3], name: ['John', undefined, 'Bob'], @@ -38,11 +38,11 @@ describe('transposeObjectArray', () => { }); it('should handle different value types', () => { - const items = [ + const objects = [ { id: 1, active: true, count: 10, tags: ['a', 'b'] }, { id: 2, active: false, count: 20, tags: ['c'] }, ]; - const result = transposeObjectArray(items, ['active', 'tags']); + const result = transposeObjectArray({objects, properties: ['active', 'tags']}); expect(result).to.deep.equal({ active: [true, false], tags: [['a', 'b'], ['c']], @@ -54,11 +54,11 @@ describe('transposeObjectArray', () => { }); it('should handle extracting all properties (empty rest)', () => { - const items = [ + const objects = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, ]; - const result = transposeObjectArray(items, ['id', 'name']); + const result = transposeObjectArray({objects, properties: ['id', 'name']}); expect(result).to.deep.equal({ id: [1, 2], name: ['John', 'Jane'], @@ -67,11 +67,11 @@ describe('transposeObjectArray', () => { }); it('should handle extracting no properties (everything in rest)', () => { - const items = [ + const objects = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, ]; - const result = transposeObjectArray(items, []); + const result = transposeObjectArray({objects, properties: []}); expect(result).to.deep.equal({ rest: [ { id: 1, name: 'John' }, @@ -81,11 +81,11 @@ describe('transposeObjectArray', () => { }); it('should handle objects with null or undefined values', () => { - const items = [ + const objects = [ { id: 1, name: null, age: 25 }, { id: 2, name: undefined, age: 30 }, ]; - const result = transposeObjectArray(items, ['id', 'name']); + const result = transposeObjectArray({objects, properties: ['id', 'name']}); expect(result).to.deep.equal({ id: [1, 2], name: [null, undefined], @@ -94,11 +94,11 @@ describe('transposeObjectArray', () => { }); it('should handle nested objects', () => { - const items = [ + const objects = [ { id: 1, user: { name: 'John', age: 25 } }, { id: 2, user: { name: 'Jane', age: 30 } }, ]; - const result = transposeObjectArray(items, ['id', 'user']); + const result = transposeObjectArray({objects, properties: ['id', 'user']}); expect(result).to.deep.equal({ id: [1, 2], user: [ @@ -110,11 +110,11 @@ describe('transposeObjectArray', () => { }); it('should preserve property order in rest object', () => { - const items = [ + const objects = [ { a: 1, b: 2, c: 3, d: 4 }, { a: 5, b: 6, c: 7, d: 8 }, ]; - const result = transposeObjectArray(items, ['a', 'c']); + const result = transposeObjectArray({objects, properties: ['a', 'c']}); expect(result).to.deep.equal({ a: [1, 5], c: [3, 7], @@ -124,4 +124,16 @@ describe('transposeObjectArray', () => { ], }); }); + + it('should omit rest properties if includeOtherProperties is false', () => { + const objects = [ + { id: 1, name: null, age: 25 }, + { id: 2, name: undefined, age: 30 }, + ]; + 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 d62ed08..98fb589 100644 --- a/src/transposeObjectArray.ts +++ b/src/transposeObjectArray.ts @@ -42,15 +42,14 @@ type TransposedObjectArray = { * while keeping the remaining properties grouped in a 'rest' array. * @template T - The type of objects in the input array * @template K - The keys of properties to transpose - * @param items - Array of objects to transpose - * @param properties - Array of property keys to transpose into arrays + * @param param - the objects, properties, and transposing options * @returns An object containing transposed arrays for each selected property * @example - * const items = [ + * const objects = [ * { id: 1, name: 'John', age: 25 }, * { id: 2, name: 'Jane', age: 30 } * ] - * const result = transposeObjectArray(items, ['id', 'name']); + * const result = transposeObjectArray({objects, properties: ['id', 'name']}); * // Returns: { * // id: [1, 2], * // name: ['John', 'Jane'], @@ -58,10 +57,19 @@ type TransposedObjectArray = { * // } */ export const transposeObjectArray = ( - items: T[], - properties: K[], + { 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 => - items.reduce( + objects.reduce( (acc, item) => { const result = { ...acc } as TransposedObjectArray; @@ -77,7 +85,10 @@ export const transposeObjectArray = ( } }); - result.rest = [...(acc.rest || []), restObject]; + if(options.includeOtherProperties) { + + result.rest = [...(acc.rest || []), restObject]; + } return result; },