Skip to content

Commit

Permalink
feat: reduce functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed May 25, 2021
1 parent 7e559ad commit fda77ba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/functions/reduce.spec.ts
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()
})
})
29 changes: 29 additions & 0 deletions src/functions/reduce.ts
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))
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {indexedBy, IndexedArray, IndexedByOptions} from './functions/indexed-by'
export {keys} from './functions/keys'
export {mapProperty} from './functions/map-property'
export {rangeAsString, rangeAsArray} from './functions/range-as-string'
export {reduceTruthy, reduceFalsy} from './functions/reduce'
export {propIs, propIsNot} from './functions/selectors'
export {nl2br} from './functions/nl2br'
export {parameterize} from './functions/parameterize'
Expand Down

0 comments on commit fda77ba

Please sign in to comment.