forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasPath.js
81 lines (65 loc) · 2.38 KB
/
hasPath.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('hasPath', function() {
var obj = {
objVal: {b: {c: 'c'}},
falseVal: false,
nullVal: null,
undefinedVal: undefined,
arrayVal: ['arr']
};
it('returns true for existing path', function() {
eq(R.hasPath(['objVal'], obj), true);
eq(R.hasPath(['objVal', 'b'], obj), true);
eq(R.hasPath(['objVal', 'b', 'c'], obj), true);
eq(R.hasPath(['arrayVal'], obj), true);
});
it('returns true for existing path to falsy values', function() {
eq(R.hasPath(['falseVal'], obj), true);
eq(R.hasPath(['nullVal'], obj), true);
eq(R.hasPath(['undefinedVal'], obj), true);
});
it('return false for a test for a child to a non-object', function() {
eq(R.hasPath(['undefinedVal', 'child', 'grandchild'], obj), false);
eq(R.hasPath(['falseVal', 'child', 'grandchild'], obj), false);
eq(R.hasPath(['nullVal', 'child', 'grandchild'], obj), false);
eq(R.hasPath(['arrayVal', 0, 'child', 'grandchild'], obj), false);
});
it('returns true for existing path with indexes', function() {
eq(R.hasPath(['arrayVal', 0], obj), true);
});
it('returns false for non-existing path with indexes', function() {
eq(R.hasPath(['arrayVal', 1], obj), false);
});
it('tests for paths in arrays', function() {
eq(R.hasPath([0], [1, 2]), true);
eq(R.hasPath([2], [1, 2]), false);
eq(R.hasPath(['0'], [1, 2]), true);
eq(R.hasPath(['2'], [1, 2]), false);
});
it('returns false for non-existent path', function() {
eq(R.hasPath(['Unknown'], obj), false);
eq(R.hasPath(['objVal', 'Unknown'], obj), false);
});
it('does not check properties from the prototype chain', function() {
var Person = function() {};
Person.prototype.age = {x: 1};
var bob = new Person();
eq(R.hasPath(['age'], bob), false);
eq(R.hasPath(['age', 'x'], bob), false);
eq(R.hasPath(['toString'], bob), false);
});
it('returns false for non-objects', function() {
eq(R.hasPath([], obj), false);
});
it('tests paths on non-objects', function() {
eq(R.hasPath(['a', 'b'], undefined), false);
eq(R.hasPath(['a', 'b'], null), false);
eq(R.hasPath('a', true), false);
eq(R.hasPath('a', ''), false);
eq(R.hasPath('a', /a/), false);
});
it('tests currying', function() {
eq(R.hasPath(['a', 'b'])({ a: { b: 1 } }), true);
});
});