Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
$(document).ready(function() {
2
3
module("Objects");
4
5
test("objects: keys", function() {
6
var exception = /object/;
7
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
8
// the test above is not safe because it relies on for-in enumeration order
9
var a = []; a[1] = 0;
10
equals(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
11
raises(function() { _.keys(null); }, exception, 'throws an error for `null` values');
12
raises(function() { _.keys(void 0); }, exception, 'throws an error for `undefined` values');
13
raises(function() { _.keys(1); }, exception, 'throws an error for number primitives');
14
raises(function() { _.keys('a'); }, exception, 'throws an error for string primitives');
15
raises(function() { _.keys(true); }, exception, 'throws an error for boolean primitives');
16
});
17
18
test("objects: values", function() {
19
equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
20
});
21
22
test("objects: functions", function() {
23
var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce};
24
ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object');
25
26
var Animal = function(){};
27
Animal.prototype.run = function(){};
28
equals(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype');
29
});
30
31
test("objects: extend", function() {
32
var result;
33
equals(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
34
equals(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
35
equals(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden');
36
result = _.extend({x:'x'}, {a:'a'}, {b:'b'});
37
ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects');
38
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
39
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
40
result = _.extend({}, {a: void 0, b: null});
41
equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values');
42
});
43
44
test("objects: defaults", function() {
45
var result;
46
var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"};
47
48
_.defaults(options, {zero: 1, one: 10, twenty: 20});
49
equals(options.zero, 0, 'value exists');
50
equals(options.one, 1, 'value exists');
51
equals(options.twenty, 20, 'default applied');
52
53
_.defaults(options, {empty: "full"}, {nan: "nan"}, {word: "word"}, {word: "dog"});
54
equals(options.empty, "", 'value exists');
55
ok(_.isNaN(options.nan), "NaN isn't overridden");
56
equals(options.word, "word", 'new value is added, first one wins');
57
});
58
59
test("objects: clone", function() {
60
var moe = {name : 'moe', lucky : [13, 27, 34]};
61
var clone = _.clone(moe);
62
equals(clone.name, 'moe', 'the clone as the attributes of the original');
63
64
clone.name = 'curly';
65
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
66
67
clone.lucky.push(101);
68
equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
69
70
equals(_.clone(undefined), void 0, 'non objects should not be changed by clone');
71
equals(_.clone(1), 1, 'non objects should not be changed by clone');
72
equals(_.clone(null), null, 'non objects should not be changed by clone');
73
});
74
75
test("objects: isEqual", function() {
76
function First() {
77
this.value = 1;
78
}
79
First.prototype.value = 1;
80
function Second() {
81
this.value = 1;
82
}
83
Second.prototype.value = 2;
84
85
// Basic equality and identity comparisons.
86
ok(_.isEqual(null, null), "`null` is equal to `null`");
87
ok(_.isEqual(), "`undefined` is equal to `undefined`");
88
89
ok(!_.isEqual(0, -0), "`0` is not equal to `-0`");
90
ok(!_.isEqual(-0, 0), "Commutative equality is implemented for `0` and `-0`");
91
ok(!_.isEqual(null, undefined), "`null` is not equal to `undefined`");
92
ok(!_.isEqual(undefined, null), "Commutative equality is implemented for `null` and `undefined`");
93
94
// String object and primitive comparisons.
95
ok(_.isEqual("Curly", "Curly"), "Identical string primitives are equal");
96
ok(_.isEqual(new String("Curly"), new String("Curly")), "String objects with identical primitive values are equal");
97
98
ok(!_.isEqual("Curly", "Larry"), "String primitives with different values are not equal");
99
ok(!_.isEqual(new String("Curly"), "Curly"), "String primitives and their corresponding object wrappers are not equal");
100
ok(!_.isEqual("Curly", new String("Curly")), "Commutative equality is implemented for string objects and primitives");
101
ok(!_.isEqual(new String("Curly"), new String("Larry")), "String objects with different primitive values are not equal");
102
ok(!_.isEqual(new String("Curly"), {toString: function(){ return "Curly"; }}), "String objects and objects with a custom `toString` method are not equal");
103
104
// Number object and primitive comparisons.
105
ok(_.isEqual(75, 75), "Identical number primitives are equal");
106
ok(_.isEqual(new Number(75), new Number(75)), "Number objects with identical primitive values are equal");
107
108
ok(!_.isEqual(75, new Number(75)), "Number primitives and their corresponding object wrappers are not equal");
109
ok(!_.isEqual(new Number(75), 75), "Commutative equality is implemented for number objects and primitives");
110
ok(!_.isEqual(new Number(75), new Number(63)), "Number objects with different primitive values are not equal");
111
ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), "Number objects and objects with a `valueOf` method are not equal");
112
113
// Comparisons involving `NaN`.
114
ok(_.isEqual(NaN, NaN), "`NaN` is equal to `NaN`");
115
ok(!_.isEqual(61, NaN), "A number primitive is not equal to `NaN`");
116
ok(!_.isEqual(new Number(79), NaN), "A number object is not equal to `NaN`");
117
ok(!_.isEqual(Infinity, NaN), "`Infinity` is not equal to `NaN`");
118
119
// Boolean object and primitive comparisons.
120
ok(_.isEqual(true, true), "Identical boolean primitives are equal");
121
ok(_.isEqual(new Boolean, new Boolean), "Boolean objects with identical primitive values are equal");
122
ok(!_.isEqual(true, new Boolean(true)), "Boolean primitives and their corresponding object wrappers are not equal");
123
ok(!_.isEqual(new Boolean(true), true), "Commutative equality is implemented for booleans");
124
ok(!_.isEqual(new Boolean(true), new Boolean), "Boolean objects with different primitive values are not equal");
125
126
// Common type coercions.
127
ok(!_.isEqual(true, new Boolean(false)), "Boolean objects are not equal to the boolean primitive `true`");
128
ok(!_.isEqual("75", 75), "String and number primitives with like values are not equal");
129
ok(!_.isEqual(new Number(63), new String(63)), "String and number objects with like values are not equal");
130
ok(!_.isEqual(75, "75"), "Commutative equality is implemented for like string and number values");
131
ok(!_.isEqual(0, ""), "Number and string primitives with like values are not equal");
132
ok(!_.isEqual(1, true), "Number and boolean primitives with like values are not equal");
133
ok(!_.isEqual(new Boolean(false), new Number(0)), "Boolean and number objects with like values are not equal");
134
ok(!_.isEqual(false, new String("")), "Boolean primitives and string objects with like values are not equal");
135
ok(!_.isEqual(12564504e5, new Date(2009, 9, 25)), "Dates and their corresponding numeric primitive values are not equal");
136
137
// Dates.
138
ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), "Date objects referencing identical times are equal");
139
ok(!_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), "Date objects referencing different times are not equal");
140
ok(!_.isEqual(new Date(2009, 11, 13), {
141
getTime: function(){
142
return 12606876e5;
143
}
144
}), "Date objects and objects with a `getTime` method are not equal");
145
ok(!_.isEqual(new Date("Curly"), new Date("Curly")), "Invalid dates are not equal");
146
147
// Functions.
148
ok(!_.isEqual(First, Second), "Different functions with identical bodies and source code representations are not equal");
149
150
// RegExps.
151
ok(_.isEqual(/(?:)/gim, /(?:)/gim), "RegExps with equivalent patterns and flags are equal");
152
ok(!_.isEqual(/(?:)/g, /(?:)/gi), "RegExps with equivalent patterns and different flags are not equal");
153
ok(!_.isEqual(/Moe/gim, /Curly/gim), "RegExps with different patterns and equivalent flags are not equal");
154
ok(!_.isEqual(/(?:)/gi, /(?:)/g), "Commutative equality is implemented for RegExps");
155
ok(!_.isEqual(/Curly/g, {source: "Larry", global: true, ignoreCase: false, multiline: false}), "RegExps and RegExp-like objects are not equal");
156
157
// Empty arrays, array-like objects, and object literals.
158
ok(_.isEqual({}, {}), "Empty object literals are equal");
159
ok(_.isEqual([], []), "Empty array literals are equal");
160
ok(_.isEqual([{}], [{}]), "Empty nested arrays and objects are equal");
161
ok(!_.isEqual({length: 0}, []), "Array-like objects and arrays are not equal.");
162
ok(!_.isEqual([], {length: 0}), "Commutative equality is implemented for array-like objects");
163
164
ok(!_.isEqual({}, []), "Object literals and array literals are not equal");
165
ok(!_.isEqual([], {}), "Commutative equality is implemented for objects and arrays");
166
167
// Arrays with primitive and object values.
168
ok(_.isEqual([1, "Larry", true], [1, "Larry", true]), "Arrays containing identical primitives are equal");
169
ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, 9, 25)]), "Arrays containing equivalent elements are equal");
170
171
// Multi-dimensional arrays.
172
var a = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
173
var b = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
174
ok(_.isEqual(a, b), "Arrays containing nested arrays and objects are recursively compared");
175
176
// Overwrite the methods defined in ES 5.1 section 15.4.4.
177
a.forEach = a.map = a.filter = a.every = a.indexOf = a.lastIndexOf = a.some = a.reduce = a.reduceRight = null;
178
b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sort = b.unshift = null;
179
180
// Array elements and properties.
181
ok(!_.isEqual(a, b), "Arrays containing equivalent elements and different non-numeric properties are not equal");
182
a.push("White Rocks");
183
ok(!_.isEqual(a, b), "Arrays of different lengths are not equal");
184
a.push("East Boulder");
185
b.push("Gunbarrel Ranch", "Teller Farm");
186
ok(!_.isEqual(a, b), "Arrays of identical lengths containing different elements are not equal");
187
188
// Sparse arrays.
189
ok(_.isEqual(Array(3), Array(3)), "Sparse arrays of identical lengths are equal");
190
ok(!_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are not equal when both are empty");
191
192
// According to the Microsoft deviations spec, section 2.1.26, JScript 5.x treats `undefined`
193
// elements in arrays as elisions. Thus, sparse arrays and dense arrays containing `undefined`
194
// values are equivalent.
195
if (0 in [undefined]) {
196
ok(!_.isEqual(Array(3), [undefined, undefined, undefined]), "Sparse and dense arrays are not equal");
197
ok(!_.isEqual([undefined, undefined, undefined], Array(3)), "Commutative equality is implemented for sparse and dense arrays");
198
}
199
200
// Simple objects.
201
ok(_.isEqual({a: "Curly", b: 1, c: true}, {a: "Curly", b: 1, c: true}), "Objects containing identical primitives are equal");
202
ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, b: new Date(2009, 11, 13)}), "Objects containing equivalent members are equal");
203
ok(!_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), "Objects of identical sizes with different values are not equal");
204
ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), "Objects of identical sizes with different property names are not equal");
205
ok(!_.isEqual({a: 1, b: 2}, {a: 1}), "Objects of different sizes are not equal");
206
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), "Commutative equality is implemented for objects");
207
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), "Objects with identical keys and different values are not equivalent");
208
209
// `A` contains nested objects and arrays.
210
a = {
211
name: new String("Moe Howard"),
212
age: new Number(77),
213
stooge: true,
214
hobbies: ["acting"],
215
film: {
216
name: "Sing a Song of Six Pants",
217
release: new Date(1947, 9, 30),
218
stars: [new String("Larry Fine"), "Shemp Howard"],
219
minutes: new Number(16),
220
seconds: 54
221
}
222
};
223
224
// `B` contains equivalent nested objects and arrays.
225
b = {
226
name: new String("Moe Howard"),
227
age: new Number(77),
228
stooge: true,
229
hobbies: ["acting"],
230
film: {
231
name: "Sing a Song of Six Pants",
232
release: new Date(1947, 9, 30),
233
stars: [new String("Larry Fine"), "Shemp Howard"],
234
minutes: new Number(16),
235
seconds: 54
236
}
237
};
238
ok(_.isEqual(a, b), "Objects with nested equivalent members are recursively compared");
239
240
// Instances.
241
ok(_.isEqual(new First, new First), "Object instances are equal");
242
ok(!_.isEqual(new First, new Second), "Objects with different constructors and identical own properties are not equal");
243
ok(!_.isEqual({value: 1}, new First), "Object instances and objects sharing equivalent properties are not identical");
244
ok(!_.isEqual({value: 2}, new Second), "The prototype chain of objects should not be examined");
245
246
// Circular Arrays.
247
(a = []).push(a);
248
(b = []).push(b);
249
ok(_.isEqual(a, b), "Arrays containing circular references are equal");
250
a.push(new String("Larry"));
251
b.push(new String("Larry"));
252
ok(_.isEqual(a, b), "Arrays containing circular references and equivalent properties are equal");
253
a.push("Shemp");
254
b.push("Curly");
255
ok(!_.isEqual(a, b), "Arrays containing circular references and different properties are not equal");
256
257
// Circular Objects.
258
a = {abc: null};
259
b = {abc: null};
260
a.abc = a;
261
b.abc = b;
262
ok(_.isEqual(a, b), "Objects containing circular references are equal");
263
a.def = 75;
264
b.def = 75;
265
ok(_.isEqual(a, b), "Objects containing circular references and equivalent properties are equal");
266
a.def = new Number(75);
267
b.def = new Number(63);
268
ok(!_.isEqual(a, b), "Objects containing circular references and different properties are not equal");
269
270
// Cyclic Structures.
271
a = [{abc: null}];
272
b = [{abc: null}];
273
(a[0].abc = a).push(a);
274
(b[0].abc = b).push(b);
275
ok(_.isEqual(a, b), "Cyclic structures are equal");
276
a[0].def = "Larry";
277
b[0].def = "Larry";
278
ok(_.isEqual(a, b), "Cyclic structures containing equivalent properties are equal");
279
a[0].def = new String("Larry");
280
b[0].def = new String("Curly");
281
ok(!_.isEqual(a, b), "Cyclic structures containing different properties are not equal");
282
283
// Complex Circular References.
284
a = {foo: {b: {foo: {c: {foo: null}}}}};
285
b = {foo: {b: {foo: {c: {foo: null}}}}};
286
a.foo.b.foo.c.foo = a;
287
b.foo.b.foo.c.foo = b;
288
ok(_.isEqual(a, b), "Cyclic structures with nested and identically-named properties are equal");
289
290
// Chaining.
291
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal');
292
equals(_({x: 1, y: 2}).chain().isEqual(_({x: 1, y: 2}).chain()).value(), true, '`isEqual` can be chained');
293
294
// Custom `isEqual` methods.
295
var isEqualObj = {isEqual: function (o) { return o.isEqual == this.isEqual; }, unique: {}};
296
var isEqualObjClone = {isEqual: isEqualObj.isEqual, unique: {}};
297
298
ok(_.isEqual(isEqualObj, isEqualObjClone), 'Both objects implement identical `isEqual` methods');
299
ok(_.isEqual(isEqualObjClone, isEqualObj), 'Commutative equality is implemented for objects with custom `isEqual` methods');
300
ok(!_.isEqual(isEqualObj, {}), 'Objects that do not implement equivalent `isEqual` methods are not equal');
301
ok(!_.isEqual({}, isEqualObj), 'Commutative equality is implemented for objects with different `isEqual` methods');
302
303
// Custom `isEqual` methods - comparing different types
304
LocalizedString = (function() {
305
function LocalizedString(id) { this.id = id; this.string = (this.id===10)? 'Bonjour': ''; }
306
LocalizedString.prototype.isEqual = function(that) {
307
if (_.isString(that)) return this.string == that;
308
else if (that instanceof LocalizedString) return this.id == that.id;
309
return false;
310
};
311
return LocalizedString;
312
})();
313
var localized_string1 = new LocalizedString(10), localized_string2 = new LocalizedString(10), localized_string3 = new LocalizedString(11);
314
ok(_.isEqual(localized_string1, localized_string2), 'comparing same typed instances with same ids');
315
ok(!_.isEqual(localized_string1, localized_string3), 'comparing same typed instances with different ids');
316
ok(_.isEqual(localized_string1, 'Bonjour'), 'comparing different typed instances with same values');
317
ok(_.isEqual('Bonjour', localized_string1), 'comparing different typed instances with same values');
318
ok(!_.isEqual('Bonjour', localized_string3), 'comparing two localized strings with different ids');
319
ok(!_.isEqual(localized_string1, 'Au revoir'), 'comparing different typed instances with different values');
320
ok(!_.isEqual('Au revoir', localized_string1), 'comparing different typed instances with different values');
321
322
// Custom `isEqual` methods - comparing with serialized data
323
Date.prototype.toJSON = function() {
324
return {
325
_type:'Date',
326
year:this.getUTCFullYear(),
327
month:this.getUTCMonth(),
328
day:this.getUTCDate(),
329
hours:this.getUTCHours(),
330
minutes:this.getUTCMinutes(),
331
seconds:this.getUTCSeconds()
332
};
333
};
334
Date.prototype.isEqual = function(that) {
335
var this_date_components = this.toJSON();
336
var that_date_components = (that instanceof Date) ? that.toJSON() : that;
337
delete this_date_components['_type']; delete that_date_components['_type']
338
return _.isEqual(this_date_components, that_date_components);
339
};
340
341
var date = new Date();
342
var date_json = {
343
_type:'Date',
344
year:date.getUTCFullYear(),
345
month:date.getUTCMonth(),
346
day:date.getUTCDate(),
347
hours:date.getUTCHours(),
348
minutes:date.getUTCMinutes(),
349
seconds:date.getUTCSeconds()
350
};
351
352
ok(_.isEqual(date_json, date), 'serialized date matches date');
353
ok(_.isEqual(date, date_json), 'date matches serialized date');
354
});
355
356
test("objects: isEmpty", function() {
357
ok(!_([1]).isEmpty(), '[1] is not empty');
358
ok(_.isEmpty([]), '[] is empty');
359
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
360
ok(_.isEmpty({}), '{} is empty');
361
ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty');
362
ok(_.isEmpty(null), 'null is empty');
363
ok(_.isEmpty(), 'undefined is empty');
364
ok(_.isEmpty(''), 'the empty string is empty');
365
ok(!_.isEmpty('moe'), 'but other strings are not');
366
367
var obj = {one : 1};
368
delete obj.one;
369
ok(_.isEmpty(obj), 'deleting all the keys from an object empties it');
370
});
371
372
// Setup remote variables for iFrame tests.
373
var iframe = document.createElement('iframe');
374
jQuery(iframe).appendTo(document.body);
375
var iDoc = iframe.contentDocument || iframe.contentWindow.document;
376
iDoc.write(
377
"<script>\
378
parent.iElement = document.createElement('div');\
379
parent.iArguments = (function(){ return arguments; })(1, 2, 3);\
380
parent.iArray = [1, 2, 3];\
381
parent.iString = new String('hello');\
382
parent.iNumber = new Number(100);\
383
parent.iFunction = (function(){});\
384
parent.iDate = new Date();\
385
parent.iRegExp = /hi/;\
386
parent.iNaN = NaN;\
387
parent.iNull = null;\
388
parent.iBoolean = new Boolean(false);\
389
parent.iUndefined = undefined;\
390
</script>"
391
);
392
iDoc.close();
393
394
test("objects: isElement", function() {
395
ok(!_.isElement('div'), 'strings are not dom elements');
396
ok(_.isElement($('html')[0]), 'the html tag is a DOM element');
397
ok(_.isElement(iElement), 'even from another frame');
398
});
399
400
test("objects: isArguments", function() {
401
var args = (function(){ return arguments; })(1, 2, 3);
402
ok(!_.isArguments('string'), 'a string is not an arguments object');
403
ok(!_.isArguments(_.isArguments), 'a function is not an arguments object');
404
ok(_.isArguments(args), 'but the arguments object is an arguments object');
405
ok(!_.isArguments(_.toArray(args)), 'but not when it\'s converted into an array');
406
ok(!_.isArguments([1,2,3]), 'and not vanilla arrays.');
407
ok(_.isArguments(iArguments), 'even from another frame');
408
});
409
410
test("objects: isObject", function() {
411
ok(_.isObject(arguments), 'the arguments object is object');
412
ok(_.isObject([1, 2, 3]), 'and arrays');
413
ok(_.isObject($('html')[0]), 'and DOM element');
414
ok(_.isObject(iElement), 'even from another frame');
415
ok(_.isObject(function () {}), 'and functions');
416
ok(_.isObject(iFunction), 'even from another frame');
417
ok(!_.isObject(null), 'but not null');
418
ok(!_.isObject(undefined), 'and not undefined');
419
ok(!_.isObject('string'), 'and not string');
420
ok(!_.isObject(12), 'and not number');
421
ok(!_.isObject(true), 'and not boolean');
422
ok(_.isObject(new String('string')), 'but new String()');
423
});
424
425
test("objects: isArray", function() {
426
ok(!_.isArray(arguments), 'the arguments object is not an array');
427
ok(_.isArray([1, 2, 3]), 'but arrays are');
428
ok(_.isArray(iArray), 'even from another frame');
429
});
430
431
test("objects: isString", function() {
432
ok(!_.isString(document.body), 'the document body is not a string');
433
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
434
ok(_.isString(iString), 'even from another frame');
435
});
436
437
test("objects: isNumber", function() {
438
ok(!_.isNumber('string'), 'a string is not a number');
439
ok(!_.isNumber(arguments), 'the arguments object is not a number');
440
ok(!_.isNumber(undefined), 'undefined is not a number');
441
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
442
ok(_.isNumber(NaN), 'NaN *is* a number');
443
ok(_.isNumber(Infinity), 'Infinity is a number');
444
ok(_.isNumber(iNumber), 'even from another frame');
445
ok(!_.isNumber('1'), 'numeric strings are not numbers');
446
});
447
448
test("objects: isBoolean", function() {
449
ok(!_.isBoolean(2), 'a number is not a boolean');
450
ok(!_.isBoolean("string"), 'a string is not a boolean');
451
ok(!_.isBoolean("false"), 'the string "false" is not a boolean');
452
ok(!_.isBoolean("true"), 'the string "true" is not a boolean');
453
ok(!_.isBoolean(arguments), 'the arguments object is not a boolean');
454
ok(!_.isBoolean(undefined), 'undefined is not a boolean');
455
ok(!_.isBoolean(NaN), 'NaN is not a boolean');
456
ok(!_.isBoolean(null), 'null is not a boolean');
457
ok(_.isBoolean(true), 'but true is');
458
ok(_.isBoolean(false), 'and so is false');
459
ok(_.isBoolean(iBoolean), 'even from another frame');
460
});
461
462
test("objects: isFunction", function() {
463
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
464
ok(!_.isFunction('moe'), 'strings are not functions');
465
ok(_.isFunction(_.isFunction), 'but functions are');
466
ok(_.isFunction(iFunction), 'even from another frame');
467
});
468
469
test("objects: isDate", function() {
470
ok(!_.isDate(100), 'numbers are not dates');
471
ok(!_.isDate({}), 'objects are not dates');
472
ok(_.isDate(new Date()), 'but dates are');
473
ok(_.isDate(iDate), 'even from another frame');
474
});
475
476
test("objects: isRegExp", function() {
477
ok(!_.isRegExp(_.identity), 'functions are not RegExps');
478
ok(_.isRegExp(/identity/), 'but RegExps are');
479
ok(_.isRegExp(iRegExp), 'even from another frame');
480
});
481
482
test("objects: isNaN", function() {
483
ok(!_.isNaN(undefined), 'undefined is not NaN');
484
ok(!_.isNaN(null), 'null is not NaN');
485
ok(!_.isNaN(0), '0 is not NaN');
486
ok(_.isNaN(NaN), 'but NaN is');
487
ok(_.isNaN(iNaN), 'even from another frame');
488
});
489
490
test("objects: isNull", function() {
491
ok(!_.isNull(undefined), 'undefined is not null');
492
ok(!_.isNull(NaN), 'NaN is not null');
493
ok(_.isNull(null), 'but null is');
494
ok(_.isNull(iNull), 'even from another frame');
495
});
496
497
test("objects: isUndefined", function() {
498
ok(!_.isUndefined(1), 'numbers are defined');
499
ok(!_.isUndefined(null), 'null is defined');
500
ok(!_.isUndefined(false), 'false is defined');
501
ok(!_.isUndefined(NaN), 'NaN is defined');
502
ok(_.isUndefined(), 'nothing is undefined');
503
ok(_.isUndefined(undefined), 'undefined is undefined');
504
ok(_.isUndefined(iUndefined), 'even from another frame');
505
});
506
507
if (window.ActiveXObject) {
508
test("objects: IE host objects", function() {
509
var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
510
ok(!_.isNumber(xml));
511
ok(!_.isBoolean(xml));
512
ok(!_.isNaN(xml));
513
ok(!_.isFunction(xml));
514
ok(!_.isNull(xml));
515
ok(!_.isUndefined(xml));
516
});
517
}
518
519
test("objects: tap", function() {
520
var intercepted = null;
521
var interceptor = function(obj) { intercepted = obj; };
522
var returned = _.tap(1, interceptor);
523
equals(intercepted, 1, "passes tapped object to interceptor");
524
equals(returned, 1, "returns tapped object");
525
526
returned = _([1,2,3]).chain().
527
map(function(n){ return n * 2; }).
528
max().
529
tap(interceptor).
530
value();
531
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
532
});
533
});
534
535