-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
132 lines (106 loc) · 3.31 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*!
* unset <https://github.com/jonschlinkert/unset>
*
* Copyright (c) 2015 .
* Licensed under the MIT license.
*/
'use strict';
/* deps: mocha */
var assert = require('assert');
var should = require('should');
var unset = require('./');
describe('unset', function () {
it('should update the given object when a property is deleted:', function () {
var obj = {a: 'b'};
unset(obj, 'a');
obj.should.eql({});
});
it('should return true when a property is deleted:', function () {
unset({a: 'b'}, 'a').should.eql(true);
});
it('should return true when the given property does not exist:', function () {
unset({a: 'b'}, 'z').should.eql(true);
});
it('should delete nested values:', function () {
var one = {a: {b: {c: 'd'}}};
unset(one, 'a.b');
one.should.eql({a: {}});
var two = {a: {b: {c: 'd'}}};
unset(two, 'a.b.c');
two.should.eql({a: {b: {}}});
var three = {a: {b: {c: 'd', e: 'f'}}};
unset(three, 'a.b.c');
three.should.eql({a: {b: {e: 'f'}}});
});
it('should delete...:', function () {
var three = {'a.b': 'c', d: 'e'};
unset(three, 'a.b');
three.should.eql({d: 'e'});
});
it('should delete nested escaped values:', function () {
var one = {a: {'b.c': 'd'}};
unset(one, 'a.b\\.c');
one.should.eql({a: {}});
var two = {'a.b.c': 'd'};
unset(two, 'a\\.b\\.c');
two.should.eql({});
var three = {'a.b': 'c', d: 'e'};
unset(three, 'a\\.b');
three.should.eql({d: 'e'});
});
it('should throw an error when invalid args are passed:', function () {
(function () {
unset();
}).should.throw('expected an object.');
});
});
describe('input path as array paths', function () {
it('should update the given object when a property is deleted:', function () {
var obj = {a: 'b'};
unset(obj, ['a']);
obj.should.eql({});
});
it('should return true when a property is deleted:', function () {
unset({a: 'b'}, ['a']).should.eql(true);
});
it('should return true when the given property does not exist:', function () {
unset({a: 'b'}, ['z']).should.eql(true);
});
it('should delete nested values:', function () {
var one = {a: {b: {c: 'd'}}};
unset(one, ['a', 'b']);
one.should.eql({a: {}});
var two = {a: {b: {c: 'd'}}};
unset(two, ['a', 'b', 'c']);
two.should.eql({a: {b: {}}});
var three = {a: {b: {c: 'd', e: 'f'}}};
unset(three, ['a', 'b', 'c']);
three.should.eql({a: {b: {e: 'f'}}});
});
it('should delete...:', function () {
var three = {'a.b': 'c', d: 'e'};
unset(three, ['a.b']);
three.should.eql({d: 'e'});
});
it('should delete nested escaped values:', function () {
var one = {a: {'b.c': 'd'}};
unset(one, ['a', 'b.c']);
one.should.eql({a: {}});
var two = {'a.b.c': 'd'};
unset(two, ['a.b.c']);
two.should.eql({});
var three = {'a.b': 'c', d: 'e'};
unset(three, ['a.b']);
three.should.eql({d: 'e'});
});
it('should throw an error when invalid args are passed:', function () {
(function () {
unset();
}).should.throw('expected an object.');
});
it('should not allow unsetting unsafe values', () => {
assert.throws(() => unset({}, '__proto__.toString'));
assert.throws(() => unset(()=>{}, 'prototype'));
assert.throws(() => unset(()=>{}, 'contructor'));
});
});