2017-04-19 00:55:46 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import u from '../lib'
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
describe('u.is', () => {
|
|
|
|
it('returns true if path matches a value predicate', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b', 4, { a: { b: 4 } })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('returns true if path matches a function predicate', () => {
|
2020-04-02 14:16:13 +00:00
|
|
|
const isEven = (x) => x % 2 === 0
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b', isEven, { a: { b: 6 } })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('returns false if path matches a value predicate', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b', 4, { a: { b: 5 } })
|
|
|
|
expect(result).to.be.false
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('returns false if path matches a function predicate', () => {
|
2020-04-02 14:16:13 +00:00
|
|
|
const isEven = (x) => x % 2 === 0
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b', isEven, { a: { b: 7 } })
|
|
|
|
expect(result).to.be.false
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('returns false if the path does not exist', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b.c.d', 4, { a: { b: {} } })
|
|
|
|
expect(result).to.be.false
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('can test for undefined', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b.c', undefined, { a: { b: {} } })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('tests the actual object if a blank path is given', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('', 4, 4)
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('can use arrays as paths', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is(['a', 'b'], 4, { a: { b: 4 } })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('can include array indexes in paths', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
let result = u.is('a.1.b', 4, { a: [{}, { b: 4 }] })
|
|
|
|
expect(result).to.be.true
|
2015-08-07 03:48:04 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
result = u.is(['a', 1, 'b'], 4, { a: [{}, { b: 4 }] })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
2015-08-07 03:48:04 +00:00
|
|
|
|
|
|
|
it('can be partially applied', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const result = u.is('a.b')(4)({ a: { b: 4 } })
|
|
|
|
expect(result).to.be.true
|
|
|
|
})
|
|
|
|
})
|