forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconforms-methods.js
153 lines (119 loc) · 4.28 KB
/
conforms-methods.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
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
import assert from 'assert';
import lodashStable from 'lodash';
import { _, stubFalse, stubTrue, empties } from './utils.js';
import conformsTo from '../conformsTo.js';
describe('conforms methods', function() {
lodashStable.each(['conforms', 'conformsTo'], function(methodName) {
var isConforms = methodName == 'conforms';
function conforms(source) {
return isConforms ? _.conforms(source) : function(object) {
return conformsTo(object, source);
};
}
it('`_.' + methodName + '` should check if `object` conforms to `source`', function() {
var objects = [
{ 'a': 1, 'b': 8 },
{ 'a': 2, 'b': 4 },
{ 'a': 3, 'b': 16 }
];
var par = conforms({
'b': function(value) { return value > 4; }
});
var actual = lodashStable.filter(objects, par);
assert.deepStrictEqual(actual, [objects[0], objects[2]]);
par = conforms({
'b': function(value) { return value > 8; },
'a': function(value) { return value > 1; }
});
actual = lodashStable.filter(objects, par);
assert.deepStrictEqual(actual, [objects[2]]);
});
it('`_.' + methodName + '` should not match by inherited `source` properties', function() {
function Foo() {
this.a = function(value) {
return value > 1;
};
}
Foo.prototype.b = function(value) {
return value > 8;
};
var objects = [
{ 'a': 1, 'b': 8 },
{ 'a': 2, 'b': 4 },
{ 'a': 3, 'b': 16 }
];
var par = conforms(new Foo),
actual = lodashStable.filter(objects, par);
assert.deepStrictEqual(actual, [objects[1], objects[2]]);
});
it('`_.' + methodName + '` should not invoke `source` predicates for missing `object` properties', function() {
var count = 0;
var par = conforms({
'a': function() { count++; return true; }
});
assert.strictEqual(par({}), false);
assert.strictEqual(count, 0);
});
it('`_.' + methodName + '` should work with a function for `object`', function() {
function Foo() {}
Foo.a = 1;
function Bar() {}
Bar.a = 2;
var par = conforms({
'a': function(value) { return value > 1; }
});
assert.strictEqual(par(Foo), false);
assert.strictEqual(par(Bar), true);
});
it('`_.' + methodName + '` should work with a function for `source`', function() {
function Foo() {}
Foo.a = function(value) { return value > 1; };
var objects = [{ 'a': 1 }, { 'a': 2 }],
actual = lodashStable.filter(objects, conforms(Foo));
assert.deepStrictEqual(actual, [objects[1]]);
});
it('`_.' + methodName + '` should work with a non-plain `object`', function() {
function Foo() {
this.a = 1;
}
Foo.prototype.b = 2;
var par = conforms({
'b': function(value) { return value > 1; }
});
assert.strictEqual(par(new Foo), true);
});
it('`_.' + methodName + '` should return `false` when `object` is nullish', function() {
var values = [, null, undefined],
expected = lodashStable.map(values, stubFalse);
var par = conforms({
'a': function(value) { return value > 1; }
});
var actual = lodashStable.map(values, function(value, index) {
try {
return index ? par(value) : par();
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should return `true` when comparing an empty `source` to a nullish `object`', function() {
var values = [, null, undefined],
expected = lodashStable.map(values, stubTrue),
par = conforms({});
var actual = lodashStable.map(values, function(value, index) {
try {
return index ? par(value) : par();
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should return `true` when comparing an empty `source`', function() {
var object = { 'a': 1 },
expected = lodashStable.map(empties, stubTrue);
var actual = lodashStable.map(empties, function(value) {
var par = conforms(value);
return par(object);
});
assert.deepStrictEqual(actual, expected);
});
});
});