Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.slice(0,n)and the spread operator (...).const ary = (fn, n) => (...args) => fn(...args.slice(0, n));const firstTwoMax = ary(Math.max, 2); [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); diff --git a/docs/array.html b/docs/array.html index 4275f4276..042200070 100644 --- a/docs/array.html +++ b/docs/array.html @@ -79,7 +79,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Array
all
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.every()to test if all elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const all = (arr, fn = Boolean) => arr.every(fn); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Array
all
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.every()to test if all elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const all = (arr, fn = Boolean) => arr.every(fn);all([4, 2, 3], x => x > 1); // true all([1, 2, 3]); // trueany
Returns
trueif the provided predicate function returnstruefor at least one element in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const any = (arr, fn = Boolean) => arr.some(fn); diff --git a/docs/browser.html b/docs/browser.html index f31071fa8..218af59d5 100644 --- a/docs/browser.html +++ b/docs/browser.html @@ -79,7 +79,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Browser
arrayToHtmlList
Converts the given array elements into
<li>tags and appends them to the list of the given id.Use
Array.map()anddocument.querySelector()to create a list of html tags.const arrayToHtmlList = (arr, listID) => + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Browser
arrayToHtmlList
Converts the given array elements into
<li>tags and appends them to the list of the given id.Use
Array.map()anddocument.querySelector()to create a list of html tags.const arrayToHtmlList = (arr, listID) => arr.map(item => (document.querySelector('#' + listID).innerHTML += `<li>${item}</li>`));arrayToHtmlList(['item 1', 'item 2'], 'myListID');bottomVisible
Returns
trueif the bottom of the page is visible,falseotherwise.Use
scrollY,scrollHeightandclientHeightto determine if the bottom of the page is visible.const bottomVisible = () => diff --git a/docs/date.html b/docs/date.html index 541993c59..055440ea7 100644 --- a/docs/date.html +++ b/docs/date.html @@ -79,7 +79,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Date
formatDuration
Returns the human readable format of the given number of milliseconds.
Divide
mswith the appropriate values to obtain the appropriate values forday,hour,minute,secondandmillisecond. UseObject.entries()withArray.filter()to keep only non-zero values. UseArray.map()to create the string for each value, pluralizing appropriately. UseString.join(', ')to combine the values into a string.const formatDuration = ms => { + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Date
formatDuration
Returns the human readable format of the given number of milliseconds.
Divide
mswith the appropriate values to obtain the appropriate values forday,hour,minute,secondandmillisecond. UseObject.entries()withArray.filter()to keep only non-zero values. UseArray.map()to create the string for each value, pluralizing appropriately. UseString.join(', ')to combine the values into a string.const formatDuration = ms => { if (ms < 0) ms = -ms; const time = { day: Math.floor(ms / 86400000), diff --git a/docs/function.html b/docs/function.html index f4a7c9b0a..a8c9b5f23 100644 --- a/docs/function.html +++ b/docs/function.html @@ -79,7 +79,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Function
attempt
Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.
Use a
try... catchblock to return either the result of the function or an appropriate error.const attempt = (fn, ...args) => { + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Function
attempt
Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.
Use a
try... catchblock to return either the result of the function or an appropriate error.const attempt = (fn, ...args) => { try { return fn(...args); } catch (e) { @@ -293,4 +293,8 @@ console.log< };var f = n => (n > 50 ? false : [-n, n + 10]); unfold(f, 10); // [-10, -20, -30, -40, -50] +when
Tests a value,
x, against a predicate function. Iftrue, returnfn(x). Else, returnx.Return a function expecting a single value,
x, that returns the appropriate value based onpred.const when = (pred, whenTrue) => x => (pred(x) ? whenTrue(x) : x); +const doubleEvenNumbers = when(x => x % 2 === 0, x => x * 2); +doubleEvenNumbers(2); // 4 +doubleEvenNumbers(1); // 1
