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); @@ -750,6 +750,31 @@ document.bodyShow examplesonUserInputChange(type => { console.log('The user is now using', type, 'as an input method.'); }); +recordAnimationFrames
Invokes the provided callback on each animation frame.
Use recursion. Provided that
runningistrue, continue invokingwindow.requestAnimationFrame()which invokes the provided callback. Return an object with two methodsstartandstopto allow manual control of the recording. Omit the second argument,autoStart, to implicitly callstartwhen the function is invoked.const recordAnimationFrames = (callback, autoStart = true) => { + let running = true, + raf; + const stop = () => { + running = false; + cancelAnimationFrame(raf); + }; + const start = () => { + running = true; + run(); + }; + const run = () => { + raf = requestAnimationFrame(() => { + callback(); + if (running) run(); + }); + }; + if (autoStart) start(); + return { start, stop }; +}; +const cb = () => console.log('Animation frame fired'); +const recorder = recordAnimationFrames(cb); // logs 'Animation frame fired' on each animation frame +recorder.stop(); // stops logging +recorder.start(); // starts again +const recorder2 = recordAnimationFrames(cb, false); // `start` needs to be explicitly called to begin recording framesredirect
Redirects to a specified URL.
Use
window.location.hreforwindow.location.replace()to redirect tourl. Pass a second argument to simulate a link click (true- default) or an HTTP redirect (false).const redirect = (url, asLink = true) => asLink ? (window.location.href = url) : window.location.replace(url);redirect('https://google.com'); diff --git a/snippets/recordAnimationFrames.md b/snippets/recordAnimationFrames.md index 100504eb9..a775e0460 100644 --- a/snippets/recordAnimationFrames.md +++ b/snippets/recordAnimationFrames.md @@ -9,24 +9,25 @@ Omit the second argument, `autoStart`, to implicitly call `start` when the funct ```js const recordAnimationFrames = (callback, autoStart = true) => { - let running = true, raf + let running = true, + raf; const stop = () => { - running = false - cancelAnimationFrame(raf) - } + running = false; + cancelAnimationFrame(raf); + }; const start = () => { - running = true - run() - } + running = true; + run(); + }; const run = () => { raf = requestAnimationFrame(() => { - callback() - if (running) run() - }) - } - if (autoStart) start() - return { start, stop } -} + callback(); + if (running) run(); + }); + }; + if (autoStart) start(); + return { start, stop }; +}; ``` ```js