-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters