Skip to content

Commit

Permalink
Add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Oct 18, 2023
1 parent 3fe388b commit c110423
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const groupBy = <T>(data: T[], prop: Extract<keyof T, string>): Record<keyof T, T[]> => {
const result = {} as Record<keyof T, T[]>;
data.forEach((item) => {
const key = String(item[prop]) as keyof T;
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
});
return result;
};

export const aggregate = <T, U>(data: T[], prop: Extract<keyof T, string>, map: (x: T[]) => U): Record<keyof T, U> => {
const result = {} as Record<keyof T, U>;
const grouped = groupBy(data, prop);
Object.keys(grouped).forEach((key) => {
result[key as keyof T] = map(grouped[key as keyof T]);
});

return result;
};
8 changes: 8 additions & 0 deletions src/simpleHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import crypto from 'crypto';

export const getSimpleHash = (obj: unknown) => {
if (obj === undefined) throw new Error('Object is undefined');

const objString = JSON.stringify(obj);
return crypto.createHash('sha256').update(objString).digest('hex');
};
48 changes: 48 additions & 0 deletions test/groupBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { groupBy, aggregate } from '../src/groupBy';

describe('groupBy', () => {
it('should group an array of objects by a given property', () => {
const data = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'John' },
{ id: 4, name: 'Jane' },
];

const result = groupBy(data, 'name');

expect(result).toEqual({
John: [
{ id: 1, name: 'John' },
{ id: 3, name: 'John' },
],
Jane: [
{ id: 2, name: 'Jane' },
{ id: 4, name: 'Jane' },
],
});
});
});

describe('aggregate', () => {
it('should group an array of objects by a given property and apply a map function', () => {
const data = [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 },
{ id: 3, name: 'John', age: 35 },
{ id: 4, name: 'Jane', age: 40 },
];

const result = aggregate(data, 'name', (group) => {
const totalAge = group.reduce((acc, curr) => acc + curr.age, 0);
const averageAge = totalAge / group.length;

return { totalAge, averageAge };
});

expect(result).toEqual({
John: { totalAge: 65, averageAge: 32.5 },
Jane: { totalAge: 65, averageAge: 32.5 },
});
});
});
27 changes: 27 additions & 0 deletions test/simpleHash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getSimpleHash } from '../src/simpleHash';

describe('getSimpleHash', () => {
it('should return a string', () => {
const result = getSimpleHash('test');
expect(typeof result).toBe('string');
});

it('should return the same hash for the same input', () => {
const input = { name: 'John', age: 30 };
const result1 = getSimpleHash(input);
const result2 = getSimpleHash(input);
expect(result1).toBe(result2);
});

it('should return different hashes for different inputs', () => {
const input1 = { name: 'John', age: 30 };
const input2 = { name: 'Jane', age: 25 };
const result1 = getSimpleHash(input1);
const result2 = getSimpleHash(input2);
expect(result1).not.toBe(result2);
});

it('should fail if input is undefined', () => {
expect(() => getSimpleHash(undefined)).toThrowError('Object is undefined');
});
});

0 comments on commit c110423

Please sign in to comment.