forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhas-methods.js
More file actions
205 lines (159 loc) · 6.79 KB
/
has-methods.js
File metadata and controls
205 lines (159 loc) · 6.79 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import assert from 'assert';
import lodashStable from 'lodash';
import { _, toArgs, stubTrue, args, symbol, defineProperty, stubFalse } from './utils.js';
describe('has methods', function() {
lodashStable.each(['has', 'hasIn'], function(methodName) {
var func = _[methodName],
isHas = methodName == 'has',
sparseArgs = toArgs([1]),
sparseArray = Array(1),
sparseString = Object('a');
delete sparseArgs[0];
delete sparseString[0];
it('`_.' + methodName + '` should check for own properties', function() {
var object = { 'a': 1 };
lodashStable.each(['a', ['a']], function(path) {
assert.strictEqual(func(object, path), true);
});
});
it('`_.' + methodName + '` should not use the `hasOwnProperty` method of `object`', function() {
var object = { 'hasOwnProperty': null, 'a': 1 };
assert.strictEqual(func(object, 'a'), true);
});
it('`_.' + methodName + '` should support deep paths', function() {
var object = { 'a': { 'b': 2 } };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
assert.strictEqual(func(object, path), true);
});
lodashStable.each(['a.a', ['a', 'a']], function(path) {
assert.strictEqual(func(object, path), false);
});
});
it('`_.' + methodName + '` should coerce `path` to a string', function() {
function fn() {}
fn.toString = lodashStable.constant('fn');
var object = { 'null': 1 , 'undefined': 2, 'fn': 3, '[object Object]': 4 },
paths = [null, undefined, fn, {}],
expected = lodashStable.map(paths, stubTrue);
lodashStable.times(2, function(index) {
var actual = lodashStable.map(paths, function(path) {
return func(object, index ? [path] : path);
});
assert.deepStrictEqual(actual, expected);
});
});
it('`_.' + methodName + '` should work with `arguments` objects', function() {
assert.strictEqual(func(args, 1), true);
});
it('`_.' + methodName + '` should work with a non-string `path`', function() {
var array = [1, 2, 3];
lodashStable.each([1, [1]], function(path) {
assert.strictEqual(func(array, path), true);
});
});
it('`_.' + methodName + '` should preserve the sign of `0`', function() {
var object = { '-0': 'a', '0': 'b' },
props = [-0, Object(-0), 0, Object(0)],
expected = lodashStable.map(props, stubTrue);
var actual = lodashStable.map(props, function(key) {
return func(object, key);
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should work with a symbol `path`', function() {
function Foo() {}
if (Symbol) {
Foo.prototype[symbol] = 1;
var symbol2 = Symbol('b');
defineProperty(Foo.prototype, symbol2, {
'configurable': true,
'enumerable': false,
'writable': true,
'value': 2
});
var object = isHas ? Foo.prototype : new Foo;
assert.strictEqual(func(object, symbol), true);
assert.strictEqual(func(object, symbol2), true);
}
});
it('`_.' + methodName + '` should check for a key over a path', function() {
var object = { 'a.b': 1 };
lodashStable.each(['a.b', ['a.b']], function(path) {
assert.strictEqual(func(object, path), true);
});
});
it('`_.' + methodName + '` should return `true` for indexes of sparse values', function() {
var values = [sparseArgs, sparseArray, sparseString],
expected = lodashStable.map(values, stubTrue);
var actual = lodashStable.map(values, function(value) {
return func(value, 0);
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should return `true` for indexes of sparse values with deep paths', function() {
var values = [sparseArgs, sparseArray, sparseString],
expected = lodashStable.map(values, lodashStable.constant([true, true]));
var actual = lodashStable.map(values, function(value) {
return lodashStable.map(['a[0]', ['a', '0']], function(path) {
return func({ 'a': value }, path);
});
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should return `' + (isHas ? 'false' : 'true') + '` for inherited properties', function() {
function Foo() {}
Foo.prototype.a = 1;
lodashStable.each(['a', ['a']], function(path) {
assert.strictEqual(func(new Foo, path), !isHas);
});
});
it('`_.' + methodName + '` should return `' + (isHas ? 'false' : 'true') + '` for nested inherited properties', function() {
function Foo() {}
Foo.prototype.a = { 'b': 1 };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
assert.strictEqual(func(new Foo, path), !isHas);
});
});
it('`_.' + methodName + '` should return `false` when `object` is nullish', function() {
var values = [null, undefined],
expected = lodashStable.map(values, stubFalse);
lodashStable.each(['constructor', ['constructor']], function(path) {
var actual = lodashStable.map(values, function(value) {
return func(value, path);
});
assert.deepStrictEqual(actual, expected);
});
});
it('`_.' + methodName + '` should return `false` for deep paths when `object` is nullish', function() {
var values = [null, undefined],
expected = lodashStable.map(values, stubFalse);
lodashStable.each(['constructor.prototype.valueOf', ['constructor', 'prototype', 'valueOf']], function(path) {
var actual = lodashStable.map(values, function(value) {
return func(value, path);
});
assert.deepStrictEqual(actual, expected);
});
});
it('`_.' + methodName + '` should return `false` for nullish values of nested objects', function() {
var values = [, null, undefined],
expected = lodashStable.map(values, stubFalse);
lodashStable.each(['a.b', ['a', 'b']], function(path) {
var actual = lodashStable.map(values, function(value, index) {
var object = index ? { 'a': value } : {};
return func(object, path);
});
assert.deepStrictEqual(actual, expected);
});
});
it('`_.' + methodName + '` should return `false` over sparse values of deep paths', function() {
var values = [sparseArgs, sparseArray, sparseString],
expected = lodashStable.map(values, lodashStable.constant([false, false]));
var actual = lodashStable.map(values, function(value) {
return lodashStable.map(['a[0].b', ['a', '0', 'b']], function(path) {
return func({ 'a': value }, path);
});
});
assert.deepStrictEqual(actual, expected);
});
});
});