From aada675cf28fbbf9aa8bc278695edc76ec2a972b Mon Sep 17 00:00:00 2001 From: Bao Pham Date: Wed, 7 Mar 2018 12:37:41 +0800 Subject: [PATCH] Add some() and every() methods for Collection (#216) --- src/Collection.js | 24 +++++++++++++++++++ src/__tests__/Collection-test.js | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/Collection.js b/src/Collection.js index 8cf1a764..3112e322 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -78,6 +78,30 @@ class Collection { return this; } + /** + * Tests whether at-least one path passes the test implemented by the provided callback. + * + * @param {function} callback + * @return {boolean} + */ + some(callback) { + return this.__paths.some( + (path, i, paths) => callback.call(path, path, i, paths) + ); + } + + /** + * Tests whether all paths pass the test implemented by the provided callback. + * + * @param {function} callback + * @return {boolean} + */ + every(callback) { + return this.__paths.every( + (path, i, paths) => callback.call(path, path, i, paths) + ); + } + /** * Executes the callback for every path in the collection and returns a new * collection from the return values (which must be paths). diff --git a/src/__tests__/Collection-test.js b/src/__tests__/Collection-test.js index 94167f0f..d9b389e3 100644 --- a/src/__tests__/Collection-test.js +++ b/src/__tests__/Collection-test.js @@ -234,6 +234,46 @@ describe('Collection API', function() { }); }); + describe('some', function() { + it('lets you test each element of a collection and stops when one passes the test', function() { + const each = jest.genMockFunction().mockImplementation(() => true); + Collection.fromNodes(nodes).some(each); + + expect(each.mock.calls.length).toBe(1); + expect(each.mock.calls[0][0].value).toBe(nodes[0]); + }); + + it('returns true if at least one element passes the test', function() { + const result = Collection.fromNodes(nodes).some((_, i) => i === 1); + expect(result).toBe(true); + }); + + it('returns false if no elements pass the test', function() { + const result = Collection.fromNodes(nodes).some(() => false); + expect(result).toBe(false); + }); + }); + + describe('every', function() { + it('lets you test each element of a collection and stops when one fails the test', function() { + const each = jest.genMockFunction().mockImplementation(() => false); + Collection.fromNodes(nodes).every(each); + + expect(each.mock.calls.length).toBe(1); + expect(each.mock.calls[0][0].value).toBe(nodes[0]); + }); + + it('returns true if all elements pass the test', function() { + const result = Collection.fromNodes(nodes).every(() => true); + expect(result).toBe(true); + }); + + it('returns false if at least one element does not pass the test', function() { + const result = Collection.fromNodes(nodes).every((_, i) => i === 1); + expect(result).toBe(false); + }); + }); + describe('map', function() { it('returns a new collection with mapped values', function() { const root = Collection.fromNodes(nodes);