forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop.js
108 lines (92 loc) · 3.02 KB
/
prop.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var fc = require('fast-check');
describe('prop', function() {
var fred = {name: 'Fred', age: 23};
it('returns a function that fetches the appropriate property', function() {
var nm = R.prop('name');
eq(typeof nm, 'function');
eq(nm(fred), 'Fred');
});
it('handles number as property', function() {
var deities = ['Cthulhu', 'Dagon', 'Yog-Sothoth'];
eq(R.prop(0, deities), 'Cthulhu');
eq(R.prop(1, deities), 'Dagon');
eq(R.prop(2, deities), 'Yog-Sothoth');
eq(R.prop(-1, deities), 'Yog-Sothoth');
});
it('shows the same behaviour as path for a nonexistent property', function() {
var propResult = R.prop('incorrect', fred);
var pathResult = R.path(['incorrect'], fred);
eq(propResult, pathResult);
});
it('shows the same behaviour as path for an undefined property', function() {
var propResult = R.prop(undefined, fred);
var pathResult = R.path([undefined], fred);
eq(propResult, pathResult);
});
it('shows the same behaviour as path for a null property', function() {
var propResult = R.prop(null, fred);
var pathResult = R.path([null], fred);
eq(propResult, pathResult);
});
it('shows the same behaviour as path for a valid property and object', function() {
var propResult = R.prop('age', fred);
var pathResult = R.path(['age'], fred);
eq(propResult, pathResult);
});
it('shows the same behaviour as path for a null object', function() {
var propResult = R.prop('age', null);
var pathResult = R.path(['age'], null);
eq(propResult, pathResult);
});
it('shows the same behaviour as path for an undefined object', function() {
var propResult, propException, pathResult, pathException;
try {
propResult = R.prop('name', undefined);
} catch (e) {
propException = e;
}
try {
pathResult = R.path(['name'], undefined);
} catch (e) {
pathException = e;
}
eq(propResult, pathResult);
eq(propException, pathException);
});
it('returns that value associated to a property given valid one', function() {
fc.assert(
fc.property(fc.string(), fc.anything(), function(p, value) {
const o = { [p]: value };
eq(R.prop(p, o), value);
})
);
});
it('shows the same behaviour as path on any object', function() {
fc.assert(
fc.property(fc.string(), fc.object(), function(p, o) {
eq(R.prop(p, o), R.path([p], o));
})
);
});
it('shows the same behaviour as path on any value', function() {
fc.assert(
fc.property(fc.string(), fc.anything(), function(p, o) {
var propResult, propException, pathResult, pathException;
try {
propResult = R.prop(p, o);
} catch (e) {
propException = e;
}
try {
pathResult = R.path([p], o);
} catch (e) {
pathException = e;
}
eq(propResult, pathResult);
eq(propException, pathException);
})
);
});
});