2 var _ = typeof require == 'function' ? require('..') : window._;
4 QUnit.module('Chaining');
6 QUnit.test('map/flatten/reduce', function(assert) {
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'
13 var counts = _(lyrics).chain()
14 .map(function(line) { return line.split(''); })
16 .reduce(function(hash, l) {
17 hash[l] = hash[l] || 0;
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');
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) {
30 }).reject(function(n) {
32 }).sortBy(function(n) {
35 assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
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) {
42 }).reject(function(n) {
44 }).sortBy(function(n) {
47 assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
50 QUnit.test('reverse/concat/unshift/pop/map', function(assert) {
51 var numbers = [1, 2, 3, 4, 5];
52 numbers = _(numbers).chain()
57 .map(function(n){ return n * 2; })
59 assert.deepEqual(numbers, [34, 10, 8, 6, 4, 2, 10, 10], 'can chain together array functions.');
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');
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');
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');
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]);
90 QUnit.test('#1562: Engine proxies for chained functions', function(assert) {
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');