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