-
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
3 changed files
with
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {reduceTruthy, reduceFalsy} from '../index' | ||
|
||
describe('Reduce Functions', () => { | ||
it('should reduce truthy', () => { | ||
expect(reduceTruthy([true, true, false, true], e => e)).toBeFalsy() | ||
expect(reduceTruthy([true, true, true, true], e => e)).toBeTruthy() | ||
expect(reduceTruthy([false, true, true, true], e => e)).toBeFalsy() | ||
}) | ||
|
||
it('should only call the check function until it hits a false', () => { | ||
const check = jest.fn(e => e) | ||
|
||
reduceTruthy([true, true, false, true, true, true, false, true], check) | ||
|
||
expect(check).toHaveBeenCalledTimes(3) | ||
}) | ||
|
||
it('should reuce falsey', () => { | ||
expect(reduceFalsy([false, false, true, false], e => e)).toBeFalsy() | ||
expect(reduceFalsy([false, false, false, false], e => e)).toBeTruthy() | ||
expect(reduceFalsy([true, false, true, false], e => e)).toBeFalsy() | ||
}) | ||
}) |
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,29 @@ | ||
/** | ||
* Reduce the array into a single boolean value for if the check function returns true. | ||
* | ||
* _once the first `false` is encountered `check` is not run again_ | ||
* | ||
* @param array The array to test | ||
* @param check The function to check the array entries with | ||
* @returns `true` if `check` only ever returned `true`, otherwise `false` | ||
*/ | ||
export const reduceTruthy = <T>(array: T[], check: (entry: T) => boolean) => { | ||
return array.reduce((result, entry) => { | ||
if (!result) return false | ||
|
||
return check(entry) | ||
}, true) | ||
} | ||
|
||
/** | ||
* Reduce the array into a single boolean value for if the check function returns false. | ||
* | ||
* _once the first `true` is encountered `check` is not run again_ | ||
* | ||
* @param array The array to test | ||
* @param check The function to check the array entries with | ||
* @returns `true` if `check` only ever returned `false`, otherwise `false` | ||
*/ | ||
export const reduceFalsy = <T>(array: T[], check: (entry: T) => boolean) => { | ||
return reduceTruthy(array, e => !check(e)) | ||
} |
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