summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE/client/bower_components/lodash/vendor/underscore/test/chaining.js
blob: 6ad21dcf6f858e3f8d87f8d5b72a366d1c03d4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function() {
  var _ = typeof require == 'function' ? require('..') : window._;

  QUnit.module('Chaining');

  QUnit.test('map/flatten/reduce', function(assert) {
    var lyrics = [
      'I\'m a lumberjack and I\'m okay',
      'I sleep all night and I work all day',
      'He\'s a lumberjack and he\'s okay',
      'He sleeps all night and he works all day'
    ];
    var counts = _(lyrics).chain()
      .map(function(line) { return line.split(''); })
      .flatten()
      .reduce(function(hash, l) {
        hash[l] = hash[l] || 0;
        hash[l]++;
        return hash;
      }, {})
      .value();
    assert.equal(counts.a, 16, 'counted all the letters in the song');
    assert.equal(counts.e, 10, 'counted all the letters in the song');
  });

  QUnit.test('select/reject/sortBy', function(assert) {
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    numbers = _(numbers).chain().select(function(n) {
      return n % 2 === 0;
    }).reject(function(n) {
      return n % 4 === 0;
    }).sortBy(function(n) {
      return -n;
    }).value();
    assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
  });

  QUnit.test('select/reject/sortBy in functional style', function(assert) {
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    numbers = _.chain(numbers).select(function(n) {
      return n % 2 === 0;
    }).reject(function(n) {
      return n % 4 === 0;
    }).sortBy(function(n) {
      return -n;
    }).value();
    assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers');
  });

  QUnit.test('reverse/concat/unshift/pop/map', function(assert) {
    var numbers = [1, 2, 3, 4, 5];
    numbers = _(numbers).chain()
      .reverse()
      .concat([5, 5, 5])
      .unshift(17)
      .pop()
      .map(function(n){ return n * 2; })
      .value();
    assert.deepEqual(numbers, [34, 10, 8, 6, 4, 2, 10, 10], 'can chain together array functions.');
  });

  QUnit.test('splice', function(assert) {
    var instance = _([1, 2, 3, 4, 5]).chain();
    assert.deepEqual(instance.splice(1, 3).value(), [1, 5]);
    assert.deepEqual(instance.splice(1, 0).value(), [1, 5]);
    assert.deepEqual(instance.splice(1, 1).value(), [1]);
    assert.deepEqual(instance.splice(0, 1).value(), [], '#397 Can create empty array');
  });

  QUnit.test('shift', function(assert) {
    var instance = _([1, 2, 3]).chain();
    assert.deepEqual(instance.shift().value(), [2, 3]);
    assert.deepEqual(instance.shift().value(), [3]);
    assert.deepEqual(instance.shift().value(), [], '#397 Can create empty array');
  });

  QUnit.test('pop', function(assert) {
    var instance = _([1, 2, 3]).chain();
    assert.deepEqual(instance.pop().value(), [1, 2]);
    assert.deepEqual(instance.pop().value(), [1]);
    assert.deepEqual(instance.pop().value(), [], '#397 Can create empty array');
  });

  QUnit.test('chaining works in small stages', function(assert) {
    var o = _([1, 2, 3, 4]).chain();
    assert.deepEqual(o.filter(function(i) { return i < 3; }).value(), [1, 2]);
    assert.deepEqual(o.filter(function(i) { return i > 2; }).value(), [3, 4]);
  });

  QUnit.test('#1562: Engine proxies for chained functions', function(assert) {
    var wrapped = _(512);
    assert.strictEqual(wrapped.toJSON(), 512);
    assert.strictEqual(wrapped.valueOf(), 512);
    assert.strictEqual(+wrapped, 512);
    assert.strictEqual(wrapped.toString(), '512');
    assert.strictEqual('' + wrapped, '512');
  });

}());