Download:
child 26:4e7999b5675c
parent 24:b7d97ca5ea2c
25:9636252f0f93
Anton Shestakov <av6@dwimlabs.net>, Wed, 22 Jun 2016 14:25:54 +0800
backbone.shard: add new methods (from Underscore), test them Namely, these methods are added: - difference - findIndex - findLastIndex - includes - sample

2 файлов изменено, 21 вставок(+), 6 удалений(-) [+]
backbone.shard.js file | annotate | diff | comparison | revisions
test/underscore-methods.js file | annotate | diff | comparison | revisions
--- a/backbone.shard.js Wed Jun 22 13:59:56 2016 +0800
+++ b/backbone.shard.js Wed Jun 22 14:25:54 2016 +0800
@@ -61,12 +61,14 @@
});
// 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',
- 'isEmpty', 'chain'];
+var methods = [
+ '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
@@ -25,6 +25,13 @@
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');
@@ -43,6 +50,9 @@
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() {
@@ -65,6 +75,8 @@
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() {
@@ -103,6 +115,7 @@
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';