-
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(repeat): Update to allow uncurried call
- Loading branch information
Showing
2 changed files
with
35 additions
and
21 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,29 +1,42 @@ | ||
import { pipe } from "../pipe/pipe" | ||
|
||
const _repeat = (_fn, count = 0) => { | ||
const result = [] | ||
const fn = Array.isArray(_fn) ? pipe(..._fn) : _fn | ||
const isFunction = typeof fn === "function" | ||
|
||
for (let i = 0; i < count; i++) { | ||
result.push(isFunction ? fn(i) : fn) | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Return an array of fixed size containing a specified value or function result | ||
* | ||
* @tag Array | ||
* @signature (fn: Function|mixed) => (count:number): Array | ||
* | ||
* @param {Function|mixed} fn Function or value to repeat | ||
* @param {number} count Number of times | ||
* @param {Function|mixed} fn Function or value to repeat | ||
* @param {Number} source Number of times | ||
* | ||
* @return {Array} | ||
* | ||
* @name repeat | ||
* @tag Array | ||
* @signature (fn: Function|mixed) => (count: Number): Array | ||
* | ||
* @example | ||
* repeat(2)(3) | ||
* // => [2, 2, 2] | ||
* | ||
* repeat(index=>index+1)(3) | ||
* repeat(index => index + 1, 3) | ||
* // => [1, 2, 3] | ||
*/ | ||
const repeat = fn => (count = 0) => { | ||
const result = [] | ||
|
||
for (let i = 0; i < count; i++) { | ||
result.push(typeof fn === "function" ? fn.call(null, i) : fn) | ||
export const repeat = (...params) => { | ||
// @signature (fn) => (source) | ||
if (params.length <= 1) { | ||
return source => _repeat(params[0], source) | ||
} | ||
|
||
return result | ||
// @signature (fn, source) | ||
return _repeat(...params) | ||
} | ||
|
||
export { repeat } |
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