forked from jonschlinkert/omit-deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
85 lines (70 loc) · 2.68 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
require('mocha');
var assert = require('assert');
var omitDeep = require('./');
describe('.omit()', function() {
it('should return the object if it is not a plain object', function() {
var date = new Date();
var obj = omitDeep(date, 'foo');
assert(obj === date);
assert.deepEqual(obj, date);
});
it('should return the value if it is not a plain object', function() {
assert.deepEqual(omitDeep('foo'), 'foo');
assert.deepEqual(omitDeep(42), 42);
});
it('should omit keys using dot notation', function() {
var obj = omitDeep({a: {b: {c: {d: {e: 'e'}, f: {g: 'g'}}}}}, 'a.b.c.d');
assert.deepEqual(obj, {a: {b: {c: {f: {g: 'g'}}}}});
});
it('should omit multiple keys using dot notation', function() {
var obj = omitDeep({a: {x: 'y', b: {c: {d: {e: 'e'}, f: {g: 'g'}}}}}, ['a.b.c.d', 'a.x']);
assert.deepEqual(obj, {a: {b: {c: {f: {g: 'g'}}}}});
});
it('should recursively omit key passed as a string.', function() {
var obj = omitDeep({a: 'a', b: 'b', c: {b: 'b', d: 'd', e: {b: 'b', f: 'f', g: {b: 'b', c: 'c'}}}}, 'b');
assert.deepEqual(obj, {a: 'a', c: {d: 'd', e: {f: 'f', g: {c: 'c'}}}});
});
it('should recursively omit key passed as an array.', function() {
var obj = omitDeep({a: 'a', b: 'b', c: {b: 'b', d: 'd', e: {b: 'b', f: 'f', g: {b: 'b', c: 'c'}}}}, ['b']);
assert.deepEqual(obj, {a: 'a', c: {d: 'd', e: {f: 'f', g: {c: 'c'}}}});
});
it('should recursively omit multiple keys.', function() {
var obj = omitDeep({a: 'a', b: 'b', c: {b: 'b', d: 'd', e: {b: 'b', f: 'f', g: {b: 'b', c: 'c'}}}}, ['b', 'd', 'f']);
assert.deepEqual(obj, {a: 'a', c: {e: {g: {c: 'c'}}}});
});
it('should omit the given keys.', function() {
assert.deepEqual(omitDeep({a: 'a', b: 'b', c: 'c'}, ['a', 'c']), { b: 'b' });
});
it('should return the object if no keys are specified.', function() {
assert.deepEqual(omitDeep({a: 'a', b: 'b', c: 'c'}), {a: 'a', b: 'b', c: 'c'});
});
it('should return an empty object if no object is specified.', function() {
assert.deepEqual(omitDeep(), {});
});
it('should return the input unchaged if not an array or an object', function() {
assert.deepEqual(omitDeep('foo'), 'foo');
});
it('should omit keys from objects in arrays', function() {
var obj = omitDeep([
{a: 'a', b: 'b'},
[
{a: 'a', b: 'b'}
]
], 'b');
assert.deepEqual(obj, [
{a: 'a'},
[
{a: 'a'}
]
]);
});
it('should preserve arrays when not omitting objects from them', function() {
var obj = omitDeep({
'numbers': ['1', '2']
}, 'nothing');
assert.deepEqual(obj, {
'numbers': ['1', '2']
});
});
});