1(function() { 2 var _ = typeof require == 'function' ? require('..') : window._; 3 4 QUnit.module('Objects'); 5 6 var testElement = typeof document === 'object' ? document.createElement('div') : void 0; 7 8 QUnit.test('keys', function(assert) { 9 assert.deepEqual(_.keys({one: 1, two: 2}), ['one', 'two'], 'can extract the keys from an object'); 10 // the test above is not safe because it relies on for-in enumeration order 11 var a = []; a[1] = 0; 12 assert.deepEqual(_.keys(a), ['1'], 'is not fooled by sparse arrays; see issue #95'); 13 assert.deepEqual(_.keys(null), []); 14 assert.deepEqual(_.keys(void 0), []); 15 assert.deepEqual(_.keys(1), []); 16 assert.deepEqual(_.keys('a'), []); 17 assert.deepEqual(_.keys(true), []); 18 19 // keys that may be missed if the implementation isn't careful 20 var trouble = { 21 constructor: Object, 22 valueOf: _.noop, 23 hasOwnProperty: null, 24 toString: 5, 25 toLocaleString: void 0, 26 propertyIsEnumerable: /a/, 27 isPrototypeOf: this, 28 __defineGetter__: Boolean, 29 __defineSetter__: {}, 30 __lookupSetter__: false, 31 __lookupGetter__: [] 32 }; 33 var troubleKeys = ['constructor', 'valueOf', 'hasOwnProperty', 'toString', 'toLocaleString', 'propertyIsEnumerable', 34 'isPrototypeOf', '__defineGetter__', '__defineSetter__', '__lookupSetter__', '__lookupGetter__'].sort(); 35 assert.deepEqual(_.keys(trouble).sort(), troubleKeys, 'matches non-enumerable properties'); 36 }); 37 38 QUnit.test('allKeys', function(assert) { 39 assert.deepEqual(_.allKeys({one: 1, two: 2}), ['one', 'two'], 'can extract the allKeys from an object'); 40 // the test above is not safe because it relies on for-in enumeration order 41 var a = []; a[1] = 0; 42 assert.deepEqual(_.allKeys(a), ['1'], 'is not fooled by sparse arrays; see issue #95'); 43 44 a.a = a; 45 assert.deepEqual(_.allKeys(a), ['1', 'a'], 'is not fooled by sparse arrays with additional properties'); 46 47 _.each([null, void 0, 1, 'a', true, NaN, {}, [], new Number(5), new Date(0)], function(val) { 48 assert.deepEqual(_.allKeys(val), []); 49 }); 50 51 // allKeys that may be missed if the implementation isn't careful 52 var trouble = { 53 constructor: Object, 54 valueOf: _.noop, 55 hasOwnProperty: null, 56 toString: 5, 57 toLocaleString: void 0, 58 propertyIsEnumerable: /a/, 59 isPrototypeOf: this 60 }; 61 var troubleKeys = ['constructor', 'valueOf', 'hasOwnProperty', 'toString', 'toLocaleString', 'propertyIsEnumerable', 62 'isPrototypeOf'].sort(); 63 assert.deepEqual(_.allKeys(trouble).sort(), troubleKeys, 'matches non-enumerable properties'); 64 65 function A() {} 66 A.prototype.foo = 'foo'; 67 var b = new A(); 68 b.bar = 'bar'; 69 assert.deepEqual(_.allKeys(b).sort(), ['bar', 'foo'], 'should include inherited keys'); 70 71 function y() {} 72 y.x = 'z'; 73 assert.deepEqual(_.allKeys(y), ['x'], 'should get keys from constructor'); 74 }); 75 76 QUnit.test('values', function(assert) { 77 assert.deepEqual(_.values({one: 1, two: 2}), [1, 2], 'can extract the values from an object'); 78 assert.deepEqual(_.values({one: 1, two: 2, length: 3}), [1, 2, 3], '... even when one of them is "length"'); 79 }); 80 81 QUnit.test('pairs', function(assert) { 82 assert.deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can convert an object into pairs'); 83 assert.deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', 2], ['length', 3]], '... even when one of them is "length"'); 84 }); 85 86 QUnit.test('invert', function(assert) { 87 var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}; 88 assert.deepEqual(_.keys(_.invert(obj)), ['Moe', 'Larry', 'Curly'], 'can invert an object'); 89 assert.deepEqual(_.invert(_.invert(obj)), obj, 'two inverts gets you back where you started'); 90 91 obj = {length: 3}; 92 assert.equal(_.invert(obj)['3'], 'length', 'can invert an object with "length"'); 93 }); 94 95 QUnit.test('functions', function(assert) { 96 var obj = {a: 'dash', b: _.map, c: /yo/, d: _.reduce}; 97 assert.deepEqual(['b', 'd'], _.functions(obj), 'can grab the function names of any passed-in object'); 98 99 var Animal = function(){}; 100 Animal.prototype.run = function(){}; 101 assert.deepEqual(_.functions(new Animal), ['run'], 'also looks up functions on the prototype'); 102 }); 103 104 QUnit.test('methods', function(assert) { 105 assert.strictEqual(_.methods, _.functions, 'is an alias for functions'); 106 }); 107 108 QUnit.test('extend', function(assert) { 109 var result; 110 assert.equal(_.extend({}, {a: 'b'}).a, 'b', 'can extend an object with the attributes of another'); 111 assert.equal(_.extend({a: 'x'}, {a: 'b'}).a, 'b', 'properties in source override destination'); 112 assert.equal(_.extend({x: 'x'}, {a: 'b'}).x, 'x', "properties not in source don't get overriden"); 113 result = _.extend({x: 'x'}, {a: 'a'}, {b: 'b'}); 114 assert.deepEqual(result, {x: 'x', a: 'a', b: 'b'}, 'can extend from multiple source objects'); 115 result = _.extend({x: 'x'}, {a: 'a', x: 2}, {a: 'b'}); 116 assert.deepEqual(result, {x: 2, a: 'b'}, 'extending from multiple source objects last property trumps'); 117 result = _.extend({}, {a: void 0, b: null}); 118 assert.deepEqual(_.keys(result), ['a', 'b'], 'extend copies undefined values'); 119 120 var F = function() {}; 121 F.prototype = {a: 'b'}; 122 var subObj = new F(); 123 subObj.c = 'd'; 124 assert.deepEqual(_.extend({}, subObj), {a: 'b', c: 'd'}, 'extend copies all properties from source'); 125 _.extend(subObj, {}); 126 assert.notOk(subObj.hasOwnProperty('a'), "extend does not convert destination object's 'in' properties to 'own' properties"); 127 128 try { 129 result = {}; 130 _.extend(result, null, void 0, {a: 1}); 131 } catch (e) { /* ignored */ } 132 133 assert.equal(result.a, 1, 'should not error on `null` or `undefined` sources'); 134 135 assert.strictEqual(_.extend(null, {a: 1}), null, 'extending null results in null'); 136 assert.strictEqual(_.extend(void 0, {a: 1}), void 0, 'extending undefined results in undefined'); 137 }); 138 139 QUnit.test('extendOwn', function(assert) { 140 var result; 141 assert.equal(_.extendOwn({}, {a: 'b'}).a, 'b', 'can extend an object with the attributes of another'); 142 assert.equal(_.extendOwn({a: 'x'}, {a: 'b'}).a, 'b', 'properties in source override destination'); 143 assert.equal(_.extendOwn({x: 'x'}, {a: 'b'}).x, 'x', "properties not in source don't get overriden"); 144 result = _.extendOwn({x: 'x'}, {a: 'a'}, {b: 'b'}); 145 assert.deepEqual(result, {x: 'x', a: 'a', b: 'b'}, 'can extend from multiple source objects'); 146 result = _.extendOwn({x: 'x'}, {a: 'a', x: 2}, {a: 'b'}); 147 assert.deepEqual(result, {x: 2, a: 'b'}, 'extending from multiple source objects last property trumps'); 148 assert.deepEqual(_.extendOwn({}, {a: void 0, b: null}), {a: void 0, b: null}, 'copies undefined values'); 149 150 var F = function() {}; 151 F.prototype = {a: 'b'}; 152 var subObj = new F(); 153 subObj.c = 'd'; 154 assert.deepEqual(_.extendOwn({}, subObj), {c: 'd'}, 'copies own properties from source'); 155 156 result = {}; 157 assert.deepEqual(_.extendOwn(result, null, void 0, {a: 1}), {a: 1}, 'should not error on `null` or `undefined` sources'); 158 159 _.each(['a', 5, null, false], function(val) { 160 assert.strictEqual(_.extendOwn(val, {a: 1}), val, 'extending non-objects results in returning the non-object value'); 161 }); 162 163 assert.strictEqual(_.extendOwn(void 0, {a: 1}), void 0, 'extending undefined results in undefined'); 164 165 result = _.extendOwn({a: 1, 0: 2, 1: '5', length: 6}, {0: 1, 1: 2, length: 2}); 166 assert.deepEqual(result, {a: 1, 0: 1, 1: 2, length: 2}, 'should treat array-like objects like normal objects'); 167 }); 168 169 QUnit.test('assign', function(assert) { 170 assert.strictEqual(_.assign, _.extendOwn, 'is an alias for extendOwn'); 171 }); 172 173 QUnit.test('pick', function(assert) { 174 var result; 175 result = _.pick({a: 1, b: 2, c: 3}, 'a', 'c'); 176 assert.deepEqual(result, {a: 1, c: 3}, 'can restrict properties to those named'); 177 result = _.pick({a: 1, b: 2, c: 3}, ['b', 'c']); 178 assert.deepEqual(result, {b: 2, c: 3}, 'can restrict properties to those named in an array'); 179 result = _.pick({a: 1, b: 2, c: 3}, ['a'], 'b'); 180 assert.deepEqual(result, {a: 1, b: 2}, 'can restrict properties to those named in mixed args'); 181 result = _.pick(['a', 'b'], 1); 182 assert.deepEqual(result, {1: 'b'}, 'can pick numeric properties'); 183 184 _.each([null, void 0], function(val) { 185 assert.deepEqual(_.pick(val, 'hasOwnProperty'), {}, 'Called with null/undefined'); 186 assert.deepEqual(_.pick(val, _.constant(true)), {}); 187 }); 188 assert.deepEqual(_.pick(5, 'toString', 'b'), {toString: Number.prototype.toString}, 'can iterate primitives'); 189 190 var data = {a: 1, b: 2, c: 3}; 191 var callback = function(value, key, object) { 192 assert.strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); 193 assert.strictEqual(object, data); 194 return value !== this.value; 195 }; 196 result = _.pick(data, callback, {value: 2}); 197 assert.deepEqual(result, {a: 1, c: 3}, 'can accept a predicate and context'); 198 199 var Obj = function(){}; 200 Obj.prototype = {a: 1, b: 2, c: 3}; 201 var instance = new Obj(); 202 assert.deepEqual(_.pick(instance, 'a', 'c'), {a: 1, c: 3}, 'include prototype props'); 203 204 assert.deepEqual(_.pick(data, function(val, key) { 205 return this[key] === 3 && this === instance; 206 }, instance), {c: 3}, 'function is given context'); 207 208 assert.notOk(_.has(_.pick({}, 'foo'), 'foo'), 'does not set own property if property not in object'); 209 _.pick(data, function(value, key, obj) { 210 assert.equal(obj, data, 'passes same object as third parameter of iteratee'); 211 }); 212 }); 213 214 QUnit.test('omit', function(assert) { 215 var result; 216 result = _.omit({a: 1, b: 2, c: 3}, 'b'); 217 assert.deepEqual(result, {a: 1, c: 3}, 'can omit a single named property'); 218 result = _.omit({a: 1, b: 2, c: 3}, 'a', 'c'); 219 assert.deepEqual(result, {b: 2}, 'can omit several named properties'); 220 result = _.omit({a: 1, b: 2, c: 3}, ['b', 'c']); 221 assert.deepEqual(result, {a: 1}, 'can omit properties named in an array'); 222 result = _.omit(['a', 'b'], 0); 223 assert.deepEqual(result, {1: 'b'}, 'can omit numeric properties'); 224 225 assert.deepEqual(_.omit(null, 'a', 'b'), {}, 'non objects return empty object'); 226 assert.deepEqual(_.omit(void 0, 'toString'), {}, 'null/undefined return empty object'); 227 assert.deepEqual(_.omit(5, 'toString', 'b'), {}, 'returns empty object for primitives'); 228 229 var data = {a: 1, b: 2, c: 3}; 230 var callback = function(value, key, object) { 231 assert.strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); 232 assert.strictEqual(object, data); 233 return value !== this.value; 234 }; 235 result = _.omit(data, callback, {value: 2}); 236 assert.deepEqual(result, {b: 2}, 'can accept a predicate'); 237 238 var Obj = function(){}; 239 Obj.prototype = {a: 1, b: 2, c: 3}; 240 var instance = new Obj(); 241 assert.deepEqual(_.omit(instance, 'b'), {a: 1, c: 3}, 'include prototype props'); 242 243 assert.deepEqual(_.omit(data, function(val, key) { 244 return this[key] === 3 && this === instance; 245 }, instance), {a: 1, b: 2}, 'function is given context'); 246 }); 247 248 QUnit.test('defaults', function(assert) { 249 var options = {zero: 0, one: 1, empty: '', nan: NaN, nothing: null}; 250 251 _.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'}); 252 assert.equal(options.zero, 0, 'value exists'); 253 assert.equal(options.one, 1, 'value exists'); 254 assert.equal(options.twenty, 20, 'default applied'); 255 assert.equal(options.nothing, null, "null isn't overridden"); 256 257 _.defaults(options, {empty: 'full'}, {nan: 'nan'}, {word: 'word'}, {word: 'dog'}); 258 assert.equal(options.empty, '', 'value exists'); 259 assert.ok(_.isNaN(options.nan), "NaN isn't overridden"); 260 assert.equal(options.word, 'word', 'new value is added, first one wins'); 261 262 try { 263 options = {}; 264 _.defaults(options, null, void 0, {a: 1}); 265 } catch (e) { /* ignored */ } 266 267 assert.equal(options.a, 1, 'should not error on `null` or `undefined` sources'); 268 269 assert.deepEqual(_.defaults(null, {a: 1}), {a: 1}, 'defaults skips nulls'); 270 assert.deepEqual(_.defaults(void 0, {a: 1}), {a: 1}, 'defaults skips undefined'); 271 }); 272 273 QUnit.test('clone', function(assert) { 274 var moe = {name: 'moe', lucky: [13, 27, 34]}; 275 var clone = _.clone(moe); 276 assert.equal(clone.name, 'moe', 'the clone as the attributes of the original'); 277 278 clone.name = 'curly'; 279 assert.ok(clone.name === 'curly' && moe.name === 'moe', 'clones can change shallow attributes without affecting the original'); 280 281 clone.lucky.push(101); 282 assert.equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original'); 283 284 assert.equal(_.clone(void 0), void 0, 'non objects should not be changed by clone'); 285 assert.equal(_.clone(1), 1, 'non objects should not be changed by clone'); 286 assert.equal(_.clone(null), null, 'non objects should not be changed by clone'); 287 }); 288 289 QUnit.test('create', function(assert) { 290 var Parent = function() {}; 291 Parent.prototype = {foo: function() {}, bar: 2}; 292 293 _.each(['foo', null, void 0, 1], function(val) { 294 assert.deepEqual(_.create(val), {}, 'should return empty object when a non-object is provided'); 295 }); 296 297 assert.ok(_.create([]) instanceof Array, 'should return new instance of array when array is provided'); 298 299 var Child = function() {}; 300 Child.prototype = _.create(Parent.prototype); 301 assert.ok(new Child instanceof Parent, 'object should inherit prototype'); 302 303 var func = function() {}; 304 Child.prototype = _.create(Parent.prototype, {func: func}); 305 assert.strictEqual(Child.prototype.func, func, 'properties should be added to object'); 306 307 Child.prototype = _.create(Parent.prototype, {constructor: Child}); 308 assert.strictEqual(Child.prototype.constructor, Child); 309 310 Child.prototype.foo = 'foo'; 311 var created = _.create(Child.prototype, new Child); 312 assert.notOk(created.hasOwnProperty('foo'), 'should only add own properties'); 313 }); 314 315 QUnit.test('isEqual', function(assert) { 316 function First() { 317 this.value = 1; 318 } 319 First.prototype.value = 1; 320 function Second() { 321 this.value = 1; 322 } 323 Second.prototype.value = 2; 324 325 // Basic equality and identity comparisons. 326 assert.ok(_.isEqual(null, null), '`null` is equal to `null`'); 327 assert.ok(_.isEqual(), '`undefined` is equal to `undefined`'); 328 329 assert.notOk(_.isEqual(0, -0), '`0` is not equal to `-0`'); 330 assert.notOk(_.isEqual(-0, 0), 'Commutative equality is implemented for `0` and `-0`'); 331 assert.notOk(_.isEqual(null, void 0), '`null` is not equal to `undefined`'); 332 assert.notOk(_.isEqual(void 0, null), 'Commutative equality is implemented for `null` and `undefined`'); 333 334 // String object and primitive comparisons. 335 assert.ok(_.isEqual('Curly', 'Curly'), 'Identical string primitives are equal'); 336 assert.ok(_.isEqual(new String('Curly'), new String('Curly')), 'String objects with identical primitive values are equal'); 337 assert.ok(_.isEqual(new String('Curly'), 'Curly'), 'String primitives and their corresponding object wrappers are equal'); 338 assert.ok(_.isEqual('Curly', new String('Curly')), 'Commutative equality is implemented for string objects and primitives'); 339 340 assert.notOk(_.isEqual('Curly', 'Larry'), 'String primitives with different values are not equal'); 341 assert.notOk(_.isEqual(new String('Curly'), new String('Larry')), 'String objects with different primitive values are not equal'); 342 assert.notOk(_.isEqual(new String('Curly'), {toString: function(){ return 'Curly'; }}), 'String objects and objects with a custom `toString` method are not equal'); 343 344 // Number object and primitive comparisons. 345 assert.ok(_.isEqual(75, 75), 'Identical number primitives are equal'); 346 assert.ok(_.isEqual(new Number(75), new Number(75)), 'Number objects with identical primitive values are equal'); 347 assert.ok(_.isEqual(75, new Number(75)), 'Number primitives and their corresponding object wrappers are equal'); 348 assert.ok(_.isEqual(new Number(75), 75), 'Commutative equality is implemented for number objects and primitives'); 349 assert.notOk(_.isEqual(new Number(0), -0), '`new Number(0)` and `-0` are not equal'); 350 assert.notOk(_.isEqual(0, new Number(-0)), 'Commutative equality is implemented for `new Number(0)` and `-0`'); 351 352 assert.notOk(_.isEqual(new Number(75), new Number(63)), 'Number objects with different primitive values are not equal'); 353 assert.notOk(_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), 'Number objects and objects with a `valueOf` method are not equal'); 354 355 // Comparisons involving `NaN`. 356 assert.ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`'); 357 assert.ok(_.isEqual(new Number(NaN), NaN), 'Object(`NaN`) is equal to `NaN`'); 358 assert.notOk(_.isEqual(61, NaN), 'A number primitive is not equal to `NaN`'); 359 assert.notOk(_.isEqual(new Number(79), NaN), 'A number object is not equal to `NaN`'); 360 assert.notOk(_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`'); 361 362 // Boolean object and primitive comparisons. 363 assert.ok(_.isEqual(true, true), 'Identical boolean primitives are equal'); 364 assert.ok(_.isEqual(new Boolean, new Boolean), 'Boolean objects with identical primitive values are equal'); 365 assert.ok(_.isEqual(true, new Boolean(true)), 'Boolean primitives and their corresponding object wrappers are equal'); 366 assert.ok(_.isEqual(new Boolean(true), true), 'Commutative equality is implemented for booleans'); 367 assert.notOk(_.isEqual(new Boolean(true), new Boolean), 'Boolean objects with different primitive values are not equal'); 368 369 // Common type coercions. 370 assert.notOk(_.isEqual(new Boolean(false), true), '`new Boolean(false)` is not equal to `true`'); 371 assert.notOk(_.isEqual('75', 75), 'String and number primitives with like values are not equal'); 372 assert.notOk(_.isEqual(new Number(63), new String(63)), 'String and number objects with like values are not equal'); 373 assert.notOk(_.isEqual(75, '75'), 'Commutative equality is implemented for like string and number values'); 374 assert.notOk(_.isEqual(0, ''), 'Number and string primitives with like values are not equal'); 375 assert.notOk(_.isEqual(1, true), 'Number and boolean primitives with like values are not equal'); 376 assert.notOk(_.isEqual(new Boolean(false), new Number(0)), 'Boolean and number objects with like values are not equal'); 377 assert.notOk(_.isEqual(false, new String('')), 'Boolean primitives and string objects with like values are not equal'); 378 assert.notOk(_.isEqual(12564504e5, new Date(2009, 9, 25)), 'Dates and their corresponding numeric primitive values are not equal'); 379 380 // Dates. 381 assert.ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), 'Date objects referencing identical times are equal'); 382 assert.notOk(_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), 'Date objects referencing different times are not equal'); 383 assert.notOk(_.isEqual(new Date(2009, 11, 13), { 384 getTime: function(){ 385 return 12606876e5; 386 } 387 }), 'Date objects and objects with a `getTime` method are not equal'); 388 assert.notOk(_.isEqual(new Date('Curly'), new Date('Curly')), 'Invalid dates are not equal'); 389 390 // Functions. 391 assert.notOk(_.isEqual(First, Second), 'Different functions with identical bodies and source code representations are not equal'); 392 393 // RegExps. 394 assert.ok(_.isEqual(/(?:)/gim, /(?:)/gim), 'RegExps with equivalent patterns and flags are equal'); 395 assert.ok(_.isEqual(/(?:)/gi, /(?:)/ig), 'Flag order is not significant'); 396 assert.notOk(_.isEqual(/(?:)/g, /(?:)/gi), 'RegExps with equivalent patterns and different flags are not equal'); 397 assert.notOk(_.isEqual(/Moe/gim, /Curly/gim), 'RegExps with different patterns and equivalent flags are not equal'); 398 assert.notOk(_.isEqual(/(?:)/gi, /(?:)/g), 'Commutative equality is implemented for RegExps'); 399 assert.notOk(_.isEqual(/Curly/g, {source: 'Larry', global: true, ignoreCase: false, multiline: false}), 'RegExps and RegExp-like objects are not equal'); 400 401 // Empty arrays, array-like objects, and object literals. 402 assert.ok(_.isEqual({}, {}), 'Empty object literals are equal'); 403 assert.ok(_.isEqual([], []), 'Empty array literals are equal'); 404 assert.ok(_.isEqual([{}], [{}]), 'Empty nested arrays and objects are equal'); 405 assert.notOk(_.isEqual({length: 0}, []), 'Array-like objects and arrays are not equal.'); 406 assert.notOk(_.isEqual([], {length: 0}), 'Commutative equality is implemented for array-like objects'); 407 408 assert.notOk(_.isEqual({}, []), 'Object literals and array literals are not equal'); 409 assert.notOk(_.isEqual([], {}), 'Commutative equality is implemented for objects and arrays'); 410 411 // Arrays with primitive and object values. 412 assert.ok(_.isEqual([1, 'Larry', true], [1, 'Larry', true]), 'Arrays containing identical primitives are equal'); 413 assert.ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, 9, 25)]), 'Arrays containing equivalent elements are equal'); 414 415 // Multi-dimensional arrays. 416 var a = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}]; 417 var b = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}]; 418 assert.ok(_.isEqual(a, b), 'Arrays containing nested arrays and objects are recursively compared'); 419 420 // Overwrite the methods defined in ES 5.1 section 15.4.4. 421 a.forEach = a.map = a.filter = a.every = a.indexOf = a.lastIndexOf = a.some = a.reduce = a.reduceRight = null; 422 b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sort = b.unshift = null; 423 424 // Array elements and properties. 425 assert.ok(_.isEqual(a, b), 'Arrays containing equivalent elements and different non-numeric properties are equal'); 426 a.push('White Rocks'); 427 assert.notOk(_.isEqual(a, b), 'Arrays of different lengths are not equal'); 428 a.push('East Boulder'); 429 b.push('Gunbarrel Ranch', 'Teller Farm'); 430 assert.notOk(_.isEqual(a, b), 'Arrays of identical lengths containing different elements are not equal'); 431 432 // Sparse arrays. 433 assert.ok(_.isEqual(Array(3), Array(3)), 'Sparse arrays of identical lengths are equal'); 434 assert.notOk(_.isEqual(Array(3), Array(6)), 'Sparse arrays of different lengths are not equal when both are empty'); 435 436 var sparse = []; 437 sparse[1] = 5; 438 assert.ok(_.isEqual(sparse, [void 0, 5]), 'Handles sparse arrays as dense'); 439 440 // Simple objects. 441 assert.ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true}), 'Objects containing identical primitives are equal'); 442 assert.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'); 443 assert.notOk(_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), 'Objects of identical sizes with different values are not equal'); 444 assert.notOk(_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), 'Objects of identical sizes with different property names are not equal'); 445 assert.notOk(_.isEqual({a: 1, b: 2}, {a: 1}), 'Objects of different sizes are not equal'); 446 assert.notOk(_.isEqual({a: 1}, {a: 1, b: 2}), 'Commutative equality is implemented for objects'); 447 assert.notOk(_.isEqual({x: 1, y: void 0}, {x: 1, z: 2}), 'Objects with identical keys and different values are not equivalent'); 448 449 // `A` contains nested objects and arrays. 450 a = { 451 name: new String('Moe Howard'), 452 age: new Number(77), 453 stooge: true, 454 hobbies: ['acting'], 455 film: { 456 name: 'Sing a Song of Six Pants', 457 release: new Date(1947, 9, 30), 458 stars: [new String('Larry Fine'), 'Shemp Howard'], 459 minutes: new Number(16), 460 seconds: 54 461 } 462 }; 463 464 // `B` contains equivalent nested objects and arrays. 465 b = { 466 name: new String('Moe Howard'), 467 age: new Number(77), 468 stooge: true, 469 hobbies: ['acting'], 470 film: { 471 name: 'Sing a Song of Six Pants', 472 release: new Date(1947, 9, 30), 473 stars: [new String('Larry Fine'), 'Shemp Howard'], 474 minutes: new Number(16), 475 seconds: 54 476 } 477 }; 478 assert.ok(_.isEqual(a, b), 'Objects with nested equivalent members are recursively compared'); 479 480 // Instances. 481 assert.ok(_.isEqual(new First, new First), 'Object instances are equal'); 482 assert.notOk(_.isEqual(new First, new Second), 'Objects with different constructors and identical own properties are not equal'); 483 assert.notOk(_.isEqual({value: 1}, new First), 'Object instances and objects sharing equivalent properties are not equal'); 484 assert.notOk(_.isEqual({value: 2}, new Second), 'The prototype chain of objects should not be examined'); 485 486 // Circular Arrays. 487 (a = []).push(a); 488 (b = []).push(b); 489 assert.ok(_.isEqual(a, b), 'Arrays containing circular references are equal'); 490 a.push(new String('Larry')); 491 b.push(new String('Larry')); 492 assert.ok(_.isEqual(a, b), 'Arrays containing circular references and equivalent properties are equal'); 493 a.push('Shemp'); 494 b.push('Curly'); 495 assert.notOk(_.isEqual(a, b), 'Arrays containing circular references and different properties are not equal'); 496 497 // More circular arrays #767. 498 a = ['everything is checked but', 'this', 'is not']; 499 a[1] = a; 500 b = ['everything is checked but', ['this', 'array'], 'is not']; 501 assert.notOk(_.isEqual(a, b), 'Comparison of circular references with non-circular references are not equal'); 502 503 // Circular Objects. 504 a = {abc: null}; 505 b = {abc: null}; 506 a.abc = a; 507 b.abc = b; 508 assert.ok(_.isEqual(a, b), 'Objects containing circular references are equal'); 509 a.def = 75; 510 b.def = 75; 511 assert.ok(_.isEqual(a, b), 'Objects containing circular references and equivalent properties are equal'); 512 a.def = new Number(75); 513 b.def = new Number(63); 514 assert.notOk(_.isEqual(a, b), 'Objects containing circular references and different properties are not equal'); 515 516 // More circular objects #767. 517 a = {everything: 'is checked', but: 'this', is: 'not'}; 518 a.but = a; 519 b = {everything: 'is checked', but: {that: 'object'}, is: 'not'}; 520 assert.notOk(_.isEqual(a, b), 'Comparison of circular references with non-circular object references are not equal'); 521 522 // Cyclic Structures. 523 a = [{abc: null}]; 524 b = [{abc: null}]; 525 (a[0].abc = a).push(a); 526 (b[0].abc = b).push(b); 527 assert.ok(_.isEqual(a, b), 'Cyclic structures are equal'); 528 a[0].def = 'Larry'; 529 b[0].def = 'Larry'; 530 assert.ok(_.isEqual(a, b), 'Cyclic structures containing equivalent properties are equal'); 531 a[0].def = new String('Larry'); 532 b[0].def = new String('Curly'); 533 assert.notOk(_.isEqual(a, b), 'Cyclic structures containing different properties are not equal'); 534 535 // Complex Circular References. 536 a = {foo: {b: {foo: {c: {foo: null}}}}}; 537 b = {foo: {b: {foo: {c: {foo: null}}}}}; 538 a.foo.b.foo.c.foo = a; 539 b.foo.b.foo.c.foo = b; 540 assert.ok(_.isEqual(a, b), 'Cyclic structures with nested and identically-named properties are equal'); 541 542 // Chaining. 543 assert.notOk(_.isEqual(_({x: 1, y: void 0}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal'); 544 545 a = _({x: 1, y: 2}).chain(); 546 b = _({x: 1, y: 2}).chain(); 547 assert.equal(_.isEqual(a.isEqual(b), _(true)), true, '`isEqual` can be chained'); 548 549 // Objects without a `constructor` property 550 if (Object.create) { 551 a = Object.create(null, {x: {value: 1, enumerable: true}}); 552 b = {x: 1}; 553 assert.ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. from Object.create'); 554 } 555 556 function Foo() { this.a = 1; } 557 Foo.prototype.constructor = null; 558 559 var other = {a: 1}; 560 assert.strictEqual(_.isEqual(new Foo, other), false, 'Objects from different constructors are not equal'); 561 562 563 // Tricky object cases val comparisions 564 assert.equal(_.isEqual([0], [-0]), false); 565 assert.equal(_.isEqual({a: 0}, {a: -0}), false); 566 assert.equal(_.isEqual([NaN], [NaN]), true); 567 assert.equal(_.isEqual({a: NaN}, {a: NaN}), true); 568 569 if (typeof Symbol !== 'undefined') { 570 var symbol = Symbol('x'); 571 assert.strictEqual(_.isEqual(symbol, symbol), true, 'A symbol is equal to itself'); 572 assert.strictEqual(_.isEqual(symbol, Object(symbol)), true, 'Even when wrapped in Object()'); 573 assert.strictEqual(_.isEqual(symbol, null), false, 'Different types are not equal'); 574 } 575 576 }); 577 578 QUnit.test('isEmpty', function(assert) { 579 assert.notOk(_([1]).isEmpty(), '[1] is not empty'); 580 assert.ok(_.isEmpty([]), '[] is empty'); 581 assert.notOk(_.isEmpty({one: 1}), '{one: 1} is not empty'); 582 assert.ok(_.isEmpty({}), '{} is empty'); 583 assert.ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty'); 584 assert.ok(_.isEmpty(null), 'null is empty'); 585 assert.ok(_.isEmpty(), 'undefined is empty'); 586 assert.ok(_.isEmpty(''), 'the empty string is empty'); 587 assert.notOk(_.isEmpty('moe'), 'but other strings are not'); 588 589 var obj = {one: 1}; 590 delete obj.one; 591 assert.ok(_.isEmpty(obj), 'deleting all the keys from an object empties it'); 592 593 var args = function(){ return arguments; }; 594 assert.ok(_.isEmpty(args()), 'empty arguments object is empty'); 595 assert.notOk(_.isEmpty(args('')), 'non-empty arguments object is not empty'); 596 597 // covers collecting non-enumerable properties in IE < 9 598 var nonEnumProp = {toString: 5}; 599 assert.notOk(_.isEmpty(nonEnumProp), 'non-enumerable property is not empty'); 600 }); 601 602 if (typeof document === 'object') { 603 QUnit.test('isElement', function(assert) { 604 assert.notOk(_.isElement('div'), 'strings are not dom elements'); 605 assert.ok(_.isElement(testElement), 'an element is a DOM element'); 606 }); 607 } 608 609 QUnit.test('isArguments', function(assert) { 610 var args = (function(){ return arguments; }(1, 2, 3)); 611 assert.notOk(_.isArguments('string'), 'a string is not an arguments object'); 612 assert.notOk(_.isArguments(_.isArguments), 'a function is not an arguments object'); 613 assert.ok(_.isArguments(args), 'but the arguments object is an arguments object'); 614 assert.notOk(_.isArguments(_.toArray(args)), 'but not when it\'s converted into an array'); 615 assert.notOk(_.isArguments([1, 2, 3]), 'and not vanilla arrays.'); 616 }); 617 618 QUnit.test('isObject', function(assert) { 619 assert.ok(_.isObject(arguments), 'the arguments object is object'); 620 assert.ok(_.isObject([1, 2, 3]), 'and arrays'); 621 if (testElement) { 622 assert.ok(_.isObject(testElement), 'and DOM element'); 623 } 624 assert.ok(_.isObject(function() {}), 'and functions'); 625 assert.notOk(_.isObject(null), 'but not null'); 626 assert.notOk(_.isObject(void 0), 'and not undefined'); 627 assert.notOk(_.isObject('string'), 'and not string'); 628 assert.notOk(_.isObject(12), 'and not number'); 629 assert.notOk(_.isObject(true), 'and not boolean'); 630 assert.ok(_.isObject(new String('string')), 'but new String()'); 631 }); 632 633 QUnit.test('isArray', function(assert) { 634 assert.notOk(_.isArray(void 0), 'undefined vars are not arrays'); 635 assert.notOk(_.isArray(arguments), 'the arguments object is not an array'); 636 assert.ok(_.isArray([1, 2, 3]), 'but arrays are'); 637 }); 638 639 QUnit.test('isString', function(assert) { 640 var obj = new String('I am a string object'); 641 if (testElement) { 642 assert.notOk(_.isString(testElement), 'an element is not a string'); 643 } 644 assert.ok(_.isString([1, 2, 3].join(', ')), 'but strings are'); 645 assert.strictEqual(_.isString('I am a string literal'), true, 'string literals are'); 646 assert.ok(_.isString(obj), 'so are String objects'); 647 assert.strictEqual(_.isString(1), false); 648 }); 649 650 QUnit.test('isSymbol', function(assert) { 651 assert.notOk(_.isSymbol(0), 'numbers are not symbols'); 652 assert.notOk(_.isSymbol(''), 'strings are not symbols'); 653 assert.notOk(_.isSymbol(_.isSymbol), 'functions are not symbols'); 654 if (typeof Symbol === 'function') { 655 assert.ok(_.isSymbol(Symbol()), 'symbols are symbols'); 656 assert.ok(_.isSymbol(Symbol('description')), 'described symbols are symbols'); 657 assert.ok(_.isSymbol(Object(Symbol())), 'boxed symbols are symbols'); 658 } 659 }); 660 661 QUnit.test('isNumber', function(assert) { 662 assert.notOk(_.isNumber('string'), 'a string is not a number'); 663 assert.notOk(_.isNumber(arguments), 'the arguments object is not a number'); 664 assert.notOk(_.isNumber(void 0), 'undefined is not a number'); 665 assert.ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are'); 666 assert.ok(_.isNumber(NaN), 'NaN *is* a number'); 667 assert.ok(_.isNumber(Infinity), 'Infinity is a number'); 668 assert.notOk(_.isNumber('1'), 'numeric strings are not numbers'); 669 }); 670 671 QUnit.test('isBoolean', function(assert) { 672 assert.notOk(_.isBoolean(2), 'a number is not a boolean'); 673 assert.notOk(_.isBoolean('string'), 'a string is not a boolean'); 674 assert.notOk(_.isBoolean('false'), 'the string "false" is not a boolean'); 675 assert.notOk(_.isBoolean('true'), 'the string "true" is not a boolean'); 676 assert.notOk(_.isBoolean(arguments), 'the arguments object is not a boolean'); 677 assert.notOk(_.isBoolean(void 0), 'undefined is not a boolean'); 678 assert.notOk(_.isBoolean(NaN), 'NaN is not a boolean'); 679 assert.notOk(_.isBoolean(null), 'null is not a boolean'); 680 assert.ok(_.isBoolean(true), 'but true is'); 681 assert.ok(_.isBoolean(false), 'and so is false'); 682 }); 683 684 QUnit.test('isMap', function(assert) { 685 assert.notOk(_.isMap('string'), 'a string is not a map'); 686 assert.notOk(_.isMap(2), 'a number is not a map'); 687 assert.notOk(_.isMap({}), 'an object is not a map'); 688 assert.notOk(_.isMap(false), 'a boolean is not a map'); 689 assert.notOk(_.isMap(void 0), 'undefined is not a map'); 690 assert.notOk(_.isMap([1, 2, 3]), 'an array is not a map'); 691 if (typeof Set === 'function') { 692 assert.notOk(_.isMap(new Set()), 'a set is not a map'); 693 } 694 if (typeof WeakSet === 'function') { 695 assert.notOk(_.isMap(new WeakSet()), 'a weakset is not a map'); 696 } 697 if (typeof WeakMap === 'function') { 698 assert.notOk(_.isMap(new WeakMap()), 'a weakmap is not a map'); 699 } 700 if (typeof Map === 'function') { 701 var keyString = 'a string'; 702 var obj = new Map(); 703 obj.set(keyString, 'value'); 704 assert.ok(_.isMap(obj), 'but a map is'); 705 } 706 }); 707 708 QUnit.test('isWeakMap', function(assert) { 709 assert.notOk(_.isWeakMap('string'), 'a string is not a weakmap'); 710 assert.notOk(_.isWeakMap(2), 'a number is not a weakmap'); 711 assert.notOk(_.isWeakMap({}), 'an object is not a weakmap'); 712 assert.notOk(_.isWeakMap(false), 'a boolean is not a weakmap'); 713 assert.notOk(_.isWeakMap(void 0), 'undefined is not a weakmap'); 714 assert.notOk(_.isWeakMap([1, 2, 3]), 'an array is not a weakmap'); 715 if (typeof Set === 'function') { 716 assert.notOk(_.isWeakMap(new Set()), 'a set is not a weakmap'); 717 } 718 if (typeof WeakSet === 'function') { 719 assert.notOk(_.isWeakMap(new WeakSet()), 'a weakset is not a weakmap'); 720 } 721 if (typeof Map === 'function') { 722 assert.notOk(_.isWeakMap(new Map()), 'a map is not a weakmap'); 723 } 724 if (typeof WeakMap === 'function') { 725 var keyObj = {}, obj = new WeakMap(); 726 obj.set(keyObj, 'value'); 727 assert.ok(_.isWeakMap(obj), 'but a weakmap is'); 728 } 729 }); 730 731 QUnit.test('isSet', function(assert) { 732 assert.notOk(_.isSet('string'), 'a string is not a set'); 733 assert.notOk(_.isSet(2), 'a number is not a set'); 734 assert.notOk(_.isSet({}), 'an object is not a set'); 735 assert.notOk(_.isSet(false), 'a boolean is not a set'); 736 assert.notOk(_.isSet(void 0), 'undefined is not a set'); 737 assert.notOk(_.isSet([1, 2, 3]), 'an array is not a set'); 738 if (typeof Map === 'function') { 739 assert.notOk(_.isSet(new Map()), 'a map is not a set'); 740 } 741 if (typeof WeakMap === 'function') { 742 assert.notOk(_.isSet(new WeakMap()), 'a weakmap is not a set'); 743 } 744 if (typeof WeakSet === 'function') { 745 assert.notOk(_.isSet(new WeakSet()), 'a weakset is not a set'); 746 } 747 if (typeof Set === 'function') { 748 var obj = new Set(); 749 obj.add(1).add('string').add(false).add({}); 750 assert.ok(_.isSet(obj), 'but a set is'); 751 } 752 }); 753 754 QUnit.test('isWeakSet', function(assert) { 755 756 assert.notOk(_.isWeakSet('string'), 'a string is not a weakset'); 757 assert.notOk(_.isWeakSet(2), 'a number is not a weakset'); 758 assert.notOk(_.isWeakSet({}), 'an object is not a weakset'); 759 assert.notOk(_.isWeakSet(false), 'a boolean is not a weakset'); 760 assert.notOk(_.isWeakSet(void 0), 'undefined is not a weakset'); 761 assert.notOk(_.isWeakSet([1, 2, 3]), 'an array is not a weakset'); 762 if (typeof Map === 'function') { 763 assert.notOk(_.isWeakSet(new Map()), 'a map is not a weakset'); 764 } 765 if (typeof WeakMap === 'function') { 766 assert.notOk(_.isWeakSet(new WeakMap()), 'a weakmap is not a weakset'); 767 } 768 if (typeof Set === 'function') { 769 assert.notOk(_.isWeakSet(new Set()), 'a set is not a weakset'); 770 } 771 if (typeof WeakSet === 'function') { 772 var obj = new WeakSet(); 773 obj.add({x: 1}, {y: 'string'}).add({y: 'string'}).add({z: [1, 2, 3]}); 774 assert.ok(_.isWeakSet(obj), 'but a weakset is'); 775 } 776 }); 777 778 QUnit.test('isFunction', function(assert) { 779 assert.notOk(_.isFunction(void 0), 'undefined vars are not functions'); 780 assert.notOk(_.isFunction([1, 2, 3]), 'arrays are not functions'); 781 assert.notOk(_.isFunction('moe'), 'strings are not functions'); 782 assert.ok(_.isFunction(_.isFunction), 'but functions are'); 783 assert.ok(_.isFunction(function(){}), 'even anonymous ones'); 784 785 if (testElement) { 786 assert.notOk(_.isFunction(testElement), 'elements are not functions'); 787 } 788 789 var nodelist = typeof document != 'undefined' && document.childNodes; 790 if (nodelist) { 791 assert.notOk(_.isFunction(nodelist)); 792 } 793 }); 794 795 if (typeof Int8Array !== 'undefined') { 796 QUnit.test('#1929 Typed Array constructors are functions', function(assert) { 797 _.chain(['Float32Array', 'Float64Array', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array']) 798 .map(_.propertyOf(typeof GLOBAL != 'undefined' ? GLOBAL : window)) 799 .compact() 800 .each(function(TypedArray) { 801 // PhantomJS reports `typeof UInt8Array == 'object'` and doesn't report toString TypeArray 802 // as a function 803 assert.strictEqual(_.isFunction(TypedArray), Object.prototype.toString.call(TypedArray) === '[object Function]'); 804 }); 805 }); 806 } 807 808 QUnit.test('isDate', function(assert) { 809 assert.notOk(_.isDate(100), 'numbers are not dates'); 810 assert.notOk(_.isDate({}), 'objects are not dates'); 811 assert.ok(_.isDate(new Date()), 'but dates are'); 812 }); 813 814 QUnit.test('isRegExp', function(assert) { 815 assert.notOk(_.isRegExp(_.identity), 'functions are not RegExps'); 816 assert.ok(_.isRegExp(/identity/), 'but RegExps are'); 817 }); 818 819 QUnit.test('isFinite', function(assert) { 820 assert.notOk(_.isFinite(void 0), 'undefined is not finite'); 821 assert.notOk(_.isFinite(null), 'null is not finite'); 822 assert.notOk(_.isFinite(NaN), 'NaN is not finite'); 823 assert.notOk(_.isFinite(Infinity), 'Infinity is not finite'); 824 assert.notOk(_.isFinite(-Infinity), '-Infinity is not finite'); 825 assert.ok(_.isFinite('12'), 'Numeric strings are numbers'); 826 assert.notOk(_.isFinite('1a'), 'Non numeric strings are not numbers'); 827 assert.notOk(_.isFinite(''), 'Empty strings are not numbers'); 828 var obj = new Number(5); 829 assert.ok(_.isFinite(obj), 'Number instances can be finite'); 830 assert.ok(_.isFinite(0), '0 is finite'); 831 assert.ok(_.isFinite(123), 'Ints are finite'); 832 assert.ok(_.isFinite(-12.44), 'Floats are finite'); 833 if (typeof Symbol === 'function') { 834 assert.notOk(_.isFinite(Symbol()), 'symbols are not numbers'); 835 assert.notOk(_.isFinite(Symbol('description')), 'described symbols are not numbers'); 836 assert.notOk(_.isFinite(Object(Symbol())), 'boxed symbols are not numbers'); 837 } 838 }); 839 840 QUnit.test('isNaN', function(assert) { 841 assert.notOk(_.isNaN(void 0), 'undefined is not NaN'); 842 assert.notOk(_.isNaN(null), 'null is not NaN'); 843 assert.notOk(_.isNaN(0), '0 is not NaN'); 844 assert.notOk(_.isNaN(new Number(0)), 'wrapped 0 is not NaN'); 845 assert.ok(_.isNaN(NaN), 'but NaN is'); 846 assert.ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN'); 847 if (typeof Symbol !== 'undefined'){ 848 assert.notOk(_.isNaN(Symbol()), 'symbol is not NaN'); 849 } 850 }); 851 852 QUnit.test('isNull', function(assert) { 853 assert.notOk(_.isNull(void 0), 'undefined is not null'); 854 assert.notOk(_.isNull(NaN), 'NaN is not null'); 855 assert.ok(_.isNull(null), 'but null is'); 856 }); 857 858 QUnit.test('isUndefined', function(assert) { 859 assert.notOk(_.isUndefined(1), 'numbers are defined'); 860 assert.notOk(_.isUndefined(null), 'null is defined'); 861 assert.notOk(_.isUndefined(false), 'false is defined'); 862 assert.notOk(_.isUndefined(NaN), 'NaN is defined'); 863 assert.ok(_.isUndefined(), 'nothing is undefined'); 864 assert.ok(_.isUndefined(void 0), 'undefined is undefined'); 865 }); 866 867 QUnit.test('isError', function(assert) { 868 assert.notOk(_.isError(1), 'numbers are not Errors'); 869 assert.notOk(_.isError(null), 'null is not an Error'); 870 assert.notOk(_.isError(Error), 'functions are not Errors'); 871 assert.ok(_.isError(new Error()), 'Errors are Errors'); 872 assert.ok(_.isError(new EvalError()), 'EvalErrors are Errors'); 873 assert.ok(_.isError(new RangeError()), 'RangeErrors are Errors'); 874 assert.ok(_.isError(new ReferenceError()), 'ReferenceErrors are Errors'); 875 assert.ok(_.isError(new SyntaxError()), 'SyntaxErrors are Errors'); 876 assert.ok(_.isError(new TypeError()), 'TypeErrors are Errors'); 877 assert.ok(_.isError(new URIError()), 'URIErrors are Errors'); 878 }); 879 880 QUnit.test('tap', function(assert) { 881 var intercepted = null; 882 var interceptor = function(obj) { intercepted = obj; }; 883 var returned = _.tap(1, interceptor); 884 assert.equal(intercepted, 1, 'passes tapped object to interceptor'); 885 assert.equal(returned, 1, 'returns tapped object'); 886 887 returned = _([1, 2, 3]).chain(). 888 map(function(n){ return n * 2; }). 889 max(). 890 tap(interceptor). 891 value(); 892 assert.equal(returned, 6, 'can use tapped objects in a chain'); 893 assert.equal(intercepted, returned, 'can use tapped objects in a chain'); 894 }); 895 896 QUnit.test('has', function(assert) { 897 var obj = {foo: 'bar', func: function(){}}; 898 assert.ok(_.has(obj, 'foo'), 'has() checks that the object has a property.'); 899 assert.notOk(_.has(obj, 'baz'), "has() returns false if the object doesn't have the property."); 900 assert.ok(_.has(obj, 'func'), 'has() works for functions too.'); 901 obj.hasOwnProperty = null; 902 assert.ok(_.has(obj, 'foo'), 'has() works even when the hasOwnProperty method is deleted.'); 903 var child = {}; 904 child.prototype = obj; 905 assert.notOk(_.has(child, 'foo'), 'has() does not check the prototype chain for a property.'); 906 assert.strictEqual(_.has(null, 'foo'), false, 'has() returns false for null'); 907 assert.strictEqual(_.has(void 0, 'foo'), false, 'has() returns false for undefined'); 908 }); 909 910 QUnit.test('isMatch', function(assert) { 911 var moe = {name: 'Moe Howard', hair: true}; 912 var curly = {name: 'Curly Howard', hair: false}; 913 914 assert.equal(_.isMatch(moe, {hair: true}), true, 'Returns a boolean'); 915 assert.equal(_.isMatch(curly, {hair: true}), false, 'Returns a boolean'); 916 917 assert.equal(_.isMatch(5, {__x__: void 0}), false, 'can match undefined props on primitives'); 918 assert.equal(_.isMatch({__x__: void 0}, {__x__: void 0}), true, 'can match undefined props'); 919 920 assert.equal(_.isMatch(null, {}), true, 'Empty spec called with null object returns true'); 921 assert.equal(_.isMatch(null, {a: 1}), false, 'Non-empty spec called with null object returns false'); 922 923 _.each([null, void 0], function(item) { assert.strictEqual(_.isMatch(item, null), true, 'null matches null'); }); 924 _.each([null, void 0], function(item) { assert.strictEqual(_.isMatch(item, null), true, 'null matches {}'); }); 925 assert.strictEqual(_.isMatch({b: 1}, {a: void 0}), false, 'handles undefined values (1683)'); 926 927 _.each([true, 5, NaN, null, void 0], function(item) { 928 assert.strictEqual(_.isMatch({a: 1}, item), true, 'treats primitives as empty'); 929 }); 930 931 function Prototest() {} 932 Prototest.prototype.x = 1; 933 var specObj = new Prototest; 934 assert.equal(_.isMatch({x: 2}, specObj), true, 'spec is restricted to own properties'); 935 936 specObj.y = 5; 937 assert.equal(_.isMatch({x: 1, y: 5}, specObj), true); 938 assert.equal(_.isMatch({x: 1, y: 4}, specObj), false); 939 940 assert.ok(_.isMatch(specObj, {x: 1, y: 5}), 'inherited and own properties are checked on the test object'); 941 942 Prototest.x = 5; 943 assert.ok(_.isMatch({x: 5, y: 1}, Prototest), 'spec can be a function'); 944 945 //null edge cases 946 var oCon = {constructor: Object}; 947 assert.deepEqual(_.map([null, void 0, 5, {}], _.partial(_.isMatch, _, oCon)), [false, false, false, true], 'doesnt falsey match constructor on undefined/null'); 948 }); 949 950 QUnit.test('matcher', function(assert) { 951 var moe = {name: 'Moe Howard', hair: true}; 952 var curly = {name: 'Curly Howard', hair: false}; 953 var stooges = [moe, curly]; 954 955 assert.equal(_.matcher({hair: true})(moe), true, 'Returns a boolean'); 956 assert.equal(_.matcher({hair: true})(curly), false, 'Returns a boolean'); 957 958 assert.equal(_.matcher({__x__: void 0})(5), false, 'can match undefined props on primitives'); 959 assert.equal(_.matcher({__x__: void 0})({__x__: void 0}), true, 'can match undefined props'); 960 961 assert.equal(_.matcher({})(null), true, 'Empty spec called with null object returns true'); 962 assert.equal(_.matcher({a: 1})(null), false, 'Non-empty spec called with null object returns false'); 963 964 assert.strictEqual(_.find(stooges, _.matcher({hair: false})), curly, 'returns a predicate that can be used by finding functions.'); 965 assert.strictEqual(_.find(stooges, _.matcher(moe)), moe, 'can be used to locate an object exists in a collection.'); 966 assert.deepEqual(_.filter([null, void 0], _.matcher({a: 1})), [], 'Do not throw on null values.'); 967 968 assert.deepEqual(_.filter([null, void 0], _.matcher(null)), [null, void 0], 'null matches null'); 969 assert.deepEqual(_.filter([null, void 0], _.matcher({})), [null, void 0], 'null matches {}'); 970 assert.deepEqual(_.filter([{b: 1}], _.matcher({a: void 0})), [], 'handles undefined values (1683)'); 971 972 _.each([true, 5, NaN, null, void 0], function(item) { 973 assert.equal(_.matcher(item)({a: 1}), true, 'treats primitives as empty'); 974 }); 975 976 function Prototest() {} 977 Prototest.prototype.x = 1; 978 var specObj = new Prototest; 979 var protospec = _.matcher(specObj); 980 assert.equal(protospec({x: 2}), true, 'spec is restricted to own properties'); 981 982 specObj.y = 5; 983 protospec = _.matcher(specObj); 984 assert.equal(protospec({x: 1, y: 5}), true); 985 assert.equal(protospec({x: 1, y: 4}), false); 986 987 assert.ok(_.matcher({x: 1, y: 5})(specObj), 'inherited and own properties are checked on the test object'); 988 989 Prototest.x = 5; 990 assert.ok(_.matcher(Prototest)({x: 5, y: 1}), 'spec can be a function'); 991 992 // #1729 993 var o = {b: 1}; 994 var m = _.matcher(o); 995 996 assert.equal(m({b: 1}), true); 997 o.b = 2; 998 o.a = 1; 999 assert.equal(m({b: 1}), true, 'changing spec object doesnt change matches result'); 1000 1001 1002 //null edge cases 1003 var oCon = _.matcher({constructor: Object}); 1004 assert.deepEqual(_.map([null, void 0, 5, {}], oCon), [false, false, false, true], 'doesnt falsey match constructor on undefined/null'); 1005 }); 1006 1007 QUnit.test('matches', function(assert) { 1008 assert.strictEqual(_.matches, _.matcher, 'is an alias for matcher'); 1009 }); 1010 1011 QUnit.test('findKey', function(assert) { 1012 var objects = { 1013 a: {a: 0, b: 0}, 1014 b: {a: 1, b: 1}, 1015 c: {a: 2, b: 2} 1016 }; 1017 1018 assert.equal(_.findKey(objects, function(obj) { 1019 return obj.a === 0; 1020 }), 'a'); 1021 1022 assert.equal(_.findKey(objects, function(obj) { 1023 return obj.b * obj.a === 4; 1024 }), 'c'); 1025 1026 assert.equal(_.findKey(objects, 'a'), 'b', 'Uses lookupIterator'); 1027 1028 assert.equal(_.findKey(objects, function(obj) { 1029 return obj.b * obj.a === 5; 1030 }), void 0); 1031 1032 assert.strictEqual(_.findKey([1, 2, 3, 4, 5, 6], function(obj) { 1033 return obj === 3; 1034 }), '2', 'Keys are strings'); 1035 1036 assert.strictEqual(_.findKey(objects, function(a) { 1037 return a.foo === null; 1038 }), void 0); 1039 1040 _.findKey({a: {a: 1}}, function(a, key, obj) { 1041 assert.equal(key, 'a'); 1042 assert.deepEqual(obj, {a: {a: 1}}); 1043 assert.strictEqual(this, objects, 'called with context'); 1044 }, objects); 1045 1046 var array = [1, 2, 3, 4]; 1047 array.match = 55; 1048 assert.strictEqual(_.findKey(array, function(x) { return x === 55; }), 'match', 'matches array-likes keys'); 1049 }); 1050 1051 1052 QUnit.test('mapObject', function(assert) { 1053 var obj = {a: 1, b: 2}; 1054 var objects = { 1055 a: {a: 0, b: 0}, 1056 b: {a: 1, b: 1}, 1057 c: {a: 2, b: 2} 1058 }; 1059 1060 assert.deepEqual(_.mapObject(obj, function(val) { 1061 return val * 2; 1062 }), {a: 2, b: 4}, 'simple objects'); 1063 1064 assert.deepEqual(_.mapObject(objects, function(val) { 1065 return _.reduce(val, function(memo, v){ 1066 return memo + v; 1067 }, 0); 1068 }), {a: 0, b: 2, c: 4}, 'nested objects'); 1069 1070 assert.deepEqual(_.mapObject(obj, function(val, key, o) { 1071 return o[key] * 2; 1072 }), {a: 2, b: 4}, 'correct keys'); 1073 1074 assert.deepEqual(_.mapObject([1, 2], function(val) { 1075 return val * 2; 1076 }), {0: 2, 1: 4}, 'check behavior for arrays'); 1077 1078 assert.deepEqual(_.mapObject(obj, function(val) { 1079 return val * this.multiplier; 1080 }, {multiplier: 3}), {a: 3, b: 6}, 'keep context'); 1081 1082 assert.deepEqual(_.mapObject({a: 1}, function() { 1083 return this.length; 1084 }, [1, 2]), {a: 2}, 'called with context'); 1085 1086 var ids = _.mapObject({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){ 1087 return n.id; 1088 }); 1089 assert.deepEqual(ids, {length: void 0, 0: '1', 1: '2'}, 'Check with array-like objects'); 1090 1091 // Passing a property name like _.pluck. 1092 var people = {a: {name: 'moe', age: 30}, b: {name: 'curly', age: 50}}; 1093 assert.deepEqual(_.mapObject(people, 'name'), {a: 'moe', b: 'curly'}, 'predicate string map to object properties'); 1094 1095 _.each([null, void 0, 1, 'abc', [], {}, void 0], function(val){ 1096 assert.deepEqual(_.mapObject(val, _.identity), {}, 'mapValue identity'); 1097 }); 1098 1099 var Proto = function(){ this.a = 1; }; 1100 Proto.prototype.b = 1; 1101 var protoObj = new Proto(); 1102 assert.deepEqual(_.mapObject(protoObj, _.identity), {a: 1}, 'ignore inherited values from prototypes'); 1103 1104 }); 1105}()); 1106