Last active
March 19, 2018 14:55
-
-
Save sadasant/4584ec3e76cfc6736838d459be314e7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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