Skip to content

Instantly share code, notes, and snippets.

@sadasant
Last active March 19, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadasant/4584ec3e76cfc6736838d459be314e7c to your computer and use it in GitHub Desktop.
Save sadasant/4584ec3e76cfc6736838d459be314e7c to your computer and use it in GitHub Desktop.
const S = require('sanctuary')
let objectMatches = object => S.allPass(
S.map(([k, v]) =>
S.pipe([
S.prop(k),
S.is(Function, v) ?
v :
S.is(RegExp, v) ?
S.test(v) :
S.equals(v)
]),
S.pairs(object)
)
)
test('objectMatches', async () => {
expect(objectMatches({ a: 1 })({ a: 1 })).toEqual(true)
expect(objectMatches({ a: 2 })({ a: 1 })).toEqual(false)
expect(objectMatches({ a: 1 })({ a: 1, b: 2 })).toEqual(true)
expect(objectMatches({ a: /a/ })({ a: 'a' })).toEqual(true)
expect(objectMatches({ a: S.lt(2) })({ a: 1 })).toEqual(true)
expect(objectMatches({ a: S.lt(2) })({ a: 2 })).toEqual(false)
expect(objectMatches({ a: S.lt(3), b: S.equals(3) })({ a: 2, b: 3 })).toEqual(true)
expect(objectMatches({ a: S.lt(3), b: S.equals(3) })({ a: 2, b: 1 })).toEqual(false)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment