--- a/backbone.shard.js Wed Jun 22 13:59:56 2016 +0800
+++ b/backbone.shard.js Wed Jun 22 14:25:54 2016 +0800
// Underscore methods that we want to implement on the Shard.
-var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
- 'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
- 'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
- 'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
- 'tail', 'drop', 'last', 'without', 'indexOf', 'shuffle', 'lastIndexOf',
+ 'all', 'any', 'chain', 'collect', 'contains', 'detect', 'difference',
+ 'drop', 'each', 'every', 'filter', 'find', 'findIndex', 'findLastIndex',
+ 'first', 'foldl', 'foldr', 'forEach', 'head', 'include', 'includes',
+ 'indexOf', 'initial', 'inject', 'invoke', 'isEmpty', 'last', 'lastIndexOf',
+ 'map', 'max', 'min', 'reduce', 'reduceRight', 'reject', 'rest', 'sample',
+ 'select', 'shuffle', 'size', 'some', 'tail', 'take', 'toArray', 'without'
// Mix in each Underscore method as a proxy to `Shard#models`.
_.each(methods, function(method) {
--- a/test/underscore-methods.js Wed Jun 22 13:59:56 2016 +0800
+++ b/test/underscore-methods.js Wed Jun 22 14:25:54 2016 +0800
equal(shard.isEmpty(), false, 'isEmpty() works');
deepEqual(shard.toArray(), [a, b, c], 'toArray() works');
+ equal(shard.findIndex(function(model) {
+ return model.get('data').charCodeAt(0) % 2;
+ }), 0, 'findIndex() works');
+ equal(shard.findLastIndex(function(model) {
+ return model.get('data').charCodeAt(0) % 2;
+ }), 2, 'findLastIndex() works');
equal(shard.contains(b), true, 'contains() works');
deepEqual(shard.without(b), [a, c], 'without() works');
var sh = shard.shuffle().reduce(function(memo, model) { return memo += model.get('data'); }, '');
ok(sh.length == 3 && _(sh).contains('a') && _(sh).contains('b') && _(sh).contains('c'), 'shuffle() works');
+ var s = shard.sample(999).reduce(function(memo, model) { return memo += model.get('data'); }, '');
+ ok(s.length == 3 && _(s).contains('a') && _(s).contains('b') && _(s).contains('c'), 'sample() works');
test('Underscore methods: searching and filtering', function() {
deepEqual(shard.reject(function(model) {
return model.get('data') == 'b';
}), [a, c], 'reject() works');
+ deepEqual(shard.difference([a, c]), [b], 'difference() works');
test('Underscore methods: iterating over models', function() {
deepEqual(shard.tail(), [b, c], 'tail() works');
deepEqual(shard.drop(), [b, c], 'drop() works');
deepEqual(shard.countBy('data'), {a: 1, b: 1, c: 1}, 'countBy() works');
+ equal(shard.includes(b), true, 'includes() works');
deepEqual(shard.select(function(model) {
return model.get('data') != 'b';