forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropOr.js
74 lines (60 loc) · 2.58 KB
/
propOr.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('propOr', function() {
var fred = {name: 'Fred', age: 23};
var anon = {age: 99};
var nm = R.propOr('Unknown', 'name');
it('returns a function that fetches the appropriate property', function() {
eq(typeof nm, 'function');
eq(nm(fred), 'Fred');
});
it('returns the default value when the property does not exist', function() {
eq(nm(anon), 'Unknown');
});
it('returns the default value when the object is nil', function() {
eq(nm(null), 'Unknown');
eq(nm(void 0), 'Unknown');
});
it('uses the default when supplied an object with a nil value', function() {
eq(R.propOr('foo', 'x', {x: null}), 'foo');
eq(R.propOr('foo', 'x', {x: undefined}), 'foo');
});
it('handles number as property', function() {
var deities = ['Cthulhu', 'Dagon', 'Yog-Sothoth'];
eq(R.propOr('Unknown', 0, deities), 'Cthulhu');
eq(R.propOr('Unknown', 1, deities), 'Dagon');
eq(R.propOr('Unknown', 2, deities), 'Yog-Sothoth');
eq(R.propOr('Unknown', -1, deities), 'Yog-Sothoth');
eq(R.propOr('Unknown', 3, deities), 'Unknown');
});
it('shows the same behaviour as pathOr for a nonexistent property', function() {
var propOrResult = R.propOr('Unknown', 'incorrect', fred);
var pathOrResult = R.pathOr('Unknown', ['incorrect'], fred);
eq(propOrResult, pathOrResult);
});
it('shows the same behaviour as pathOr for an undefined property', function() {
var propOrResult = R.propOr('Unknown', undefined, fred);
var pathOrResult = R.pathOr('Unknown', [undefined], fred);
eq(propOrResult, pathOrResult);
});
it('shows the same behaviour as pathOr for a null property', function() {
var propOrResult = R.propOr('Unknown', null, fred);
var pathOrResult = R.pathOr('Unknown', [null], fred);
eq(propOrResult, pathOrResult);
});
it('shows the same behaviour as pathOr for a valid property and object', function() {
var propOrResult = R.propOr('Unknown', 'age', fred);
var pathOrResult = R.pathOr('Unknown', ['age'], fred);
eq(propOrResult, pathOrResult);
});
it('shows the same behaviour as pathOr for a null object', function() {
var propOrResult = R.propOr('Unknown', 'age', null);
var pathOrResult = R.pathOr('Unknown', ['age'], null);
eq(propOrResult, pathOrResult);
});
it('shows the same behaviour as pathOr for an undefined object', function() {
var propOrResult = R.propOr('Unknown', 'age', undefined);
var pathOrResult = R.pathOr('Unknown', ['age'], undefined);
eq(propOrResult, pathOrResult);
});
});