forked from angus-c/just
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (92 loc) · 2.81 KB
/
Copy pathindex.js
File metadata and controls
100 lines (92 loc) · 2.81 KB
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
var test = require('../util/test')(__filename);
var set = require('../../packages/object-safe-set');
var compare = require('../../packages/collection-compare');
test('sets existing property using dot-notation arg', function(t) {
t.plan(4);
var obj1 = {a: {aa: {aaa: 2}}};
t.isEqual(set(obj1, 'a.aa.aaa', 3), true);
t.ok(compare(obj1, {a: {aa: {aaa: 3}}}));
var obj2 = {a: {aa: {aaa: 2}}};
t.isEqual(set(obj2, 'a.aa', {bbb: 7}), true);
t.ok(compare(obj2, {a: {aa: {bbb: 7}}}));
t.end();
});
test('sets existing property using array arg', function(t) {
t.plan(4);
var obj1 = {a: {aa: {aaa: 2}}};
t.isEqual(set(obj1, ['a', 'aa', 'aaa'], 3), true);
t.ok(compare(obj1, {a: {aa: {aaa: 3}}}));
var obj2 = {a: {aa: {aaa: 2}}};
t.isEqual(set(obj2, ['a', 'aa'], {bbb: 7}), true);
t.ok(compare(obj2, {a: {aa: {bbb: 7}}}));
t.end();
});
test('sets non-existent property using dot-notation arg', function(t) {
t.plan(4);
var obj1 = {};
t.isEqual(set(obj1, 'a.aa.aaa', 4), true);
t.ok(compare(obj1, {a: {aa: {aaa: 4}}}));
var obj2 = {};
t.isEqual(set(obj2, 'a.aa', {bbb: 7}), true);
t.ok(compare(obj2, {a: {aa: {bbb: 7}}}));
t.end();
});
test('sets non-existent property using array arg', function(t) {
t.plan(4);
var obj1 = {};
t.isEqual(set(obj1, ['a', 'aa', 'aaa'], 4), true);
t.ok(compare(obj1, {a: {aa: {aaa: 4}}}));
var obj2 = {};
t.isEqual(set(obj2, ['a', 'aa'], {bbb: 7}), true);
t.ok(compare(obj2, {a: {aa: {bbb: 7}}}));
t.end();
});
test("doesn't interrupt property chain, using dot-notation arg", function(t) {
t.plan(2);
var obj1 = {a: 5};
t.isEqual(set(obj1, 'a.aa.aaa', 4), false);
// ok to clobber last property
var obj2 = {a: {aa: 9}};
t.isEqual(set(obj2, 'a.aa', {bbb: 7}), true);
t.end();
});
test("doesn't interrupt property chain, using array arg", function(t) {
t.plan(2);
var obj1 = {a: 5};
t.isEqual(set(obj1, ['a', 'aa', 'aaa'], 4), false);
// ok to clobber last property
var obj2 = {a: {aa: 9}};
t.isEqual(set(obj2, ['a', 'aa'], {bbb: 7}), true);
t.end();
});
test("doesn't support setting of prototype (and related) values", function(t) {
t.plan(4);
t.throws(function() {
var obj1 = {a: {}};
set(obj1, '__proto__.x', function malice() {});
});
t.throws(function() {
var obj1 = {a: {}};
set(obj1, ['a', 'b', '__proto__'], {toString: 'hehehe'});
});
t.throws(function() {
var obj2 = {a: {}};
set(obj2, 'constructor', function FakeConstructor() {});
});
t.throws(function() {
var obj3 = {a: {}};
set(obj3, 'prototype.y', 'hahahaha');
});
t.end();
});
/* eslint-disable no-undef*/
if (typeof Symbol === 'function') {
test('supports symbol prop', function(t) {
t.plan(2);
var obj1 = {a: {}};
var sym = Symbol();
t.isEqual(set(obj1.a, sym, 7), true);
t.ok(obj1.a[sym] === 7);
t.end();
});
}