Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent 445be47 commit d882e9f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export * from './valuesOf';
export * from './findAllWithRegex';
export * from './flattenObject';
export * from './aggregateObjects';
export * from './partitionObjectArray';
export * from './partitionObjectArray';
24 changes: 10 additions & 14 deletions src/partitionObjectArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* Type that represents the partitioned properties from an object type T.
* Type that represents the partitioned properties from an object type T.
* For each selected property K from T, creates an array of that property's type.
* Also includes a 'rest' property containing an array of objects with all non-selected properties.
*
* @template T - The source object type
* @template K - The keys to partition from T
* @example
Expand Down Expand Up @@ -34,11 +33,10 @@ type PartitionedArrayProperties<T, K extends keyof T> = {
/** The array of remaining properties not selected for partitioning */
rest: Array<Omit<T, K>>;
};

/**
* Partitions an array of objects by separating specified properties into their own arrays
* 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 partition
* @param items - Array of objects to partition
Expand All @@ -50,10 +48,10 @@ type PartitionedArrayProperties<T, K extends keyof T> = {
* { id: 2, name: 'Jane', age: 30, city: 'LA' }
* ]
* const result = partitionObjectArray(items, ['id', 'name']);
* // Returns: {
* // id: [1, 2],
* // name: ['John', 'Jane'],
* // rest: [{age: 25, city: 'NY'}, {age: 30, city: 'LA'}]
* // Returns: {
* // id: [1, 2],
* // name: ['John', 'Jane'],
* // rest: [{age: 25, city: 'NY'}, {age: 30, city: 'LA'}]
* // }
*/
export const partitionObjectArray = <T extends object, K extends keyof T>(
Expand All @@ -62,22 +60,20 @@ type PartitionedArrayProperties<T, K extends keyof T> = {
): PartitionedArrayProperties<T, K> =>
items.reduce((acc, item) => {
const result = { ...acc } as PartitionedArrayProperties<T, K>;

properties.forEach((prop) => {
const currentArray = (acc[prop] || []) as T[K][];
result[prop] = [...currentArray, item[prop]] as any;
});

const restObject = {} as Omit<T, K>;
Object.entries(item).forEach(([key, value]) => {
if (!properties.includes(key as K)) {
(restObject as any)[key] = value;
}
});

result.rest = [...(acc.rest || []), restObject];

return result;
}, {} as PartitionedArrayProperties<T, K>);


2 changes: 1 addition & 1 deletion src/tests/flattenObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ describe('flattenObject', () => {
user_grandParents: '',
});
});
});
});
3 changes: 1 addition & 2 deletions src/tests/partitionObjectArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai';
import { partitionObjectArray } from '../partitionObjectArray';


describe('partitionObjectArray', () => {
it('should handle empty array', () => {
const result = partitionObjectArray([], ['id', 'name']);
Expand Down Expand Up @@ -132,4 +131,4 @@ describe('partitionObjectArray', () => {
]
});
});
});
});

0 comments on commit d882e9f

Please sign in to comment.