Skip to content

Commit bd38d2a

Browse files
author
Daniel Bradley
committed
feat(arrays): Add every function
Add function to check if every item in the array satisfies the given criteria.
1 parent 9e1ea88 commit bd38d2a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/arrays.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,40 @@ export function exists<T>(a: any, b?: any): any {
349349
return partial ? exec : exec(a)
350350
}
351351

352+
/**
353+
* Tests if every element of the array satisfies the given predicate.
354+
* @param source The input collection.
355+
* @param predicate A function to test against each item of the input collection.
356+
* @example
357+
* Arrays.every([1, 2], x => x === 1) // false
358+
* // or using pipe
359+
* pipe([1, 2], Arrays.every(x => x >= 1)) // true
360+
*/
361+
export function every<T>(
362+
source: ReadonlyArray<T>,
363+
predicate: (item: T, index: number) => boolean
364+
): boolean
365+
/**
366+
* Tests if every element of the array satisfies the given predicate.
367+
* @param predicate A function to test against each item of the input collection.
368+
* @param source The input collection.
369+
* @example
370+
* Arrays.every([1, 2], x => x === 1) // false
371+
* // or using pipe
372+
* pipe([1, 2], Arrays.every(x => x >= 1)) // true
373+
*/
374+
export function every<T>(
375+
predicate: (item: T, index: number) => boolean
376+
): (source: ReadonlyArray<T>) => boolean
377+
export function every<T>(a: any, b?: any): any {
378+
const partial = typeof a === 'function'
379+
const predicate: (item: T, index: number) => boolean = partial ? a : b
380+
function exec(source: ReadonlyArray<T>): boolean {
381+
return source.every(predicate)
382+
}
383+
return partial ? exec : exec(a)
384+
}
385+
352386
/**
353387
* Returns the first element for which the given function returns true or throws if not found.
354388
* If you don't want exceptions, use `find` instead.

test/arrays.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,34 @@ describe('exists', () => {
213213
})
214214
})
215215

216+
describe('every', () => {
217+
it('matches existance', () => {
218+
expect(
219+
pipe(
220+
[1, 2],
221+
Arrays.every(x => x >= 1)
222+
)
223+
).toEqual(true)
224+
})
225+
it('matches non-existance', () => {
226+
expect(
227+
pipe(
228+
[1, 2],
229+
Arrays.every(x => x === 1)
230+
)
231+
).toEqual(false)
232+
})
233+
test('invoke', () => {
234+
expect(Arrays.every([1, 2], x => x === 1)).toEqual(false)
235+
})
236+
test('with index', () => {
237+
expect(Arrays.every([1, 2], (x, index) => index >= 0)).toEqual(true)
238+
})
239+
test('invoke from readonly array', () => {
240+
expect(Arrays.every([2, 4] as ReadonlyArray<number>, x => x % 2 === 0)).toEqual(true)
241+
})
242+
})
243+
216244
describe('get', () => {
217245
test('piped match', () => {
218246
expect(

0 commit comments

Comments
 (0)