From b51bee90226467f10404661309cabfd83c6821ee Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 24 Jan 2018 13:50:49 +0200 Subject: [PATCH] Add times --- snippets/times.md | 19 +++++++++++++++++++ tag_database | 1 + 2 files changed, 20 insertions(+) create mode 100644 snippets/times.md diff --git a/snippets/times.md b/snippets/times.md new file mode 100644 index 000000000..433cae185 --- /dev/null +++ b/snippets/times.md @@ -0,0 +1,19 @@ +### times + +Iterates over a callback `n` times + +Use `Function.call()` to call `fn` `n` times or until it returns `false`. +Omit the last argument, `context`, to use an `undefined` object (or the global object in non-strict mode). + +```js +const times = (n, fn, context = undefined) => { + let i = 0; + while (fn.call(context, i) !== false && ++i < n) {} +} +``` + +```js +var output = ''; +times(5, i => output += i); +console.log(output); // 01234 +``` diff --git a/tag_database b/tag_database index e942ec1e5..da3fb5c24 100644 --- a/tag_database +++ b/tag_database @@ -209,6 +209,7 @@ symmetricDifferenceWith:array,function tail:array take:array takeRight:array +times:function timeTaken:utility toCamelCase:string,regexp toDecimalMark:utility,math