Skip to content

Commit

Permalink
Merge a1da1f6 into 241527c
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Sep 26, 2020
2 parents 241527c + a1da1f6 commit bd39eca
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { arrayOf } from './arrayOf';
export { sumOf } from './sumOf';
export { pluck } from './pluck';
export { sum } from './sum';
8 changes: 4 additions & 4 deletions src/arrayOf.spec.ts → src/pluck.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { arrayOf } from './arrayOf';
import { pluck } from './pluck';

const data = [
{ complex: { value: 'x' }, id: 1, name: 'John' },
{ complex: { value: 'y' }, id: 2, name: 'Jane' },
];

test('numbers', () => {
expect(arrayOf(data, 'id')).toStrictEqual([1, 2]);
expect(pluck(data, 'id')).toStrictEqual([1, 2]);
});

test('strings', () => {
expect(arrayOf(data, 'name')).toStrictEqual(['John', 'Jane']);
expect(pluck(data, 'name')).toStrictEqual(['John', 'Jane']);
});

test('complex objects', () => {
expect(arrayOf(data, 'complex')).toStrictEqual([
expect(pluck(data, 'complex')).toStrictEqual([
{ value: 'x' },
{ value: 'y' },
]);
Expand Down
10 changes: 5 additions & 5 deletions src/arrayOf.ts → src/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ const byKey = <T extends unknown, K extends keyof T>(key: K) => (
) => [...accumulator, item[key]];

/**
* Takes an array of any objects and accumulates an array of a shared property
* between them.
* Takes an array of any objects and accumulates an array of a single shared
* property between them.
* @param {any[]} array The array of objects with a shared property.
* @param {string} key The property accessor for the objects in the array.
* @returns {any[]}
* @example
* ```ts
* const data = [{ name: "John", id: 1 }, { name: "Jane", id: 2 }];
*
* arrayOf(data, "name"); // ["John", "Jane"]
* arrayOf(data, "id"); // [1, 2]
* pluck(data, "name"); // ["John", "Jane"]
* pluck(data, "id"); // [1, 2]
* ```
*/
export const arrayOf = <T extends unknown, K extends keyof T>(
export const pluck = <T extends unknown, K extends keyof T>(
array: T[],
key: K,
): Array<T[K]> => array.reduce(byKey(key), []);
6 changes: 3 additions & 3 deletions src/sumOf.spec.ts → src/sum.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sumOf } from './sumOf';
import { sum } from './sum';

test('shallow objects', () => {
const data = [{ value: 1 }, { value: 1 }];

expect(sumOf(data, 'value')).toStrictEqual(2);
expect(sum(data, 'value')).toStrictEqual(2);
});

test('complex objects', () => {
Expand All @@ -12,5 +12,5 @@ test('complex objects', () => {
{ value: { nested: { nestedAgain: 1 } } },
];

expect(sumOf(data, 'value', 'nested', 'nestedAgain')).toStrictEqual(2);
expect(sum(data, 'value', 'nested', 'nestedAgain')).toStrictEqual(2);
});
4 changes: 2 additions & 2 deletions src/sumOf.ts → src/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const bySum = <
* ```ts
* const data = [{ nested: { value: 12 } }, { nested: { value: 34 } }];
*
* sumOf(data, "nested", "value"); // 46
* sum(data, "nested", "value"); // 46
* ```
*/
export const sumOf = <
export const sum = <
T,
K1 extends keyof T,
K2 extends keyof T[K1],
Expand Down

0 comments on commit bd39eca

Please sign in to comment.