Skip to content

Commit 1c860ab

Browse files
add includeOtherProperties to transposeObjectArray
1 parent 9d00d53 commit 1c860ab

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Transcend Inc.",
33
"name": "@transcend-io/type-utils",
44
"description": "Small package containing useful typescript utilities.",
5-
"version": "1.8.0",
5+
"version": "1.8.1",
66
"homepage": "https://github.com/transcend-io/type-utils",
77
"repository": {
88
"type": "git",

src/tests/transposeObjectArray.test.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { expect } from 'chai';
22
import { transposeObjectArray } from '../transposeObjectArray';
33

4-
describe('transposeObjectArray', () => {
4+
describe.only('transposeObjectArray', () => {
55
it('should handle empty array', () => {
6-
const result = transposeObjectArray([], ['id', 'name']);
6+
const result = transposeObjectArray({objects: [], properties: ['id', 'name']});
77
expect(result).to.deep.equal({});
88
});
99

1010
it('should extract multiple properties from array of objects', () => {
11-
const items = [
11+
const objects = [
1212
{ id: 1, name: 'John', age: 25, city: 'NY' },
1313
{ id: 2, name: 'Jane', age: 30, city: 'LA' },
1414
];
15-
const result = transposeObjectArray(items, ['id', 'name']);
15+
const result = transposeObjectArray({objects, properties: ['id', 'name']});
1616
expect(result).to.deep.equal({
1717
id: [1, 2],
1818
name: ['John', 'Jane'],
@@ -24,12 +24,12 @@ describe('transposeObjectArray', () => {
2424
});
2525

2626
it('should handle objects with missing properties', () => {
27-
const items = [
27+
const objects = [
2828
{ id: 1, name: 'John', age: 25 },
2929
{ id: 2, age: 30 },
3030
{ id: 3, name: 'Bob', city: 'LA' },
3131
];
32-
const result = transposeObjectArray(items, ['id', 'name']);
32+
const result = transposeObjectArray({objects, properties: ['id', 'name']});
3333
expect(result).to.deep.equal({
3434
id: [1, 2, 3],
3535
name: ['John', undefined, 'Bob'],
@@ -38,11 +38,11 @@ describe('transposeObjectArray', () => {
3838
});
3939

4040
it('should handle different value types', () => {
41-
const items = [
41+
const objects = [
4242
{ id: 1, active: true, count: 10, tags: ['a', 'b'] },
4343
{ id: 2, active: false, count: 20, tags: ['c'] },
4444
];
45-
const result = transposeObjectArray(items, ['active', 'tags']);
45+
const result = transposeObjectArray({objects, properties: ['active', 'tags']});
4646
expect(result).to.deep.equal({
4747
active: [true, false],
4848
tags: [['a', 'b'], ['c']],
@@ -54,11 +54,11 @@ describe('transposeObjectArray', () => {
5454
});
5555

5656
it('should handle extracting all properties (empty rest)', () => {
57-
const items = [
57+
const objects = [
5858
{ id: 1, name: 'John' },
5959
{ id: 2, name: 'Jane' },
6060
];
61-
const result = transposeObjectArray(items, ['id', 'name']);
61+
const result = transposeObjectArray({objects, properties: ['id', 'name']});
6262
expect(result).to.deep.equal({
6363
id: [1, 2],
6464
name: ['John', 'Jane'],
@@ -67,11 +67,11 @@ describe('transposeObjectArray', () => {
6767
});
6868

6969
it('should handle extracting no properties (everything in rest)', () => {
70-
const items = [
70+
const objects = [
7171
{ id: 1, name: 'John' },
7272
{ id: 2, name: 'Jane' },
7373
];
74-
const result = transposeObjectArray(items, []);
74+
const result = transposeObjectArray({objects, properties: []});
7575
expect(result).to.deep.equal({
7676
rest: [
7777
{ id: 1, name: 'John' },
@@ -81,11 +81,11 @@ describe('transposeObjectArray', () => {
8181
});
8282

8383
it('should handle objects with null or undefined values', () => {
84-
const items = [
84+
const objects = [
8585
{ id: 1, name: null, age: 25 },
8686
{ id: 2, name: undefined, age: 30 },
8787
];
88-
const result = transposeObjectArray(items, ['id', 'name']);
88+
const result = transposeObjectArray({objects, properties: ['id', 'name']});
8989
expect(result).to.deep.equal({
9090
id: [1, 2],
9191
name: [null, undefined],
@@ -94,11 +94,11 @@ describe('transposeObjectArray', () => {
9494
});
9595

9696
it('should handle nested objects', () => {
97-
const items = [
97+
const objects = [
9898
{ id: 1, user: { name: 'John', age: 25 } },
9999
{ id: 2, user: { name: 'Jane', age: 30 } },
100100
];
101-
const result = transposeObjectArray(items, ['id', 'user']);
101+
const result = transposeObjectArray({objects, properties: ['id', 'user']});
102102
expect(result).to.deep.equal({
103103
id: [1, 2],
104104
user: [
@@ -110,11 +110,11 @@ describe('transposeObjectArray', () => {
110110
});
111111

112112
it('should preserve property order in rest object', () => {
113-
const items = [
113+
const objects = [
114114
{ a: 1, b: 2, c: 3, d: 4 },
115115
{ a: 5, b: 6, c: 7, d: 8 },
116116
];
117-
const result = transposeObjectArray(items, ['a', 'c']);
117+
const result = transposeObjectArray({objects, properties: ['a', 'c']});
118118
expect(result).to.deep.equal({
119119
a: [1, 5],
120120
c: [3, 7],
@@ -124,4 +124,16 @@ describe('transposeObjectArray', () => {
124124
],
125125
});
126126
});
127+
128+
it('should omit rest properties if includeOtherProperties is false', () => {
129+
const objects = [
130+
{ id: 1, name: null, age: 25 },
131+
{ id: 2, name: undefined, age: 30 },
132+
];
133+
const result = transposeObjectArray({objects, properties: ['id', 'name'], options: {includeOtherProperties: false}});
134+
expect(result).to.deep.equal({
135+
id: [1, 2],
136+
name: [null, undefined],
137+
});
138+
});
127139
});

src/transposeObjectArray.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,34 @@ type TransposedObjectArray<T, K extends keyof T> = {
4242
* while keeping the remaining properties grouped in a 'rest' array.
4343
* @template T - The type of objects in the input array
4444
* @template K - The keys of properties to transpose
45-
* @param items - Array of objects to transpose
46-
* @param properties - Array of property keys to transpose into arrays
45+
* @param param - the objects, properties, and transposing options
4746
* @returns An object containing transposed arrays for each selected property
4847
* @example
49-
* const items = [
48+
* const objects = [
5049
* { id: 1, name: 'John', age: 25 },
5150
* { id: 2, name: 'Jane', age: 30 }
5251
* ]
53-
* const result = transposeObjectArray(items, ['id', 'name']);
52+
* const result = transposeObjectArray({objects, properties: ['id', 'name']});
5453
* // Returns: {
5554
* // id: [1, 2],
5655
* // name: ['John', 'Jane'],
5756
* // rest: [{age: 25}, {age: 30}]
5857
* // }
5958
*/
6059
export const transposeObjectArray = <T extends object, K extends keyof T>(
61-
items: T[],
62-
properties: K[],
60+
{ objects, properties, options = { includeOtherProperties: true } }: {
61+
/** Array of objects to transpose */
62+
objects: T[];
63+
/** Array of property keys to transpose into arrays */
64+
properties: K[];
65+
/** Options for how to transpose the array */
66+
options?: {
67+
/** Whether to include non-tranposed properties in the final result */
68+
includeOtherProperties?: boolean;
69+
}
70+
}
6371
): TransposedObjectArray<T, K> =>
64-
items.reduce(
72+
objects.reduce(
6573
(acc, item) => {
6674
const result = { ...acc } as TransposedObjectArray<T, K>;
6775

@@ -77,7 +85,10 @@ export const transposeObjectArray = <T extends object, K extends keyof T>(
7785
}
7886
});
7987

80-
result.rest = [...(acc.rest || []), restObject];
88+
if(options.includeOtherProperties) {
89+
90+
result.rest = [...(acc.rest || []), restObject];
91+
}
8192

8293
return result;
8394
},

0 commit comments

Comments
 (0)