Skip to content

Commit

Permalink
feat: add hasAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Oct 3, 2022
1 parent 400ed96 commit c330a0a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
62 changes: 62 additions & 0 deletions ts/hasAll.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import t from 'assert'
import a from 'assertron'
import { createSatisfier, hasAll } from './index.js'

test('non array returns false', () => {
a.false(createSatisfier(hasAll(1)).test(undefined))
a.false(createSatisfier(hasAll(1)).test(null))
a.false(createSatisfier(hasAll(1)).test(1))
a.false(createSatisfier(hasAll(1)).test(true))
a.false(createSatisfier(hasAll(1)).test('a'))
a.false(createSatisfier(hasAll({ a: 1 })).test(true))
a.false(createSatisfier(hasAll({ a: 1 })).test(1))
a.false(createSatisfier(hasAll({ a: 1 })).test('{ a: 1 }'))
a.false(createSatisfier(hasAll({ a: 1 })).test({ a: 1 }))
})

test('array with no match returns false', () => {
a.false(createSatisfier(hasAll(1)).test([2, 2, 3]))
a.false(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }]))
})

test('exact match array returns true', () => {
t(createSatisfier(hasAll(1)).test([1]))
t(createSatisfier(hasAll(false, false)).test([false, false]))
t(createSatisfier(hasAll('a', 'b')).test(['a', 'b']))
t(createSatisfier(hasAll({ a: 1 })).test([{ a: 1 }]))
t(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }, { a: 1 }]))
})

test('array with more than one match returns true', () => {
t(createSatisfier(hasAll({ a: 1 })).test([{ a: 1 }, { a: 1 }]))
t(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }, { a: 1 }, { a: 1 }, 'x']))
})

it('is true if all entries are in the target', () => {
t(createSatisfier(hasAll(1)).test([1, 2, 3]))
t(createSatisfier(hasAll(2)).test([1, 2, 3]))
t(createSatisfier(hasAll(3)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 2)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 3)).test([1, 2, 3]))
t(createSatisfier(hasAll(2, 3)).test([1, 2, 3]))
t(createSatisfier(hasAll(2, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(3, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(3, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 2, 3)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 3, 2)).test([1, 2, 3]))
t(createSatisfier(hasAll(2, 1, 3)).test([1, 2, 3]))
t(createSatisfier(hasAll(2, 3, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(3, 1, 2)).test([1, 2, 3]))
t(createSatisfier(hasAll(3, 2, 1)).test([1, 2, 3]))
})

it('is true even if there are duplicates in expectation', () => {
t(createSatisfier(hasAll(1, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 1, 2)).test([1, 2, 3]))
t(createSatisfier(hasAll(1, 2, 1)).test([1, 2, 3]))
t(createSatisfier(hasAll(2, 1, 1)).test([1, 2, 3]))
})

test('tersify()', () => {
t.strictEqual(hasAll({ a: 1 }, { b: 2 }).tersify(), 'hasAll({ a: 1 }, { b: 2 })')
})
12 changes: 12 additions & 0 deletions ts/hasAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { tersible, tersify } from 'tersify'
import { some } from './some.js'

/**
* Check if an array has all of the expected entries, regardless of order.
*/
export function hasAll(...expectations: any[]) {
return tersible((arr: any) => {
if (!Array.isArray(arr)) return false
return expectations.every(e => some(e)(arr))
}, () => `hasAll(${expectations.map(e => tersify(e)).join(', ')})`)
}
1 change: 1 addition & 0 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './createSatisfier.js'
export * from './every.js'
export * from './formatDiffs.js'
export * from './has.js'
export * from './hasAll.js'
export * from './interfaces.js'
export * from './isInInterval.js'
export * from './isInRange.js'
Expand Down

0 comments on commit c330a0a

Please sign in to comment.