Skip to content

Commit

Permalink
pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent c05884b commit 7765559
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
41 changes: 33 additions & 8 deletions src/tests/transposeObjectArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { transposeObjectArray } from '../transposeObjectArray';

describe('transposeObjectArray', () => {
it('should handle empty array', () => {
const result = transposeObjectArray({ objects: [], properties: ['id', 'name'] });
const result = transposeObjectArray({
objects: [],
properties: ['id', 'name'],
});
expect(result).to.deep.equal({});
});

Expand All @@ -12,7 +15,10 @@ describe('transposeObjectArray', () => {
{ id: 1, name: 'John', age: 25, city: 'NY' },
{ id: 2, name: 'Jane', age: 30, city: 'LA' },
];
const result = transposeObjectArray({ objects, properties: ['id', 'name'] });
const result = transposeObjectArray({
objects,
properties: ['id', 'name'],
});
expect(result).to.deep.equal({
id: [1, 2],
name: ['John', 'Jane'],
Expand All @@ -29,7 +35,10 @@ describe('transposeObjectArray', () => {
{ id: 2, age: 30 },
{ id: 3, name: 'Bob', city: 'LA' },
];
const result = transposeObjectArray({ objects, properties: ['id', 'name'] });
const result = transposeObjectArray({
objects,
properties: ['id', 'name'],
});
expect(result).to.deep.equal({
id: [1, 2, 3],
name: ['John', undefined, 'Bob'],
Expand All @@ -42,7 +51,10 @@ describe('transposeObjectArray', () => {
{ id: 1, active: true, count: 10, tags: ['a', 'b'] },
{ id: 2, active: false, count: 20, tags: ['c'] },
];
const result = transposeObjectArray({ objects, properties: ['active', 'tags'] });
const result = transposeObjectArray({
objects,
properties: ['active', 'tags'],
});
expect(result).to.deep.equal({
active: [true, false],
tags: [['a', 'b'], ['c']],
Expand All @@ -58,7 +70,10 @@ describe('transposeObjectArray', () => {
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
];
const result = transposeObjectArray({ objects, properties: ['id', 'name'] });
const result = transposeObjectArray({
objects,
properties: ['id', 'name'],
});
expect(result).to.deep.equal({
id: [1, 2],
name: ['John', 'Jane'],
Expand All @@ -85,7 +100,10 @@ describe('transposeObjectArray', () => {
{ id: 1, name: null, age: 25 },
{ id: 2, name: undefined, age: 30 },
];
const result = transposeObjectArray({ objects, properties: ['id', 'name'] });
const result = transposeObjectArray({
objects,
properties: ['id', 'name'],
});
expect(result).to.deep.equal({
id: [1, 2],
name: [null, undefined],
Expand All @@ -98,7 +116,10 @@ describe('transposeObjectArray', () => {
{ id: 1, user: { name: 'John', age: 25 } },
{ id: 2, user: { name: 'Jane', age: 30 } },
];
const result = transposeObjectArray({ objects, properties: ['id', 'user'] });
const result = transposeObjectArray({
objects,
properties: ['id', 'user'],
});
expect(result).to.deep.equal({
id: [1, 2],
user: [
Expand Down Expand Up @@ -130,7 +151,11 @@ describe('transposeObjectArray', () => {
{ id: 1, name: null, age: 25 },
{ id: 2, name: undefined, age: 30 },
];
const result = transposeObjectArray({ objects, properties: ['id', 'name'], options: { includeOtherProperties: false } });
const result = transposeObjectArray({
objects,
properties: ['id', 'name'],
options: { includeOtherProperties: false },
});
expect(result).to.deep.equal({
id: [1, 2],
name: [null, undefined],
Expand Down
16 changes: 9 additions & 7 deletions src/transposeObjectArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ type TransposedObjectArray<T, K extends keyof T> = {
* // rest: [{age: 25}, {age: 30}]
* // }
*/
export const transposeObjectArray = <T extends object, K extends keyof T>(
{ objects, properties, options = { includeOtherProperties: true } }: {
/** Array of objects to transpose */
objects: T[];
export const transposeObjectArray = <T extends object, K extends keyof T>({
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<T, K> =>
};
}): TransposedObjectArray<T, K> =>
objects.reduce(
(acc, item) => {
const result = { ...acc } as TransposedObjectArray<T, K>;
Expand Down

0 comments on commit 7765559

Please sign in to comment.