Skip to content

Commit

Permalink
feat: chunk and filterByValue
Browse files Browse the repository at this point in the history
  • Loading branch information
davguij committed Jun 23, 2020
1 parent cc0cbb5 commit accf8df
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ shuffle<number>([1, 2, 3, 4, 5]); // returns [2, 5, 3, 4, 1]

### List of methods

- `chunk()` — Creates an array of elements split into subarrays the length of `size`. [See documentation.](https://davguij.github.io/utilist/modules/_difference_.html)
- `difference()` — Takes two arrays and returns the difference between them as a new array. [See documentation.](https://davguij.github.io/utilist/modules/_difference_.html)
- `filterByValue()` — Filters an object by its values. [See documentation.](https://davguij.github.io/utilist/modules/_filter_object_.html)
- `isDate()` — Assesses if the input is of the type Date. [See documentation.](https://davguij.github.io/utilist/modules/_is_date_.html)
- `isEqual()` — Compares two objects and returns whether their values are equivalent. [See documentation.](https://davguij.github.io/utilist/modules/_is_equal_.html)
- `mapValues()` — Like `map()` but applied to the values of an object. [See documentation.](https://davguij.github.io/utilist/modules/_map_values_.html)
Expand Down
25 changes: 25 additions & 0 deletions src/chunk.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { chunk } from './chunk';

describe('chunk', () => {
it('should create chunks', () => {
const a = [1, 2, 3, 4];
const e = [
[1, 2],
[3, 4],
];
expect(chunk(a, 2)).toEqual(e);
});

it('should default to make 1-element chunks', () => {
const a = ['one', 'two', 'three', 'four', 'five'];
const e = [['one'], ['two'], ['three'], ['four'], ['five']];
expect(chunk(a)).toEqual(e);
});

it('should return the last chunk as remaining elements', function() {
expect(chunk([0, 1, 2, 3, 4, 5], 4)).toEqual([
[0, 1, 2, 3],
[4, 5],
]);
});
});
17 changes: 17 additions & 0 deletions src/chunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Creates an array of elements split into subarrays the length of `size`. If `elements` can't be split evenly, the final chunk will be the remaining elements.
*
* @param elements The array to split.
* @param size The amount of elements in each subarrays.
*/

export function chunk<T>(elements: T[], size = 1) {
size = size > 0 ? size : 1;
const result: T[][] = [];
let index = 0;
while (index < elements.length) {
result.push(elements.slice(index, size + index));
index += size;
}
return result;
}
26 changes: 26 additions & 0 deletions src/filter-object.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { filterByValue } from './filter-object';

describe('filterByValue', () => {
it('should return a new object', () => {
const o = { key: true };
expect(filterByValue(o, v => v)).not.toBe(o);
});

it('should include only the properties with values that pass the callback fn', () => {
const o = { a: 1, b: 2, c: 3 };
const e = { a: 1, b: 2 };
const f = filterByValue(o, v => {
return v <= 2;
});
expect(f).toEqual(e);
});

it('should support an empty object', () => {
expect(filterByValue({}, v => v)).toEqual({});
});

it('should not support an array, and should just return the same array', () => {
expect(filterByValue([], _v => true)).toEqual([]);
expect(filterByValue([1], _v => true)).toEqual([1]);
});
});
19 changes: 19 additions & 0 deletions src/filter-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns a new object identical to the `source` object, but without the entries for which the function `fn` returns false.
*
* @param source Object to filter.
* @param fn Function used to filter each value. Needs to return a boolean.
*/

export function filterByValue<T, K extends keyof T>(
source: T,
fn: (value: T[K]) => boolean
) {
if (Array.isArray(source)) {
return source;
}
const o = Object.keys(source)
.filter(key => fn(source[key as K]))
.map(key => ({ [key]: source[key as K] }));
return Object.assign({}, ...o);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './chunk';
export * from './difference';
export * from './filter-object';
export * from './is-date';
export * from './is-equal';
export * from './map-values';
Expand Down

0 comments on commit accf8df

Please sign in to comment.