forked from jonschlinkert/deep-filter-object
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
58 lines (46 loc) · 2.13 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*!
* deep-filter-object <https://github.com/jonschlinkert/deep-filter-object>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License
*/
'use strict';
var assert = require('assert');
var should = require('should');
var deepFilter = require('./');
describe('deep filter', function () {
it('should use a callback function for filtering:', function () {
deepFilter({a: 'a', b: 'b', c: 'c'}, function (value, key, obj, i) {
return key === 'b';
}).should.eql({b: 'b'});
deepFilter({a: 'a', b: {c: 'c', d: {e: {c: 'c', f: 'f'}}}, c: 'c'}, function (value, key, obj) {
return key !== 'c';
}).should.eql({a: 'a', b: {d: {e: {f: 'f'}}}});
});
it('should deeply filter keys using the given glob patterns', function () {
var obj1 = deepFilter({a: 'a', b: {a: 'a', b: {a: 'a', b: 'b', c: 'c'}}}, '*');
obj1.should.eql({a: 'a', b: {a: 'a', b: {a: 'a', b: 'b', c: 'c'}}});
var obj2 = deepFilter({a: 'a', b: {a: 'a', b: {a: 'a', b: 'b', c: 'c'}}}, 'b');
obj2.should.eql({b: {b: {b: 'b'}}});
var obj3 = deepFilter({foo: 'a', bar: {foo: 'a', bar: {foo: 'a', bar: 'b', baz: 'c'}}}, 'b*');
obj3.should.eql({bar: {bar: {bar: 'b', baz: 'c'}}});
});
it('should deeply exclude keys that match negation patterns:', function () {
var obj1 = deepFilter({a: 'a', b: {a: 'a', b: {a: 'a', b: 'b', c: 'c'}}}, ['*', '!a']);
obj1.should.eql({b: {b: {b: 'b', c: 'c'}}});
var obj2 = deepFilter({foo: 'a', bar: {foo: 'a', bar: {foo: 'a', bar: 'b', baz: 'c'}}}, ['*', '!foo']);
obj2.should.eql({bar: {bar: {bar: 'b', baz: 'c'}}});
});
it('should deeply filter keys using brace expansion', function () {
var obj1 = deepFilter({a: 'a', b: {a: 'a', b: {a: 'a', b: 'b', c: 'c'}}}, '{b,c}');
obj1.should.eql({b: {b: {b: 'b', c: 'c'}}});
});
it('should return the entire object if no pattern is given:', function () {
deepFilter({a: 'a', b: 'b', c: 'c'}).should.eql({a: 'a', b: 'b', c: 'c'});
});
it('should throw an error if an object is not passed:', function () {
(function () {
deepFilter()
}).should.throw('deep-filter-object expects an object');
});
});