1(function() {
2  var _ = typeof require == 'function' ? require('..') : window._;
3
4  QUnit.module('Chaining');
5
6  QUnit.test('map/flatten/reduce', function(assert) {
7    var lyrics = [
8      'I\'m a lumberjack and I\'m okay',
9      'I sleep all night and I work all day',
10      'He\'s a lumberjack and he\'s okay',
11      'He sleeps all night and he works all day'
12    ];
13    var counts = _(lyrics).chain()
14      .map(function(line) { return line.split(''); })
15      .flatten()
16      .reduce(function(hash, l) {
17        hash[l] = hash[l] || 0;
18        hash[l]++;
19        return hash;
20      }, {})
21      .value();
22    assert.equal(counts.a, 16, 'counted all the letters in the song');
23    assert.equal(counts.e, 10, 'counted all the letters in the song');
24  });
25
26  QUnit.test('select/reject/sortBy', function(assert) {
27    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
28    numbers = _(numbers).chain().select(function(n) {
29      return n % 2 === 0;
30    }).reject(function(n) {
31      return n % 4 === 0;
32    }).sortBy(function(n) {
33      return -n;
34    }).value();
35    assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
36  });
37
38  QUnit.test('select/reject/sortBy in functional style', function(assert) {
39    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
40    numbers = _.chain(numbers).select(function(n) {
41      return n % 2 === 0;
42    }).reject(function(n) {
43      return n % 4 === 0;
44    }).sortBy(function(n) {
45      return -n;
46    }).value();
47    assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
48  });
49
50  QUnit.test('reverse/concat/unshift/pop/map', function(assert) {
51    var numbers = [1, 2, 3, 4, 5];
52    numbers = _(numbers).chain()
53      .reverse()
54      .concat([5, 5, 5])
55      .unshift(17)
56      .pop()
57      .map(function(n){ return n * 2; })
58      .value();
59    assert.deepEqual(numbers, [34, 10, 8, 6, 4, 2, 10, 10], 'can chain together array functions.');
60  });
61
62  QUnit.test('splice', function(assert) {
63    var instance = _([1, 2, 3, 4, 5]).chain();
64    assert.deepEqual(instance.splice(1, 3).value(), [1, 5]);
65    assert.deepEqual(instance.splice(1, 0).value(), [1, 5]);
66    assert.deepEqual(instance.splice(1, 1).value(), [1]);
67    assert.deepEqual(instance.splice(0, 1).value(), [], '#397 Can create empty array');
68  });
69
70  QUnit.test('shift', function(assert) {
71    var instance = _([1, 2, 3]).chain();
72    assert.deepEqual(instance.shift().value(), [2, 3]);
73    assert.deepEqual(instance.shift().value(), [3]);
74    assert.deepEqual(instance.shift().value(), [], '#397 Can create empty array');
75  });
76
77  QUnit.test('pop', function(assert) {
78    var instance = _([1, 2, 3]).chain();
79    assert.deepEqual(instance.pop().value(), [1, 2]);
80    assert.deepEqual(instance.pop().value(), [1]);
81    assert.deepEqual(instance.pop().value(), [], '#397 Can create empty array');
82  });
83
84  QUnit.test('chaining works in small stages', function(assert) {
85    var o = _([1, 2, 3, 4]).chain();
86    assert.deepEqual(o.filter(function(i) { return i < 3; }).value(), [1, 2]);
87    assert.deepEqual(o.filter(function(i) { return i > 2; }).value(), [3, 4]);
88  });
89
90  QUnit.test('#1562: Engine proxies for chained functions', function(assert) {
91    var wrapped = _(512);
92    assert.strictEqual(wrapped.toJSON(), 512);
93    assert.strictEqual(wrapped.valueOf(), 512);
94    assert.strictEqual(+wrapped, 512);
95    assert.strictEqual(wrapped.toString(), '512');
96    assert.strictEqual('' + wrapped, '512');
97  });
98
99}());
100