Skip to content

Commit d17168d

Browse files
authored
Merge pull request documentcloud#239 from jgonggrijp/feature/atleast-atmost
containsAtLeast and containsAtMost
2 parents ed5eae3 + e6b8692 commit d17168d

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

docs/underscore.collections.walk.js.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Documentation should use [Journo](https://github.com/jashkenas/journo) formats a
1111
map: function(obj, strategy, visitor, context)
1212
pluck: function(obj, propertyName)
1313
pluckRec: function(obj, propertyName)
14+
containsAtLeast: function(list, count, value)
15+
containsAtMost: function(list, count, value)
1416
_.walk.collect = _.walk.map;
15-

test/collections.walk.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ $(document).ready(function() {
2121
};
2222
};
2323

24+
var getArrayValues = function() {
25+
return ["a", "a", "a", "a", "b", "c", "d", "e" ];
26+
};
27+
2428
QUnit.test("basic", function(assert) {
2529
// Updates the value of `node` to be the sum of the values of its subtrees.
2630
// Ignores leaf nodes.
@@ -213,4 +217,20 @@ $(document).ready(function() {
213217
assert.equal(_.walk.map(tree, _.walk.postorder, _.identity).length, 14, 'default map still works');
214218
assert.equal(_.walk.pluckRec(tree, 'val').join(''), '0123456', 'default pluckRec still works');
215219
});
220+
221+
test("containsAtLeast", function(){
222+
var array = getArrayValues();
223+
224+
equal(_.walk.containsAtLeast(array, 3, "a"), true, "list contains at least 3 items");
225+
equal(_.walk.containsAtLeast(array, 1, "b"), true, "list contains at least 1 items");
226+
equal(_.walk.containsAtLeast(array, 1, "f"), false, "list doesn't contain item for that value");
227+
});
228+
229+
test("containsAtMost", function(){
230+
var array = getArrayValues();
231+
232+
equal(_.walk.containsAtMost(array, 4, "a"), true, "list contains at most 4 items");
233+
equal(_.walk.containsAtMost(array, 1, "b"), true, "list contains at most 1 value");
234+
equal(_.walk.containsAtMost(array, 1, "f"), true, "list contains at most 1 value");
235+
});
216236
});

underscore.collections.walk.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@
168168
return visitor(subResults || leafMemo, value, key, parent);
169169
};
170170
return walkImpl(obj, this._traversalStrategy, null, reducer, context, true);
171+
},
172+
173+
// Determine if the array contains a number of repated values
174+
containsAtLeast: function(list, count, value) {
175+
var filtered = _.filter(list, function(item) {
176+
return _.isEqual(item, value);
177+
});
178+
return _.gte(_.size(filtered), count);
179+
},
180+
181+
// Determine if the array contains a number of repated values
182+
containsAtMost: function(list, count, value) {
183+
var filtered = _.filter(list, function(item) {
184+
return _.isEqual(item, value);
185+
});
186+
return _.lte(_.size(filtered), count);
171187
}
172188
};
173189

0 commit comments

Comments
 (0)