Skip to content

Commit

Permalink
feat(sets): Add every function
Browse files Browse the repository at this point in the history
Add function to check if every item in the collection satisfies the given criteria.
  • Loading branch information
Daniel Bradley committed Feb 8, 2019
1 parent d8b3550 commit af5f2c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/sets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ export function exists<T>(a: any, b?: any): any {
return partial ? exec : exec(a)
}

/**
* Tests if any element of the set satisfies the given predicate.
* @param source The input collection.
* @param predicate A function to test each item of the input collection.
*/
export function every<T>(source: ReadonlySet<T>, predicate: (item: T) => boolean): boolean
/**
* Tests if any element of the set satisfies the given predicate.
* @param predicate A function to test each item of the input collection.
* @param source The input collection.
*/
export function every<T>(predicate: (item: T) => boolean): (source: ReadonlySet<T>) => boolean
export function every<T>(a: any, b?: any): any {
const partial = typeof a === 'function'
const predicate: (item: T) => boolean = partial ? a : b
function exec(source: ReadonlySet<T>): boolean {
return Iterables.every(source, predicate)
}
return partial ? exec : exec(a)
}

/**
* Evaluates to true if the given item is in the source set.
* @param source The input collection.
Expand Down
12 changes: 12 additions & 0 deletions test/sets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ describe('exists', () => {
})
})

describe('every', () => {
it('matches existance', () => {
expect(pipe(new Set([1, 2])).then(Sets.every(x => x >= 1)).result).toEqual(true)
})
it('matches non-existance', () => {
expect(pipe(new Set([1, 2])).then(Sets.every(x => x === 1)).result).toEqual(false)
})
test('invoke', () => {
expect(Sets.every(new Set([2, 4]), x => x % 2 === 0)).toEqual(true)
})
})

describe('contains', () => {
test('piped match', () => {
expect(pipe(new Set(['amy', 'bob'])).then(Sets.contains('bob')).result).toEqual(true)
Expand Down

0 comments on commit af5f2c0

Please sign in to comment.