-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update "all", "any" and "reduce" to allow uncurried call
- Loading branch information
Showing
6 changed files
with
121 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,59 @@ | ||
import { pipe } from "../pipe/pipe" | ||
import { isMatch } from "../is-match/is-match" | ||
|
||
const _all = (_fn, _source) => { | ||
const source = Array.isArray(_source) ? _source : [_source] | ||
const fn = Array.isArray(_fn) ? pipe(..._fn) : _fn | ||
|
||
for (let i = 0, length = source.length; i < length; i++) { | ||
if (fn(source[i]) !== true) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Test if all elements of array satisfy function | ||
* Test if all elements of array satisfy a function | ||
* | ||
* @tag Core | ||
* @signature (fn: Function) => (source: Array): boolean | ||
* @see {@link allWith} | ||
* @see {@link isMatch} | ||
* @see {@link any} | ||
* @see {@link anyWith} | ||
* @param {Fn|Fn[]} fn Test function called on each elements | ||
* @param {Array} source Source array to iterate over | ||
* | ||
* @param {Function} fn Function that all elements need to satisfy | ||
* @param {Array} source Source array | ||
* @return {Boolean} True if all elements pass, otherwise false | ||
* | ||
* @return {boolean} | ||
* @name all | ||
* @tag Array | ||
* @signature (fn: Function) => (source: Array): Boolean | ||
* @signature (fn: Function, source: Array): Boolean | ||
* | ||
* @see {@link allWith} | ||
* @see {@link any} | ||
* @see {@link anyWith} | ||
* | ||
* @example | ||
* all(isNumber)([1, 2, 3]) | ||
* // => true | ||
* all(is)([1, "asd", null]) | ||
* | ||
* all(is, [1, "asd", null]) | ||
* // => false | ||
*/ | ||
const all = fn => source => { | ||
for (let i = 0, length = source.length - 1; i <= length; i++) { | ||
if (fn.call(null, source[i]) !== true) { | ||
return false | ||
} | ||
export const all = (...params) => { | ||
// @signature (fn) => (source) | ||
if (params.length <= 1) { | ||
return source => _all(params[0], source) | ||
} | ||
|
||
return true | ||
// @signature (fn, source) | ||
return _all(...params) | ||
} | ||
|
||
const allWith = subset => all(isMatch(subset)) | ||
export const allWith = (...params) => { | ||
// @signature (subset) => (source) | ||
if (params.length <= 1) { | ||
return source => _all(isMatch(params[0]), source) | ||
} | ||
|
||
export { all, allWith } | ||
// @signature (subset, source) | ||
return _all(isMatch(params[0]), params[1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,59 @@ | ||
import { pipe } from "../pipe/pipe" | ||
import { isMatch } from "../is-match/is-match" | ||
|
||
const _any = (_fn, _source) => { | ||
const source = Array.isArray(_source) ? _source : [_source] | ||
const fn = Array.isArray(_fn) ? pipe(..._fn) : _fn | ||
|
||
for (let i = 0, length = source.length; i < length; i++) { | ||
if (fn(source[i]) === true) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* Test if at least one element of array satisfies function | ||
* Test if at least one element of array satisfies a function | ||
* | ||
* @name any | ||
* @tag Core | ||
* @signature (fn: Function) => (source: Array): boolean | ||
* @see {@link isMatch} | ||
* @see {@link all} | ||
* @see {@link allWith} | ||
* @param {Fn|Fn[]} fn Test function called on each elements | ||
* @param {Array} source Source array to iterate over | ||
* | ||
* @param {Function} fn Function to be satisfied | ||
* @param {Array} source Input array | ||
* @return {Boolean} True if at least one element passes, otherwise false | ||
* | ||
* @returns {boolean} True if at least one object passes, false otherwise | ||
* @name any | ||
* @tag Array | ||
* @signature (fn: Function) => (source: Array): Boolean | ||
* @signature (fn: Function, source: Array): Boolean | ||
* | ||
* @see {@link anyWith} | ||
* @see {@link all} | ||
* @see {@link allWith} | ||
* | ||
* @example | ||
* any(isNumber)([1, "string", NaN]) | ||
* // => true | ||
* | ||
* any(is)([null]) | ||
* any([get("id"), is], [{title: ""}, {}]) | ||
* // => false | ||
*/ | ||
const any = fn => source => | ||
(Array.isArray(source) ? source : [source]).some(element => { | ||
const testResult = fn(element) | ||
export const any = (...params) => { | ||
// @signature (fn) => (source) | ||
if (params.length <= 1) { | ||
return source => _any(params[0], source) | ||
} | ||
|
||
return testResult && typeof testResult === "boolean" | ||
}) | ||
// @signature (fn, source) | ||
return _any(...params) | ||
} | ||
|
||
/** | ||
* Test if object properties match any object in input array | ||
* | ||
* @name anyWith | ||
* @tag Core | ||
* @signature (subset: Object) => (source: Object[]): boolean | ||
* @see {@link any} | ||
* @see {@link isMatch} | ||
* @see {@link all} | ||
* @see {@link allWith} | ||
* | ||
* @param {Object} subset Set of properties that should match | ||
* @param {Object[]} source Input array | ||
* | ||
* @returns {boolean} True if at least one object matches, false otherwise | ||
* | ||
* @example | ||
* anyWith({ | ||
* id: isNumber, | ||
* name: "lorem", | ||
* })([ | ||
* { id: "uuid", name: "lorem" }, | ||
* { id: 2, name: "foo" }, | ||
* { id: 3, name: "lorem", foo: "bar" }, | ||
* ]) | ||
* // => true | ||
*/ | ||
const anyWith = subset => any(isMatch(subset)) | ||
export const anyWith = (...params) => { | ||
// @signature (subset) => (source) | ||
if (params.length <= 1) { | ||
return source => _any(isMatch(params[0]), source) | ||
} | ||
|
||
export { any, anyWith } | ||
// @signature (subset, source) | ||
return _any(isMatch(params[0]), params[1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters