Skip to content

Commit 8a448b6

Browse files
pre-commit
1 parent d882e9f commit 8a448b6

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/partitionObjectArray.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ type PartitionedArrayProperties<T, K extends keyof T> = {
5454
* // rest: [{age: 25, city: 'NY'}, {age: 30, city: 'LA'}]
5555
* // }
5656
*/
57-
export const partitionObjectArray = <T extends object, K extends keyof T>(
58-
items: T[],
59-
properties: K[],
60-
): PartitionedArrayProperties<T, K> =>
61-
items.reduce((acc, item) => {
57+
export const partitionObjectArray = <T extends object, K extends keyof T>(
58+
items: T[],
59+
properties: K[],
60+
): PartitionedArrayProperties<T, K> =>
61+
items.reduce(
62+
(acc, item) => {
6263
const result = { ...acc } as PartitionedArrayProperties<T, K>;
6364

6465
properties.forEach((prop) => {
@@ -76,4 +77,6 @@ type PartitionedArrayProperties<T, K extends keyof T> = {
7677
result.rest = [...(acc.rest || []), restObject];
7778

7879
return result;
79-
}, {} as PartitionedArrayProperties<T, K>);
80+
},
81+
{} as PartitionedArrayProperties<T, K>,
82+
);

src/tests/partitionObjectArray.test.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,125 +10,118 @@ describe('partitionObjectArray', () => {
1010
it('should extract multiple properties from array of objects', () => {
1111
const items = [
1212
{ id: 1, name: 'John', age: 25, city: 'NY' },
13-
{ id: 2, name: 'Jane', age: 30, city: 'LA' }
13+
{ id: 2, name: 'Jane', age: 30, city: 'LA' },
1414
];
1515
const result = partitionObjectArray(items, ['id', 'name']);
1616
expect(result).to.deep.equal({
1717
id: [1, 2],
1818
name: ['John', 'Jane'],
1919
rest: [
2020
{ age: 25, city: 'NY' },
21-
{ age: 30, city: 'LA' }
22-
]
21+
{ age: 30, city: 'LA' },
22+
],
2323
});
2424
});
2525

2626
it('should handle objects with missing properties', () => {
2727
const items = [
2828
{ id: 1, name: 'John', age: 25 },
2929
{ id: 2, age: 30 },
30-
{ id: 3, name: 'Bob', city: 'LA' }
30+
{ id: 3, name: 'Bob', city: 'LA' },
3131
];
3232
const result = partitionObjectArray(items, ['id', 'name']);
3333
expect(result).to.deep.equal({
3434
id: [1, 2, 3],
3535
name: ['John', undefined, 'Bob'],
36-
rest: [
37-
{ age: 25 },
38-
{ age: 30 },
39-
{ city: 'LA' }
40-
]
36+
rest: [{ age: 25 }, { age: 30 }, { city: 'LA' }],
4137
});
4238
});
4339

4440
it('should handle different value types', () => {
4541
const items = [
4642
{ id: 1, active: true, count: 10, tags: ['a', 'b'] },
47-
{ id: 2, active: false, count: 20, tags: ['c'] }
43+
{ id: 2, active: false, count: 20, tags: ['c'] },
4844
];
4945
const result = partitionObjectArray(items, ['active', 'tags']);
5046
expect(result).to.deep.equal({
5147
active: [true, false],
5248
tags: [['a', 'b'], ['c']],
5349
rest: [
5450
{ id: 1, count: 10 },
55-
{ id: 2, count: 20 }
56-
]
51+
{ id: 2, count: 20 },
52+
],
5753
});
5854
});
5955

6056
it('should handle extracting all properties (empty rest)', () => {
6157
const items = [
6258
{ id: 1, name: 'John' },
63-
{ id: 2, name: 'Jane' }
59+
{ id: 2, name: 'Jane' },
6460
];
6561
const result = partitionObjectArray(items, ['id', 'name']);
6662
expect(result).to.deep.equal({
6763
id: [1, 2],
6864
name: ['John', 'Jane'],
69-
rest: [{}, {}]
65+
rest: [{}, {}],
7066
});
7167
});
7268

7369
it('should handle extracting no properties (everything in rest)', () => {
7470
const items = [
7571
{ id: 1, name: 'John' },
76-
{ id: 2, name: 'Jane' }
72+
{ id: 2, name: 'Jane' },
7773
];
7874
const result = partitionObjectArray(items, []);
7975
expect(result).to.deep.equal({
8076
rest: [
8177
{ id: 1, name: 'John' },
82-
{ id: 2, name: 'Jane' }
83-
]
78+
{ id: 2, name: 'Jane' },
79+
],
8480
});
8581
});
8682

8783
it('should handle objects with null or undefined values', () => {
8884
const items = [
8985
{ id: 1, name: null, age: 25 },
90-
{ id: 2, name: undefined, age: 30 }
86+
{ id: 2, name: undefined, age: 30 },
9187
];
9288
const result = partitionObjectArray(items, ['id', 'name']);
9389
expect(result).to.deep.equal({
9490
id: [1, 2],
9591
name: [null, undefined],
96-
rest: [
97-
{ age: 25 },
98-
{ age: 30 }
99-
]
92+
rest: [{ age: 25 }, { age: 30 }],
10093
});
10194
});
10295

10396
it('should handle nested objects', () => {
10497
const items = [
10598
{ id: 1, user: { name: 'John', age: 25 } },
106-
{ id: 2, user: { name: 'Jane', age: 30 } }
99+
{ id: 2, user: { name: 'Jane', age: 30 } },
107100
];
108101
const result = partitionObjectArray(items, ['id', 'user']);
109102
expect(result).to.deep.equal({
110103
id: [1, 2],
111104
user: [
112105
{ name: 'John', age: 25 },
113-
{ name: 'Jane', age: 30 }
106+
{ name: 'Jane', age: 30 },
114107
],
115-
rest: [{}, {}]
108+
rest: [{}, {}],
116109
});
117110
});
118111

119112
it('should preserve property order in rest object', () => {
120113
const items = [
121114
{ a: 1, b: 2, c: 3, d: 4 },
122-
{ a: 5, b: 6, c: 7, d: 8 }
115+
{ a: 5, b: 6, c: 7, d: 8 },
123116
];
124117
const result = partitionObjectArray(items, ['a', 'c']);
125118
expect(result).to.deep.equal({
126119
a: [1, 5],
127120
c: [3, 7],
128121
rest: [
129122
{ b: 2, d: 4 },
130-
{ b: 6, d: 8 }
131-
]
123+
{ b: 6, d: 8 },
124+
],
132125
});
133126
});
134127
});

0 commit comments

Comments
 (0)