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)); + }⚠️ WARNING: Snippets are not production ready.
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); @@ -1990,6 +1990,9 @@ Logs: { "id": 101 } */ +isBrowser
Determines if the current runtime environment is a browser so that front-end modules can run on the server (Node) without throwing errors.
Use
Array.includes()on thetypeofvalues of bothwindowanddocument(globals usually only available in a browser environment unless they were explicitly defined), which will returntrueif one of them isundefined.typeofallows globals to be checked for existence without throwing a ReferenceError. If both of them are notundefined, then the current environment is assumed to be a browser.const isBrowser = () => ![typeof window, typeof document].includes('undefined'); +isBrowser(); // true (browser) +isBrowser(); // false (Node)mostPerformant
Returns the index of the function in an array of functions which executed the fastest.
Use
Array.map()to generate an array where each value is the total time taken to execute the function afteriterationstimes. Use the difference inperformance.now()values before and after to get the total time in milliseconds to a high degree of accuracy. UseMath.min()to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function. Omit the second argument,iterations, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take.const mostPerformant = (fns, iterations = 10000) => { const times = fns.map(fn => { const before = performance.now(); diff --git a/snippets/isBrowser.md b/snippets/isBrowser.md index 0b41b4bba..4227234d7 100644 --- a/snippets/isBrowser.md +++ b/snippets/isBrowser.md @@ -8,10 +8,10 @@ browser environment unless they were explicitly defined), which will return `tru `typeof` allows globals to be checked for existence without throwing a ReferenceError. If both of them are not `undefined`, then the current environment is assumed to be a browser. ```js -const isBrowser = () => ![typeof window, typeof document].includes('undefined') +const isBrowser = () => ![typeof window, typeof document].includes('undefined'); ``` ```js -isBrowser() // true (browser) -isBrowser() // false (Node) +isBrowser(); // true (browser) +isBrowser(); // false (Node) ```