function.predicates.js
14.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
$(document).ready(function() {
module("underscore.function.predicates");
test("isInstanceOf", function() {
equal(_.isInstanceOf([], Array), true, 'should identify arrays');
equal(_.isInstanceOf(null, Array), false, 'should identify that null is not an array instance');
});
test("isAssociative", function() {
equal(_.isAssociative({}), true, 'should identify that a map is associative');
equal(_.isAssociative(function(){}), true, 'should identify that a function is associative');
equal(_.isAssociative([]), true, 'should identify that an array is associative');
equal(_.isAssociative(new Array(10)), true, 'should identify that an array is associative');
equal(_.isAssociative(1), false, 'should identify non-associative things');
equal(_.isAssociative(0), false, 'should identify non-associative things');
equal(_.isAssociative(-1), false, 'should identify non-associative things');
equal(_.isAssociative(3.14), false, 'should identify non-associative things');
equal(_.isAssociative('undefined'), false, 'should identify non-associative things');
equal(_.isAssociative(''), false, 'should identify non-associative things');
equal(_.isAssociative(NaN), false, 'should identify non-associative things');
equal(_.isAssociative(Infinity), false, 'should identify non-associative things');
equal(_.isAssociative(true), false, 'should identify non-associative things');
});
test("isIndexed", function() {
equal(_.isIndexed([]), true, 'should identify indexed objects');
equal(_.isIndexed([1,2,3]), true, 'should identify indexed objects');
equal(_.isIndexed(new Array(10)), true, 'should identify indexed objects');
equal(_.isIndexed(""), true, 'should identify indexed objects');
equal(_.isIndexed("abc"), true, 'should identify indexed objects');
equal(_.isIndexed(1), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(0), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(-1), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(3.14), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(undefined), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(NaN), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(Infinity), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(true), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(false), false, 'should identify when something is not an indexed object');
equal(_.isIndexed(function(){}), false, 'should identify when something is not an indexed object');
});
test("isSequential", function() {
equal(_.isSequential(new Array(10)), true, 'should identify sequential objects');
equal(_.isSequential([1,2]), true, 'should identify sequential objects');
equal(_.isSequential(arguments), true, 'should identify sequential objects');
equal(_.isSequential({}), false, 'should identify when something is not sequential');
equal(_.isSequential(function(){}), false, 'should identify when something is not sequential');
equal(_.isSequential(1), false, 'should identify when something is not sequential');
equal(_.isSequential(0), false, 'should identify when something is not sequential');
equal(_.isSequential(-1), false, 'should identify when something is not sequential');
equal(_.isSequential(3.14), false, 'should identify when something is not sequential');
equal(_.isSequential('undefined'), false, 'should identify when something is not sequential');
equal(_.isSequential(''), false, 'should identify when something is not sequential');
equal(_.isSequential(NaN), false, 'should identify when something is not sequential');
equal(_.isSequential(Infinity), false, 'should identify when something is not sequential');
equal(_.isSequential(true), false, 'should identify when something is not sequential');
});
test("isPlainObject", function() {
function SomeConstructor() {}
equal(_.isPlainObject({}), true, 'should identify empty objects');
equal(_.isPlainObject({a: 1, b: 2}), true, 'should identify objects');
equal(_.isPlainObject(Object.create(null)), false, 'should reject objects with no prototype');
equal(_.isPlainObject(new SomeConstructor), false, 'should reject instances constructed by something other than Object');
equal(_.isPlainObject([]), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(function(){}), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(null), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(1), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(0), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(-1), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(3.14), false, 'should identify when something is not a plain object');
equal(_.isPlainObject('undefined'), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(''), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(NaN), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(Infinity), false, 'should identify when something is not a plain object');
equal(_.isPlainObject(true), false, 'should identify when something is not a plain object');
});
test("isEven", function() {
equal(_.isEven(0), true, 'should identify even numbers');
equal(_.isEven(2), true, 'should identify even numbers');
equal(_.isEven(-2), true, 'should identify even numbers');
equal(_.isEven(1), false, 'should identify non-even numbers');
equal(_.isEven(null), false, 'should return false for non-numbers');
equal(_.isEven(undefined), false, 'should return false for non-numbers');
equal(_.isEven([]), false, 'should return false for non-numbers');
equal(_.isEven(NaN), false, 'should return false for non-numbers');
});
test("isOdd", function() {
equal(_.isOdd(1), true, 'should identify odd numbers');
equal(_.isOdd(33), true, 'should identify odd numbers');
equal(_.isOdd(-55), true, 'should identify odd numbers');
equal(_.isOdd(10), false, 'should identify non-odd numbers');
equal(_.isOdd(null), false, 'should return false for non-numbers');
equal(_.isOdd(undefined), false, 'should return false for non-numbers');
equal(_.isOdd([]), false, 'should return false for non-numbers');
equal(_.isOdd(NaN), false, 'should return false for non-numbers');
});
test("isPositive", function() {
equal(_.isPositive(1), true, 'should identify positive numbers');
equal(_.isPositive(-1), false, 'should identify non-positive numbers');
equal(_.isPositive(0), false, 'should identify non-positive numbers');
equal(_.isPositive(+0), false, 'should identify non-positive numbers');
});
test("isNegative", function() {
equal(_.isNegative(-1), true, 'should identify negative numbers');
equal(_.isNegative(0), false, 'should identify non-negative numbers');
equal(_.isNegative(110), false, 'should identify non-negative numbers');
equal(_.isNegative(-0), false, 'should identify non-negative numbers');
});
test("isZero", function() {
equal(_.isZero(0), true, 'should know zero');
equal(_.isZero(-0), true, 'should know zero');
equal(_.isZero(+0), true, 'should know zero');
equal(_.isZero(1), false, 'should know non-zero');
equal(_.isZero(-1), false, 'should know non-zero');
});
test("isNumeric", function() {
// Integer Literals
equal(_.isNumeric("-10"), true, "should identify Negative integer string");
equal(_.isNumeric("0"), true, "should identify Zero string");
equal(_.isNumeric("5"), true, "should identify Positive integer string");
equal(_.isNumeric(-16), true, "should identify Negative integer number");
equal(_.isNumeric(0), true, "should identify Zero integer number");
equal(_.isNumeric(32), true, "should identify Positive integer number");
equal(_.isNumeric("040"), true, "should identify Octal integer literal string");
equal(_.isNumeric(0144), true, "should identify Octal integer literal");
equal(_.isNumeric("0xFF"), true, "should identify Hexadecimal integer literal string");
equal(_.isNumeric(0xFFF), true, "should identify Hexadecimal integer literal");
// Foating-Point Literals
equal(_.isNumeric("-1.6"), true, "should identify Negative floating point string");
equal(_.isNumeric("4.536"), true, "should identify Positive floating point string");
equal(_.isNumeric(-2.6), true, "should identify Negative floating point number");
equal(_.isNumeric(3.1415), true, "should identify Positive floating point number");
equal(_.isNumeric(8e5), true, "should identify Exponential notation");
equal(_.isNumeric("123e-2"), true, "should identify Exponential notation string");
// Non-Numeric values
equal(_.isNumeric(""), false, "should identify Empty string");
equal(_.isNumeric(" "), false, "should identify Whitespace characters string");
equal(_.isNumeric("\t\t"), false, "should identify Tab characters string");
equal(_.isNumeric("abcdefghijklm1234567890"), false, "should identify Alphanumeric character string");
equal(_.isNumeric("xabcdefx"), false, "should identify Non-numeric character string");
equal(_.isNumeric(true), false, "should identify Boolean true literal");
equal(_.isNumeric(false), false, "should identify Boolean false literal");
equal(_.isNumeric("bcfed5.2"), false, "should identify Number with preceding non-numeric characters");
equal(_.isNumeric("7.2acdgs"), false, "should identify Number with trailling non-numeric characters");
equal(_.isNumeric(undefined), false, "should identify Undefined value");
equal(_.isNumeric(null), false, "should identify Null value");
equal(_.isNumeric(NaN), false, "should identify NaN value");
equal(_.isNumeric(Infinity), false, "should identify Infinity primitive");
equal(_.isNumeric(Number.POSITIVE_INFINITY), false, "should identify Positive Infinity");
equal(_.isNumeric(Number.NEGATIVE_INFINITY), false, "should identify Negative Infinity");
equal(_.isNumeric(new Date(2009,1,1)), false, "should identify Date object");
equal(_.isNumeric({}), false, "should identify Empty object");
equal(_.isNumeric(function(){}), false, "should identify Instance of a function");
});
test("isInteger and isFloat", function() {
var integerChecks = [
{value: "-10", message: "should identify Negative integer string"},
{value: "0", message: "should identify Zero string"},
{value: "5", message: "should identify Positive integer string"},
{value: -16, message: "should identify Negative integer number"},
{value: 0, message: "should identify Zero integer number"},
{value: 32, message: "should identify Positive integer number"},
{value: "040", message: "should identify Octal integer literal string"},
{value: 0144, message: "should identify Octal integer literal"},
{value: "0xFF", message: "should identify Hexadecimal integer literal string"},
{value: 0xFFF, message: "should identify Hexadecimal integer literal"},
{value: 1.0, message: "should identify float versions of integers"},
{value: 8e5, message: "Exponential notation"}
];
var floatChecks = [
{value: "-1.6", message: "should identify Negative floating point string"},
{value: "4.536", message: "should identify Positive floating point string"},
{value: -2.6, message: "should identify Negative floating point number"},
{value: 3.1415, message: "should identify Positive floating point number"},
{value: 8.11e1, message: "should identify Exponential notation "},
{value: "123e-2", message: "should identify Exponential notation string"}
];
var negativeChecks = [
{value: "abc", message: "should identify non-numeric strings"},
{value: undefined, message: "should identify undefined"},
{value: NaN, message: "should identify NaN"},
{value: null, message: "should identify null"},
{value: Infinity, message: "should identify Infinity"}
];
var testMultiple = function(cases, fn, result){
for (var i = 0; i < cases.length; i++) {
equal(fn(cases[i].value), result, cases[i].message);
}
};
testMultiple(integerChecks, _.isInteger, true);
testMultiple(floatChecks, _.isInteger, false);
testMultiple(negativeChecks, _.isInteger, false);
testMultiple(integerChecks, _.isFloat, false);
testMultiple(floatChecks, _.isFloat, true);
testMultiple(negativeChecks, _.isFloat, false);
});
test("isIncreasing", function() {
var inc = [1,2,3];
var incNM = [1,2,3,3,4];
var dec = [5,4,3,2,1];
equal(_.isIncreasing.apply(null, inc), true, 'should identify when its arguments monotonically increase');
equal(_.isIncreasing.apply(null, incNM), false, 'should identify when its arguments monotonically increase');
equal(_.isIncreasing.apply(null, dec), false, 'should identify when its arguments do not increase');
});
test("isDecreasing", function() {
var inc = [1,2,3];
var incNM = [1,2,3,3,4];
var dec = [5,4,3,2,1];
var decNM = [5,4,3,3,2,1];
equal(_.isDecreasing.apply(null, inc), false, 'should identify when its arguments monotonically decrease');
equal(_.isDecreasing.apply(null, incNM), false, 'should identify when its arguments monotonically decrease');
equal(_.isDecreasing.apply(null, dec), true, 'should identify when its arguments do not decrease');
equal(_.isDecreasing.apply(null, decNM), false, 'should identify when its arguments monotonically decrease');
});
test("isValidDate", function() {
equal(_.isValidDate(new Date), true, 'should recognize a fresh Date instance as valid');
equal(!_.isValidDate(new Date("bad date")), true, 'should recognize a Date constructed with gibberish');
});
});