Merge branch 'master' into compact-whitespace
This commit is contained in:
12
README.md
12
README.md
@ -664,6 +664,7 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr
|
|||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const sum = pipeAsyncFunctions(
|
const sum = pipeAsyncFunctions(
|
||||||
x => x + 1,
|
x => x + 1,
|
||||||
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
|
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
|
||||||
@ -2312,6 +2313,7 @@ Use `Array.prototype.filter()` to find array elements that return truthy values
|
|||||||
The `func` is invoked with three arguments (`value, index, array`).
|
The `func` is invoked with three arguments (`value, index, array`).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const remove = (arr, func) =>
|
const remove = (arr, func) =>
|
||||||
Array.isArray(arr)
|
Array.isArray(arr)
|
||||||
? arr.filter(func).reduce((acc, val) => {
|
? arr.filter(func).reduce((acc, val) => {
|
||||||
@ -4616,7 +4618,11 @@ Loop through an array of functions containing asynchronous events, calling `next
|
|||||||
```js
|
```js
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const last = fns[fns.length - 1];
|
||||||
|
const next = () => {
|
||||||
|
const fn = fns[curr++];
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
|
};
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@ -4632,6 +4638,10 @@ chainAsync([
|
|||||||
},
|
},
|
||||||
next => {
|
next => {
|
||||||
console.log('1 second');
|
console.log('1 second');
|
||||||
|
setTimeout(next, 1000);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
console.log('2 second');
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
3997
coverage/lcov.info
3997
coverage/lcov.info
File diff suppressed because it is too large
Load Diff
4
dist/_30s.es5.js
vendored
4
dist/_30s.es5.js
vendored
@ -334,9 +334,11 @@
|
|||||||
};
|
};
|
||||||
var chainAsync = function chainAsync(fns) {
|
var chainAsync = function chainAsync(fns) {
|
||||||
var curr = 0;
|
var curr = 0;
|
||||||
|
var last = fns[fns.length - 1];
|
||||||
|
|
||||||
var next = function next() {
|
var next = function next() {
|
||||||
return fns[curr++](next);
|
var fn = fns[curr++];
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
};
|
};
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
|||||||
2
dist/_30s.es5.min.js
vendored
2
dist/_30s.es5.min.js
vendored
File diff suppressed because one or more lines are too long
6
dist/_30s.esm.js
vendored
6
dist/_30s.esm.js
vendored
@ -108,7 +108,11 @@ const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperC
|
|||||||
const castArray = val => (Array.isArray(val) ? val : [val]);
|
const castArray = val => (Array.isArray(val) ? val : [val]);
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const last = fns[fns.length - 1];
|
||||||
|
const next = () => {
|
||||||
|
const fn = fns[curr++];
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
|
};
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
const chunk = (arr, size) =>
|
const chunk = (arr, size) =>
|
||||||
|
|||||||
6
dist/_30s.js
vendored
6
dist/_30s.js
vendored
@ -114,7 +114,11 @@
|
|||||||
const castArray = val => (Array.isArray(val) ? val : [val]);
|
const castArray = val => (Array.isArray(val) ? val : [val]);
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const last = fns[fns.length - 1];
|
||||||
|
const next = () => {
|
||||||
|
const fn = fns[curr++];
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
|
};
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
const chunk = (arr, size) =>
|
const chunk = (arr, size) =>
|
||||||
|
|||||||
@ -125,7 +125,8 @@ Object<span class="token punctuation">.</span><span class="token function">assig
|
|||||||
<span class="token keyword">const</span> fn <span class="token operator">=</span> <span class="token function">overArgs</span><span class="token punctuation">((</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> y<span class="token punctuation">], [</span>square<span class="token punctuation">,</span> double<span class="token punctuation">]);</span>
|
<span class="token keyword">const</span> fn <span class="token operator">=</span> <span class="token function">overArgs</span><span class="token punctuation">((</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> y<span class="token punctuation">], [</span>square<span class="token punctuation">,</span> double<span class="token punctuation">]);</span>
|
||||||
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [81, 6]</span>
|
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [81, 6]</span>
|
||||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="pipeasyncfunctions">pipeAsyncFunctions</h4><p>Performs left-to-right function composition for asynchronous functions.</p><p>Use <code>Array.prototype.reduce()</code> with the spread operator (<code>...</code>) to perform left-to-right function composition using <code>Promise.then()</code>. The functions can return a combination of: simple values, <code>Promise</code>'s, or they can be defined as <code>async</code> ones returning through <code>await</code>. All functions must be unary.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pipeAsyncFunctions</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>fns<span class="token punctuation">)</span> <span class="token operator">=></span> arg <span class="token operator">=></span> fns<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>p<span class="token punctuation">,</span> f<span class="token punctuation">)</span> <span class="token operator">=></span> p<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>f<span class="token punctuation">),</span> Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>arg<span class="token punctuation">));</span>
|
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="pipeasyncfunctions">pipeAsyncFunctions</h4><p>Performs left-to-right function composition for asynchronous functions.</p><p>Use <code>Array.prototype.reduce()</code> with the spread operator (<code>...</code>) to perform left-to-right function composition using <code>Promise.then()</code>. The functions can return a combination of: simple values, <code>Promise</code>'s, or they can be defined as <code>async</code> ones returning through <code>await</code>. All functions must be unary.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pipeAsyncFunctions</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>fns<span class="token punctuation">)</span> <span class="token operator">=></span> arg <span class="token operator">=></span> fns<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>p<span class="token punctuation">,</span> f<span class="token punctuation">)</span> <span class="token operator">=></span> p<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>f<span class="token punctuation">),</span> Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>arg<span class="token punctuation">));</span>
|
||||||
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> sum <span class="token operator">=</span> <span class="token function">pipeAsyncFunctions</span><span class="token punctuation">(</span>
|
</pre><label class="collapse">examples</label><pre class="section card-examples language-js">
|
||||||
|
<span class="token keyword">const</span> sum <span class="token operator">=</span> <span class="token function">pipeAsyncFunctions</span><span class="token punctuation">(</span>
|
||||||
x <span class="token operator">=></span> x <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
x <span class="token operator">=></span> x <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
||||||
x <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span>resolve <span class="token operator">=></span> <span class="token function">setTimeout</span><span class="token punctuation">(()</span> <span class="token operator">=></span> <span class="token function">resolve</span><span class="token punctuation">(</span>x <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">),</span> <span class="token number">1000</span><span class="token punctuation">)),</span>
|
x <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span>resolve <span class="token operator">=></span> <span class="token function">setTimeout</span><span class="token punctuation">(()</span> <span class="token operator">=></span> <span class="token function">resolve</span><span class="token punctuation">(</span>x <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">),</span> <span class="token number">1000</span><span class="token punctuation">)),</span>
|
||||||
x <span class="token operator">=></span> x <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">,</span>
|
x <span class="token operator">=></span> x <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">,</span>
|
||||||
|
|||||||
@ -121,7 +121,11 @@ console<span class="token punctuation">.</span><span class="token function">log<
|
|||||||
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">freddyBound</span><span class="token punctuation">(</span><span class="token string">'hi'</span><span class="token punctuation">,</span> <span class="token string">'!'</span><span class="token punctuation">));</span> <span class="token comment">// 'hi fred!'</span>
|
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">freddyBound</span><span class="token punctuation">(</span><span class="token string">'hi'</span><span class="token punctuation">,</span> <span class="token string">'!'</span><span class="token punctuation">));</span> <span class="token comment">// 'hi fred!'</span>
|
||||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="chainasync">chainAsync</h4><p>Chains asynchronous functions.</p><p>Loop through an array of functions containing asynchronous events, calling <code>next</code> when each asynchronous event has completed.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">chainAsync</span> <span class="token operator">=</span> fns <span class="token operator">=></span> <span class="token punctuation">{</span>
|
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="chainasync">chainAsync</h4><p>Chains asynchronous functions.</p><p>Loop through an array of functions containing asynchronous events, calling <code>next</code> when each asynchronous event has completed.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">chainAsync</span> <span class="token operator">=</span> fns <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||||
<span class="token keyword">let</span> curr <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
|
<span class="token keyword">let</span> curr <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
|
||||||
<span class="token keyword">const</span> <span class="token function-variable function">next</span> <span class="token operator">=</span> <span class="token punctuation">()</span> <span class="token operator">=></span> fns<span class="token punctuation">[</span>curr<span class="token operator">++</span><span class="token punctuation">](</span>next<span class="token punctuation">);</span>
|
<span class="token keyword">const</span> last <span class="token operator">=</span> fns<span class="token punctuation">[</span>fns<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">];</span>
|
||||||
|
<span class="token keyword">const</span> <span class="token function-variable function">next</span> <span class="token operator">=</span> <span class="token punctuation">()</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||||
|
<span class="token keyword">const</span> fn <span class="token operator">=</span> fns<span class="token punctuation">[</span>curr<span class="token operator">++</span><span class="token punctuation">];</span>
|
||||||
|
fn <span class="token operator">===</span> last <span class="token operator">?</span> <span class="token function">fn</span><span class="token punctuation">() :</span> <span class="token function">fn</span><span class="token punctuation">(</span>next<span class="token punctuation">);
|
||||||
|
};</span>
|
||||||
<span class="token function">next</span><span class="token punctuation">();
|
<span class="token function">next</span><span class="token punctuation">();
|
||||||
};</span>
|
};</span>
|
||||||
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">chainAsync</span><span class="token punctuation">([</span>
|
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">chainAsync</span><span class="token punctuation">([</span>
|
||||||
@ -130,7 +134,11 @@ console<span class="token punctuation">.</span><span class="token function">log<
|
|||||||
<span class="token function">setTimeout</span><span class="token punctuation">(</span>next<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">);
|
<span class="token function">setTimeout</span><span class="token punctuation">(</span>next<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">);
|
||||||
},</span>
|
},</span>
|
||||||
next <span class="token operator">=></span> <span class="token punctuation">{</span>
|
next <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||||
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'1 second'</span><span class="token punctuation">);
|
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'1 second'</span><span class="token punctuation">);</span>
|
||||||
|
<span class="token function">setTimeout</span><span class="token punctuation">(</span>next<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">);
|
||||||
|
},
|
||||||
|
()</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||||
|
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'2 second'</span><span class="token punctuation">);
|
||||||
}
|
}
|
||||||
]);</span>
|
]);</span>
|
||||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="compose">compose</h4><p>Performs right-to-left function composition.</p><p>Use <code>Array.prototype.reduce()</code> to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">compose</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>fns<span class="token punctuation">)</span> <span class="token operator">=></span> fns<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>f<span class="token punctuation">,</span> g<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token function">g</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)));</span>
|
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="compose">compose</h4><p>Performs right-to-left function composition.</p><p>Use <code>Array.prototype.reduce()</code> to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">compose</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>fns<span class="token punctuation">)</span> <span class="token operator">=></span> fns<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>f<span class="token punctuation">,</span> g<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token function">g</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)));</span>
|
||||||
|
|||||||
@ -405,7 +405,8 @@
|
|||||||
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="reject">reject</h4><p>Takes a predicate and array, like <code>Array.prototype.filter()</code>, but only keeps <code>x</code> if <code>pred(x) === false</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">reject</span> <span class="token operator">=</span> <span class="token punctuation">(</span>pred<span class="token punctuation">,</span> array<span class="token punctuation">)</span> <span class="token operator">=></span> array<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=> !</span><span class="token function">pred</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">));</span>
|
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="reject">reject</h4><p>Takes a predicate and array, like <code>Array.prototype.filter()</code>, but only keeps <code>x</code> if <code>pred(x) === false</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">reject</span> <span class="token operator">=</span> <span class="token punctuation">(</span>pred<span class="token punctuation">,</span> array<span class="token punctuation">)</span> <span class="token operator">=></span> array<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=> !</span><span class="token function">pred</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">));</span>
|
||||||
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">reject</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">, [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 3, 5]</span>
|
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">reject</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">, [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 3, 5]</span>
|
||||||
<span class="token function">reject</span><span class="token punctuation">(</span>word <span class="token operator">=></span> word<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">, [</span><span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'Pear'</span><span class="token punctuation">,</span> <span class="token string">'Kiwi'</span><span class="token punctuation">,</span> <span class="token string">'Banana'</span><span class="token punctuation">]);</span> <span class="token comment">// ['Pear', 'Kiwi']</span>
|
<span class="token function">reject</span><span class="token punctuation">(</span>word <span class="token operator">=></span> word<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">, [</span><span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'Pear'</span><span class="token punctuation">,</span> <span class="token string">'Kiwi'</span><span class="token punctuation">,</span> <span class="token string">'Banana'</span><span class="token punctuation">]);</span> <span class="token comment">// ['Pear', 'Kiwi']</span>
|
||||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="remove">remove</h4><p>Removes elements from an array for which the given function returns <code>false</code>.</p><p>Use <code>Array.prototype.filter()</code> to find array elements that return truthy values and <code>Array.prototype.reduce()</code> to remove elements using <code>Array.prototype.splice()</code>. The <code>func</code> is invoked with three arguments (<code>value, index, array</code>).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">remove</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span>
|
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="remove">remove</h4><p>Removes elements from an array for which the given function returns <code>false</code>.</p><p>Use <code>Array.prototype.filter()</code> to find array elements that return truthy values and <code>Array.prototype.reduce()</code> to remove elements using <code>Array.prototype.splice()</code>. The <code>func</code> is invoked with three arguments (<code>value, index, array</code>).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js">
|
||||||
|
<span class="token keyword">const</span> <span class="token function-variable function">remove</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span>
|
||||||
Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
|
Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
|
||||||
<span class="token operator">?</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>func<span class="token punctuation">).</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
|
<span class="token operator">?</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>func<span class="token punctuation">).</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||||
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>val<span class="token punctuation">),</span> <span class="token number">1</span><span class="token punctuation">);</span>
|
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>val<span class="token punctuation">),</span> <span class="token number">1</span><span class="token punctuation">);</span>
|
||||||
|
|||||||
@ -370,7 +370,7 @@
|
|||||||
"archived": false
|
"archived": false
|
||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"hash": "8c245a7fc94edffaf5cae4c28f37ed2e989772a9663a5f5bce98147f708a712e"
|
"hash": "ae8f373cb6c661896ce4256e9f8a2f7f9c14f1699651d366af038f66a938f70e"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -3348,7 +3348,7 @@
|
|||||||
"archived": false
|
"archived": false
|
||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"hash": "4815876fd6dbb17ad34c0d8918e7a72d837104f9beee7dc51b0fa73057b9e83e"
|
"hash": "0b04f5fe668888db0dc360535cd999669258019f0682eb6e4ad3a1164e408d13"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -3720,7 +3720,7 @@
|
|||||||
"archived": false
|
"archived": false
|
||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"hash": "ec9cb9384817f84cf0bacd62a23b69b2304fa2cf0352b16d3950b21d48c04f11"
|
"hash": "536833a64ce0c000b82327ed1bb9bcb82e35b237f75aefcca0e0d2def4e9cb63"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -534,9 +534,9 @@
|
|||||||
"fileName": "chainAsync.md",
|
"fileName": "chainAsync.md",
|
||||||
"text": "Chains asynchronous functions.\n\nLoop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.",
|
"text": "Chains asynchronous functions.\n\nLoop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.",
|
||||||
"codeBlocks": {
|
"codeBlocks": {
|
||||||
"es6": "const chainAsync = fns => {\n let curr = 0;\n const next = () => fns[curr++](next);\n next();\n};",
|
"es6": "const chainAsync = fns => {\n let curr = 0;\n const last = fns[fns.length - 1];\n const next = () => {\n const fn = fns[curr++];\n fn === last ? fn() : fn(next);\n };\n next();\n};",
|
||||||
"es5": "var chainAsync = function chainAsync(fns) {\n var curr = 0;\n\n var next = function next() {\n return fns[curr++](next);\n };\n\n next();\n};",
|
"es5": "var chainAsync = function chainAsync(fns) {\n var curr = 0;\n var last = fns[fns.length - 1];\n\n var next = function next() {\n var fn = fns[curr++];\n fn === last ? fn() : fn(next);\n };\n\n next();\n};",
|
||||||
"example": "chainAsync([\n next => {\n console.log('0 seconds');\n setTimeout(next, 1000);\n },\n next => {\n console.log('1 second');\n }\n]);"
|
"example": "chainAsync([\n next => {\n console.log('0 seconds');\n setTimeout(next, 1000);\n },\n next => {\n console.log('1 second');\n setTimeout(next, 1000);\n },\n () => {\n console.log('2 second');\n }\n]);"
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"function",
|
"function",
|
||||||
@ -545,7 +545,7 @@
|
|||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"archived": false,
|
"archived": false,
|
||||||
"hash": "8c245a7fc94edffaf5cae4c28f37ed2e989772a9663a5f5bce98147f708a712e"
|
"hash": "ae8f373cb6c661896ce4256e9f8a2f7f9c14f1699651d366af038f66a938f70e"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -4930,7 +4930,7 @@
|
|||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"archived": false,
|
"archived": false,
|
||||||
"hash": "4815876fd6dbb17ad34c0d8918e7a72d837104f9beee7dc51b0fa73057b9e83e"
|
"hash": "0b04f5fe668888db0dc360535cd999669258019f0682eb6e4ad3a1164e408d13"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -5477,7 +5477,7 @@
|
|||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"archived": false,
|
"archived": false,
|
||||||
"hash": "ec9cb9384817f84cf0bacd62a23b69b2304fa2cf0352b16d3950b21d48c04f11"
|
"hash": "536833a64ce0c000b82327ed1bb9bcb82e35b237f75aefcca0e0d2def4e9cb63"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -7,7 +7,11 @@ Loop through an array of functions containing asynchronous events, calling `next
|
|||||||
```js
|
```js
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const last = fns[fns.length - 1];
|
||||||
|
const next = () => {
|
||||||
|
const fn = fns[curr++];
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
|
};
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@ -20,6 +24,10 @@ chainAsync([
|
|||||||
},
|
},
|
||||||
next => {
|
next => {
|
||||||
console.log('1 second');
|
console.log('1 second');
|
||||||
|
setTimeout(next, 1000);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
console.log('2 second');
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -11,6 +11,7 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const sum = pipeAsyncFunctions(
|
const sum = pipeAsyncFunctions(
|
||||||
x => x + 1,
|
x => x + 1,
|
||||||
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
|
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
|
||||||
|
|||||||
@ -6,6 +6,7 @@ Use `Array.prototype.filter()` to find array elements that return truthy values
|
|||||||
The `func` is invoked with three arguments (`value, index, array`).
|
The `func` is invoked with three arguments (`value, index, array`).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const remove = (arr, func) =>
|
const remove = (arr, func) =>
|
||||||
Array.isArray(arr)
|
Array.isArray(arr)
|
||||||
? arr.filter(func).reduce((acc, val) => {
|
? arr.filter(func).reduce((acc, val) => {
|
||||||
|
|||||||
@ -1868,4 +1868,5 @@ const speechSynthesis = message => {
|
|||||||
const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0);
|
const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0);
|
||||||
|
|
||||||
|
|
||||||
module.exports = {all,allEqual,any,approximatelyEqual,arrayToCSV,arrayToHtmlList,ary,atob,attempt,average,averageBy,bifurcate,bifurcateBy,bind,bindAll,bindKey,binomialCoefficient,bottomVisible,btoa,byteSize,call,capitalize,capitalizeEveryWord,castArray,chainAsync,chunk,clampNumber,cloneRegExp,coalesce,coalesceFactory,collectInto,colorize,compact,compactWhitespace,compose,composeRight,converge,copyToClipboard,countBy,counter,countOccurrences,createElement,createEventHub,CSVToArray,CSVToJSON,currentURL,curry,dayOfYear,debounce,decapitalize,deepClone,deepFlatten,deepFreeze,deepMapKeys,defaults,defer,degreesToRads,delay,detectDeviceType,difference,differenceBy,differenceWith,dig,digitize,distance,drop,dropRight,dropRightWhile,dropWhile,elementContains,elementIsVisibleInViewport,elo,equals,escapeHTML,escapeRegExp,everyNth,extendHex,factorial,fibonacci,filterFalsy,filterNonUnique,filterNonUniqueBy,findKey,findLast,findLastIndex,findLastKey,flatten,flattenObject,flip,forEachRight,formatDuration,forOwn,forOwnRight,fromCamelCase,functionName,functions,gcd,geometricProgression,get,getColonTimeFromDate,getDaysDiffBetweenDates,getImages,getMeridiemSuffixOfInteger,getScrollPosition,getStyle,getType,getURLParameters,groupBy,hammingDistance,hasClass,hasFlags,hashBrowser,hashNode,head,hexToRGB,hide,httpGet,httpPost,httpsRedirect,hz,indentString,indexOfAll,initial,initialize2DArray,initializeArrayWithRange,initializeArrayWithRangeRight,initializeArrayWithValues,initializeNDArray,inRange,insertAfter,insertBefore,intersection,intersectionBy,intersectionWith,invertKeyValues,is,isAbsoluteURL,isAfterDate,isAnagram,isArrayLike,isBeforeDate,isBoolean,isBrowser,isBrowserTabFocused,isDivisible,isDuplexStream,isEmpty,isEven,isFunction,isLowerCase,isNegativeZero,isNil,isNull,isNumber,isObject,isObjectLike,isPlainObject,isPrime,isPrimitive,isPromiseLike,isReadableStream,isSameDate,isSorted,isStream,isString,isSymbol,isTravisCI,isUndefined,isUpperCase,isValidJSON,isWritableStream,join,JSONtoCSV,JSONToFile,last,lcm,longestItem,lowercaseKeys,luhnCheck,mapKeys,mapObject,mapString,mapValues,mask,matches,matchesWith,maxBy,maxDate,maxN,median,memoize,merge,midpoint,minBy,minDate,minN,mostPerformant,negate,nest,nodeListToArray,none,nthArg,nthElement,objectFromPairs,objectToPairs,observeMutations,off,offset,omit,omitBy,on,once,onUserInputChange,orderBy,over,overArgs,pad,palindrome,parseCookie,partial,partialRight,partition,percentile,permutations,pick,pickBy,pipeAsyncFunctions,pipeFunctions,pluralize,powerset,prefix,prettyBytes,primes,promisify,pull,pullAtIndex,pullAtValue,pullBy,radsToDegrees,randomHexColorCode,randomIntArrayInRange,randomIntegerInRange,randomNumberInRange,readFileLines,rearg,recordAnimationFrames,redirect,reducedFilter,reduceSuccessive,reduceWhich,reject,remove,removeNonASCII,renameKeys,reverseString,RGBToHex,round,runAsync,runPromisesInSeries,sample,sampleSize,scrollToTop,sdbm,serializeCookie,setStyle,shallowClone,shank,show,shuffle,similarity,size,sleep,smoothScroll,sortCharactersInString,sortedIndex,sortedIndexBy,sortedLastIndex,sortedLastIndexBy,splitLines,spreadOver,stableSort,standardDeviation,stringPermutations,stripHTMLTags,sum,sumBy,sumPower,symmetricDifference,symmetricDifferenceBy,symmetricDifferenceWith,tail,take,takeRight,takeRightWhile,takeWhile,throttle,times,timeTaken,toCamelCase,toCurrency,toDecimalMark,toggleClass,toHash,toKebabCase,tomorrow,toOrdinalSuffix,toSafeInteger,toSnakeCase,toTitleCase,transform,triggerEvent,truncateString,truthCheckCollection,unary,uncurry,unescapeHTML,unflattenObject,unfold,union,unionBy,unionWith,uniqueElements,uniqueElementsBy,uniqueElementsByRight,uniqueSymmetricDifference,untildify,unzip,unzipWith,URLJoin,UUIDGeneratorBrowser,UUIDGeneratorNode,validateNumber,when,without,words,xProd,yesNo,zip,zipObject,zipWith,binarySearch,celsiusToFahrenheit,cleanObj,collatz,countVowels,factors,fahrenheitToCelsius,fibonacciCountUntilNum,fibonacciUntilNum,heronArea,howManyTimes,httpDelete,httpPut,isArmstrongNumber,isSimilar,JSONToDate,kmphToMph,levenshteinDistance,mphToKmph,pipeLog,quickSort,removeVowels,solveRPN,speechSynthesis,squareSum}
|
module.exports = {all,allEqual,any,approximatelyEqual,arrayToCSV,arrayToHtmlList,ary,atob,attempt,average,averageBy,bifurcate,bifurcateBy,bind,bindAll,bindKey,binomialCoefficient,bottomVisible,btoa,byteSize,call,capitalize,capitalizeEveryWord,castArray,chainAsync,chunk,clampNumber,cloneRegExp,coalesce,coalesceFactory,collectInto,colorize,compact,compactWhitespace,compose,composeRight,converge,copyToClipboard,countBy,counter,countOccurrences,createElement,createEventHub,CSVToArray,CSVToJSON,currentURL,curry,dayOfYear,debounce,decapitalize,deepClone,deepFlatten,deepFreeze,deepMapKeys,defaults,defer,degreesToRads,delay,detectDeviceType,difference,differenceBy,differenceWith,dig,digitize,distance,drop,dropRight,dropRightWhile,dropWhile,elementContains,elementIsVisibleInViewport,elo,equals,escapeHTML,escapeRegExp,everyNth,extendHex,factorial,fibonacci,filterFalsy,filterNonUnique,filterNonUniqueBy,findKey,findLast,findLastIndex,findLastKey,flatten,flattenObject,flip,forEachRight,formatDuration,forOwn,forOwnRight,fromCamelCase,functionName,functions,gcd,geometricProgression,get,getColonTimeFromDate,getDaysDiffBetweenDates,getImages,getMeridiemSuffixOfInteger,getScrollPosition,getStyle,getType,getURLParameters,groupBy,hammingDistance,hasClass,hasFlags,hashBrowser,hashNode,head,hexToRGB,hide,httpGet,httpPost,httpsRedirect,hz,indentString,indexOfAll,initial,initialize2DArray,initializeArrayWithRange,initializeArrayWithRangeRight,initializeArrayWithValues,initializeNDArray,inRange,insertAfter,insertBefore,intersection,intersectionBy,intersectionWith,invertKeyValues,is,isAbsoluteURL,isAfterDate,isAnagram,isArrayLike,isBeforeDate,isBoolean,isBrowser,isBrowserTabFocused,isDivisible,isDuplexStream,isEmpty,isEven,isFunction,isLowerCase,isNegativeZero,isNil,isNull,isNumber,isObject,isObjectLike,isPlainObject,isPrime,isPrimitive,isPromiseLike,isReadableStream,isSameDate,isSorted,isStream,isString,isSymbol,isTravisCI,isUndefined,isUpperCase,isValidJSON,isWritableStream,join,JSONtoCSV,JSONToFile,last,lcm,longestItem,lowercaseKeys,luhnCheck,mapKeys,mapObject,mapString,mapValues,mask,matches,matchesWith,maxBy,maxDate,maxN,median,memoize,merge,midpoint,minBy,minDate,minN,mostPerformant,negate,nest,nodeListToArray,none,nthArg,nthElement,objectFromPairs,objectToPairs,observeMutations,off,offset,omit,omitBy,on,once,onUserInputChange,orderBy,over,overArgs,pad,palindrome,parseCookie,partial,partialRight,partition,percentile,permutations,pick,pickBy,pipeAsyncFunctions,pipeFunctions,pluralize,powerset,prefix,prettyBytes,primes,promisify,pull,pullAtIndex,pullAtValue,pullBy,radsToDegrees,randomHexColorCode,randomIntArrayInRange,randomIntegerInRange,randomNumberInRange,readFileLines,rearg,recordAnimationFrames,redirect,reducedFilter,reduceSuccessive,reduceWhich,reject,remove,removeNonASCII,renameKeys,reverseString,RGBToHex,round,runAsync,runPromisesInSeries,sample,sampleSize,scrollToTop,sdbm,serializeCookie,setStyle,shallowClone,shank,show,shuffle,similarity,size,sleep,smoothScroll,sortCharactersInString,sortedIndex,sortedIndexBy,sortedLastIndex,sortedLastIndexBy,splitLines,spreadOver,stableSort,standardDeviation,stringPermutations,stripHTMLTags,sum,sumBy,sumPower,symmetricDifference,symmetricDifferenceBy,symmetricDifferenceWith,tail,take,takeRight,takeRightWhile,takeWhile,throttle,times,timeTaken,toCamelCase,toCurrency,toDecimalMark,toggleClass,toHash,toKebabCase,tomorrow,toOrdinalSuffix,toSafeInteger,toSnakeCase,toTitleCase,transform,triggerEvent,truncateString,truthCheckCollection,unary,uncurry,unescapeHTML,unflattenObject,unfold,union,unionBy,unionWith,uniqueElements,uniqueElementsBy,uniqueElementsByRight,uniqueSymmetricDifference,untildify,unzip,unzipWith,URLJoin,UUIDGeneratorBrowser,UUIDGeneratorNode,validateNumber,when,without,words,xProd,yesNo,zip,zipObject,zipWith,binarySearch,celsiusToFahrenheit,cleanObj,collatz,countVowels,factors,fahrenheitToCelsius,fibonacciCountUntilNum,fibonacciUntilNum,heronArea,howManyTimes,httpDelete,httpPut,isArmstrongNumber,isSimilar,JSONToDate,kmphToMph,levenshteinDistance,mphToKmph,pipeLog,quickSort,removeVowels,solveRPN,speechSynthesis,squareSum}
|
||||||
|
|
||||||
|
|||||||
@ -5,18 +5,30 @@ test('chainAsync is a Function', () => {
|
|||||||
expect(chainAsync).toBeInstanceOf(Function);
|
expect(chainAsync).toBeInstanceOf(Function);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let incrementer = 0;
|
||||||
test('Calls all functions in an array', () => {
|
test('Calls all functions in an array', () => {
|
||||||
|
chainAsync([
|
||||||
|
next => {
|
||||||
|
incrementer += 1;
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
next => {
|
||||||
|
incrementer += 1;
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
next => {
|
||||||
|
expect(incrementer).toEqual(2);
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Last function does not receive "next" argument', () => {
|
||||||
chainAsync([
|
chainAsync([
|
||||||
next => {
|
next => {
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
next => {
|
next => {
|
||||||
(() => {
|
expect(next).toBe(undefined);
|
||||||
next();
|
|
||||||
})();
|
|
||||||
},
|
|
||||||
next => {
|
|
||||||
expect(true).toBeTruthy();
|
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -208,7 +208,11 @@
|
|||||||
"body": [
|
"body": [
|
||||||
"const chainAsync = fns => {",
|
"const chainAsync = fns => {",
|
||||||
" let curr = 0;",
|
" let curr = 0;",
|
||||||
" const next = () => fns[curr++](next);",
|
" const last = fns[fns.length - 1];",
|
||||||
|
" const next = () => {",
|
||||||
|
" const fn = fns[curr++];",
|
||||||
|
" fn === last ? fn() : fn(next);",
|
||||||
|
" };",
|
||||||
" next();",
|
" next();",
|
||||||
"};"
|
"};"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user