Skip to content

Commit aada675

Browse files
baophamfkling
authored andcommittedMar 7, 2018
Add some() and every() methods for Collection (#216)
1 parent 99aaae5 commit aada675

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
 

‎src/Collection.js

+24
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ class Collection {
7878
return this;
7979
}
8080

81+
/**
82+
* Tests whether at-least one path passes the test implemented by the provided callback.
83+
*
84+
* @param {function} callback
85+
* @return {boolean}
86+
*/
87+
some(callback) {
88+
return this.__paths.some(
89+
(path, i, paths) => callback.call(path, path, i, paths)
90+
);
91+
}
92+
93+
/**
94+
* Tests whether all paths pass the test implemented by the provided callback.
95+
*
96+
* @param {function} callback
97+
* @return {boolean}
98+
*/
99+
every(callback) {
100+
return this.__paths.every(
101+
(path, i, paths) => callback.call(path, path, i, paths)
102+
);
103+
}
104+
81105
/**
82106
* Executes the callback for every path in the collection and returns a new
83107
* collection from the return values (which must be paths).

‎src/__tests__/Collection-test.js

+40
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,46 @@ describe('Collection API', function() {
234234
});
235235
});
236236

237+
describe('some', function() {
238+
it('lets you test each element of a collection and stops when one passes the test', function() {
239+
const each = jest.genMockFunction().mockImplementation(() => true);
240+
Collection.fromNodes(nodes).some(each);
241+
242+
expect(each.mock.calls.length).toBe(1);
243+
expect(each.mock.calls[0][0].value).toBe(nodes[0]);
244+
});
245+
246+
it('returns true if at least one element passes the test', function() {
247+
const result = Collection.fromNodes(nodes).some((_, i) => i === 1);
248+
expect(result).toBe(true);
249+
});
250+
251+
it('returns false if no elements pass the test', function() {
252+
const result = Collection.fromNodes(nodes).some(() => false);
253+
expect(result).toBe(false);
254+
});
255+
});
256+
257+
describe('every', function() {
258+
it('lets you test each element of a collection and stops when one fails the test', function() {
259+
const each = jest.genMockFunction().mockImplementation(() => false);
260+
Collection.fromNodes(nodes).every(each);
261+
262+
expect(each.mock.calls.length).toBe(1);
263+
expect(each.mock.calls[0][0].value).toBe(nodes[0]);
264+
});
265+
266+
it('returns true if all elements pass the test', function() {
267+
const result = Collection.fromNodes(nodes).every(() => true);
268+
expect(result).toBe(true);
269+
});
270+
271+
it('returns false if at least one element does not pass the test', function() {
272+
const result = Collection.fromNodes(nodes).every((_, i) => i === 1);
273+
expect(result).toBe(false);
274+
});
275+
});
276+
237277
describe('map', function() {
238278
it('returns a new collection with mapped values', function() {
239279
const root = Collection.fromNodes(nodes);

0 commit comments

Comments
 (0)
Please sign in to comment.