chaining.js (lodash-3.0.0) | : | chaining.js (lodash-4.0.0) | ||
---|---|---|---|---|
(function() { | (function() { | |||
var _ = typeof require == 'function' ? require('..') : window._; | ||||
module('Chaining'); | QUnit.module('Chaining'); | |||
test('map/flatten/reduce', function() { | QUnit.test('map/flatten/reduce', function(assert) { | |||
var lyrics = [ | var lyrics = [ | |||
'I\'m a lumberjack and I\'m okay', | 'I\'m a lumberjack and I\'m okay', | |||
'I sleep all night and I work all day', | 'I sleep all night and I work all day', | |||
'He\'s a lumberjack and he\'s okay', | 'He\'s a lumberjack and he\'s okay', | |||
'He sleeps all night and he works all day' | 'He sleeps all night and he works all day' | |||
]; | ]; | |||
var counts = _(lyrics).chain() | var counts = _(lyrics).chain() | |||
.map(function(line) { return line.split(''); }) | .map(function(line) { return line.split(''); }) | |||
.flatten() | .flatten() | |||
.reduce(function(hash, l) { | .reduce(function(hash, l) { | |||
hash[l] = hash[l] || 0; | hash[l] = hash[l] || 0; | |||
hash[l]++; | hash[l]++; | |||
return hash; | return hash; | |||
}, {}).value(); | }, {}) | |||
equal(counts.a, 16, 'counted all the letters in the song'); | .value(); | |||
equal(counts.e, 10, 'counted all the letters in the song'); | assert.equal(counts.a, 16, 'counted all the letters in the song'); | |||
assert.equal(counts.e, 10, 'counted all the letters in the song'); | ||||
}); | }); | |||
test('select/reject/sortBy', function() { | QUnit.test('select/reject/sortBy', function(assert) { | |||
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |||
numbers = _(numbers).chain().select(function(n) { | numbers = _(numbers).chain().select(function(n) { | |||
return n % 2 === 0; | return n % 2 === 0; | |||
}).reject(function(n) { | }).reject(function(n) { | |||
return n % 4 === 0; | return n % 4 === 0; | |||
}).sortBy(function(n) { | }).sortBy(function(n) { | |||
return -n; | return -n; | |||
}).value(); | }).value(); | |||
deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers'); | assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers'); | |||
}); | }); | |||
test('select/reject/sortBy in functional style', function() { | QUnit.test('select/reject/sortBy in functional style', function(assert) { | |||
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |||
numbers = _.chain(numbers).select(function(n) { | numbers = _.chain(numbers).select(function(n) { | |||
return n % 2 === 0; | return n % 2 === 0; | |||
}).reject(function(n) { | }).reject(function(n) { | |||
return n % 4 === 0; | return n % 4 === 0; | |||
}).sortBy(function(n) { | }).sortBy(function(n) { | |||
return -n; | return -n; | |||
}).value(); | }).value(); | |||
deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers'); | assert.deepEqual(numbers, [10, 6, 2], 'filtered and reversed the numbers'); | |||
}); | }); | |||
test('reverse/concat/unshift/pop/map', function() { | QUnit.test('reverse/concat/unshift/pop/map', function(assert) { | |||
var numbers = [1, 2, 3, 4, 5]; | var numbers = [1, 2, 3, 4, 5]; | |||
numbers = _(numbers).chain() | numbers = _(numbers).chain() | |||
.reverse() | .reverse() | |||
.concat([5, 5, 5]) | .concat([5, 5, 5]) | |||
.unshift(17) | .unshift(17) | |||
.pop() | .pop() | |||
.map(function(n){ return n * 2; }) | .map(function(n){ return n * 2; }) | |||
.value(); | .value(); | |||
deepEqual(numbers, [34, 10, 8, 6, 4, 2, 10, 10], 'can chain together array f | assert.deepEqual(numbers, [34, 10, 8, 6, 4, 2, 10, 10], 'can chain together | |||
unctions.'); | 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 a | ||||
rray'); | ||||
}); | ||||
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' | ||||
); | ||||
}); | }); | |||
test('chaining works in small stages', function() { | 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(); | var o = _([1, 2, 3, 4]).chain(); | |||
deepEqual(o.filter(function(i) { return i < 3; }).value(), [1, 2]); | assert.deepEqual(o.filter(function(i) { return i < 3; }).value(), [1, 2]); | |||
deepEqual(o.filter(function(i) { return i > 2; }).value(), [3, 4]); | 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'); | ||||
}); | }); | |||
}()); | }()); | |||
End of changes. 12 change blocks. | ||||
15 lines changed or deleted | 50 lines changed or added |