Skip to content

Commit d7e23a3

Browse files
add includeOtherProperties to transposeObjectArray (#32)
* add includeOtherProperties to transposeObjectArray * execute all tests * fix lint * pre-commit
1 parent 9d00d53 commit d7e23a3

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
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: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import { transposeObjectArray } from '../transposeObjectArray';
33

44
describe('transposeObjectArray', () => {
55
it('should handle empty array', () => {
6-
const result = transposeObjectArray([], ['id', 'name']);
6+
const result = transposeObjectArray({
7+
objects: [],
8+
properties: ['id', 'name'],
9+
});
710
expect(result).to.deep.equal({});
811
});
912

1013
it('should extract multiple properties from array of objects', () => {
11-
const items = [
14+
const objects = [
1215
{ id: 1, name: 'John', age: 25, city: 'NY' },
1316
{ id: 2, name: 'Jane', age: 30, city: 'LA' },
1417
];
15-
const result = transposeObjectArray(items, ['id', 'name']);
18+
const result = transposeObjectArray({
19+
objects,
20+
properties: ['id', 'name'],
21+
});
1622
expect(result).to.deep.equal({
1723
id: [1, 2],
1824
name: ['John', 'Jane'],
@@ -24,12 +30,15 @@ describe('transposeObjectArray', () => {
2430
});
2531

2632
it('should handle objects with missing properties', () => {
27-
const items = [
33+
const objects = [
2834
{ id: 1, name: 'John', age: 25 },
2935
{ id: 2, age: 30 },
3036
{ id: 3, name: 'Bob', city: 'LA' },
3137
];
32-
const result = transposeObjectArray(items, ['id', 'name']);
38+
const result = transposeObjectArray({
39+
objects,
40+
properties: ['id', 'name'],
41+
});
3342
expect(result).to.deep.equal({
3443
id: [1, 2, 3],
3544
name: ['John', undefined, 'Bob'],
@@ -38,11 +47,14 @@ describe('transposeObjectArray', () => {
3847
});
3948

4049
it('should handle different value types', () => {
41-
const items = [
50+
const objects = [
4251
{ id: 1, active: true, count: 10, tags: ['a', 'b'] },
4352
{ id: 2, active: false, count: 20, tags: ['c'] },
4453
];
45-
const result = transposeObjectArray(items, ['active', 'tags']);
54+
const result = transposeObjectArray({
55+
objects,
56+
properties: ['active', 'tags'],
57+
});
4658
expect(result).to.deep.equal({
4759
active: [true, false],
4860
tags: [['a', 'b'], ['c']],
@@ -54,11 +66,14 @@ describe('transposeObjectArray', () => {
5466
});
5567

5668
it('should handle extracting all properties (empty rest)', () => {
57-
const items = [
69+
const objects = [
5870
{ id: 1, name: 'John' },
5971
{ id: 2, name: 'Jane' },
6072
];
61-
const result = transposeObjectArray(items, ['id', 'name']);
73+
const result = transposeObjectArray({
74+
objects,
75+
properties: ['id', 'name'],
76+
});
6277
expect(result).to.deep.equal({
6378
id: [1, 2],
6479
name: ['John', 'Jane'],
@@ -67,11 +82,11 @@ describe('transposeObjectArray', () => {
6782
});
6883

6984
it('should handle extracting no properties (everything in rest)', () => {
70-
const items = [
85+
const objects = [
7186
{ id: 1, name: 'John' },
7287
{ id: 2, name: 'Jane' },
7388
];
74-
const result = transposeObjectArray(items, []);
89+
const result = transposeObjectArray({ objects, properties: [] });
7590
expect(result).to.deep.equal({
7691
rest: [
7792
{ id: 1, name: 'John' },
@@ -81,11 +96,14 @@ describe('transposeObjectArray', () => {
8196
});
8297

8398
it('should handle objects with null or undefined values', () => {
84-
const items = [
99+
const objects = [
85100
{ id: 1, name: null, age: 25 },
86101
{ id: 2, name: undefined, age: 30 },
87102
];
88-
const result = transposeObjectArray(items, ['id', 'name']);
103+
const result = transposeObjectArray({
104+
objects,
105+
properties: ['id', 'name'],
106+
});
89107
expect(result).to.deep.equal({
90108
id: [1, 2],
91109
name: [null, undefined],
@@ -94,11 +112,14 @@ describe('transposeObjectArray', () => {
94112
});
95113

96114
it('should handle nested objects', () => {
97-
const items = [
115+
const objects = [
98116
{ id: 1, user: { name: 'John', age: 25 } },
99117
{ id: 2, user: { name: 'Jane', age: 30 } },
100118
];
101-
const result = transposeObjectArray(items, ['id', 'user']);
119+
const result = transposeObjectArray({
120+
objects,
121+
properties: ['id', 'user'],
122+
});
102123
expect(result).to.deep.equal({
103124
id: [1, 2],
104125
user: [
@@ -110,11 +131,11 @@ describe('transposeObjectArray', () => {
110131
});
111132

112133
it('should preserve property order in rest object', () => {
113-
const items = [
134+
const objects = [
114135
{ a: 1, b: 2, c: 3, d: 4 },
115136
{ a: 5, b: 6, c: 7, d: 8 },
116137
];
117-
const result = transposeObjectArray(items, ['a', 'c']);
138+
const result = transposeObjectArray({ objects, properties: ['a', 'c'] });
118139
expect(result).to.deep.equal({
119140
a: [1, 5],
120141
c: [3, 7],
@@ -124,4 +145,20 @@ describe('transposeObjectArray', () => {
124145
],
125146
});
126147
});
148+
149+
it('should omit rest properties if includeOtherProperties is false', () => {
150+
const objects = [
151+
{ id: 1, name: null, age: 25 },
152+
{ id: 2, name: undefined, age: 30 },
153+
];
154+
const result = transposeObjectArray({
155+
objects,
156+
properties: ['id', 'name'],
157+
options: { includeOtherProperties: false },
158+
});
159+
expect(result).to.deep.equal({
160+
id: [1, 2],
161+
name: [null, undefined],
162+
});
163+
});
127164
});

src/transposeObjectArray.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,36 @@ 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
*/
60-
export const transposeObjectArray = <T extends object, K extends keyof T>(
61-
items: T[],
62-
properties: K[],
63-
): TransposedObjectArray<T, K> =>
64-
items.reduce(
59+
export const transposeObjectArray = <T extends object, K extends keyof T>({
60+
objects,
61+
properties,
62+
options = { includeOtherProperties: true },
63+
}: {
64+
/** Array of objects to transpose */
65+
objects: T[];
66+
/** Array of property keys to transpose into arrays */
67+
properties: K[];
68+
/** Options for how to transpose the array */
69+
options?: {
70+
/** Whether to include non-tranposed properties in the final result */
71+
includeOtherProperties?: boolean;
72+
};
73+
}): TransposedObjectArray<T, K> =>
74+
objects.reduce(
6575
(acc, item) => {
6676
const result = { ...acc } as TransposedObjectArray<T, K>;
6777

@@ -77,7 +87,9 @@ export const transposeObjectArray = <T extends object, K extends keyof T>(
7787
}
7888
});
7989

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

8294
return result;
8395
},

0 commit comments

Comments
 (0)