|
247 | 247 | _.forEach(object, function(value, key) { keys.push(key); });
|
248 | 248 | deepEqual(keys, ['length']);
|
249 | 249 | });
|
| 250 | + |
| 251 | + _.each([ |
| 252 | + { 'kind': 'literal', 'value': 'abc' }, |
| 253 | + { 'kind': 'object', 'value': Object('abc') } |
| 254 | + ], |
| 255 | + function(data) { |
| 256 | + test('should work with a string ' + data.kind + ' for `collection` (test in IE < 9)', function() { |
| 257 | + var args, |
| 258 | + collection = data.value, |
| 259 | + values = []; |
| 260 | + |
| 261 | + _.forEach(collection, function(value) { |
| 262 | + args || (args = slice.call(arguments)); |
| 263 | + values.push(value); |
| 264 | + }); |
| 265 | + |
| 266 | + deepEqual(args, ['a', 0, collection]); |
| 267 | + deepEqual(values, ['a', 'b', 'c']); |
| 268 | + }); |
| 269 | + }); |
250 | 270 | }());
|
251 | 271 |
|
252 | 272 | /*--------------------------------------------------------------------------*/
|
|
556 | 576 | (function() {
|
557 | 577 | test('should pass the correct `callback` arguments when iterating an object', function() {
|
558 | 578 | var args,
|
559 |
| - object = { 'a': 'A', 'b': 'B', 'c': 'C' }, |
560 |
| - keys = _.keys(object); |
| 579 | + object = { 'a': 'A', 'b': 'B' }, |
| 580 | + lastKey = _.keys(object).pop(); |
| 581 | + |
| 582 | + var expected = lastKey == 'a' |
| 583 | + ? ['A', 'B', 'b', object] |
| 584 | + : ['B', 'A', 'a', object]; |
561 | 585 |
|
562 | 586 | _.reduceRight(object, function() {
|
563 | 587 | args || (args = slice.call(arguments));
|
564 | 588 | });
|
565 | 589 |
|
566 |
| - deepEqual(args, ['C', 'B', 'b', object]); |
| 590 | + deepEqual(args, expected); |
567 | 591 | });
|
568 | 592 |
|
569 | 593 | test('should treat array-like object with invalid `length` as a regular object', function() {
|
570 | 594 | var args,
|
571 |
| - object = { 'a': 'A', 'length': -1 }; |
| 595 | + object = { 'a': 'A', 'length': -1 }, |
| 596 | + lastKey = _.keys(object).pop(); |
| 597 | + |
| 598 | + var expected = lastKey == 'a' |
| 599 | + ? ['A', '-1', 'length', object] |
| 600 | + : [-1, 'A', 'a', object]; |
572 | 601 |
|
573 | 602 | _.reduceRight(object, function() {
|
574 | 603 | args || (args = slice.call(arguments));
|
575 | 604 | });
|
576 | 605 |
|
577 |
| - deepEqual(args, [-1, 'A', 'a', object]); |
| 606 | + deepEqual(args, expected); |
| 607 | + }); |
| 608 | + |
| 609 | + _.each([ |
| 610 | + { 'kind': 'literal', 'value': 'abc' }, |
| 611 | + { 'kind': 'object', 'value': Object('abc') } |
| 612 | + ], |
| 613 | + function(data) { |
| 614 | + test('should work with a string ' + data.kind + ' for `collection` (test in IE < 9)', function() { |
| 615 | + var args, |
| 616 | + collection = data.value; |
| 617 | + |
| 618 | + var actual = _.reduceRight(collection, function(accumulator, value) { |
| 619 | + args || (args = slice.call(arguments)); |
| 620 | + return accumulator + value; |
| 621 | + }); |
| 622 | + |
| 623 | + deepEqual(args, ['c', 'b', 1, collection]); |
| 624 | + equal(actual, 'cba'); |
| 625 | + }); |
578 | 626 | });
|
579 | 627 | }());
|
580 | 628 |
|
|
769 | 817 | var object = { 'length': -1 };
|
770 | 818 | deepEqual(_.toArray(object), [-1]);
|
771 | 819 | });
|
| 820 | + |
| 821 | + test('should work with a string for `collection` (test in IE < 9)', function() { |
| 822 | + deepEqual(_.toArray('abc'), ['a', 'b', 'c']); |
| 823 | + deepEqual(_.toArray(Object('abc')), ['a', 'b', 'c']); |
| 824 | + }); |
772 | 825 | }(1, 2, 3));
|
773 | 826 |
|
774 | 827 | /*--------------------------------------------------------------------------*/
|
|
0 commit comments