Adapter
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); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
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);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -362,6 +362,19 @@ Object.assig if (location.protocol !== 'https:') location.replace('https://' + location.href.split('//')[1]); };httpsRedirect(); // If you are on http://mydomain.com, you are redirected to https://mydomain.com +off
Removes an event listener from an element.
Use
EventTarget.removeEventListener()to remove an event listener from an element. Omit the fourth argumentoptsto usefalseor specify it based on the options used when the event listener was added.const off = (el, evt, fn, opts = false) => el.removeEventListener(evt, fn, opts); +const fn = () => console.log('!'); +document.body.addEventListener('click', fn); +off(document.body, 'click', fn); // no longer logs '!' upon clicking on the page +on
Adds an event listener to an element with the ability to use event delegation.
Use
EventTarget.addEventListener()to add an event listener to an element. If there is atargetproperty supplied to the options object, ensure the event target matches the target specified and then invoke the callback by supplying the correctthiscontext. Returns a reference to the custom delegator function, in order to be possible to use withoff. Omitoptsto default to non-delegation behavior and event bubbling.const on = (el, evt, fn, opts = {}) => { + const delegatorFn = e => e.target.matches(opts.target) && fn.call(e.target, e); + el.addEventListener(evt, opts.target ? delegatorFn : fn, opts.options || false); + if (opts.target) return delegatorFn; +}; +const fn = () => console.log('!'); +on(document.body, 'click', fn); // logs '!' upon clicking the body +on(document.body, 'click', fn, { target: 'p' }); // logs '!' upon clicking a `p` element child of the body +on(document.body, 'click', fn, { options: true }); // use capturing instead of bubblingonUserInputChangeadvanced
Run the callback whenever the user input type changes (
mouseortouch). Useful for enabling/disabling code depending on the input device. This process is dynamic and works with hybrid devices (e.g. touchscreen laptops).Use two event listeners. Assume
mouseinput initially and bind atouchstartevent listener to the document. Ontouchstart, add amousemoveevent listener to listen for two consecutivemousemoveevents firing within 20ms, usingperformance.now(). Run the callback with the input type as an argument in either of these situations.const onUserInputChange = callback => { let type = 'mouse', lastTime = 0; @@ -875,6 +888,7 @@ console.log<reverseString
Reverses a string.
Use the spread operator (
...) andArray.reverse()to reverse the order of the characters in the string. Combine characters to get a string usingString.join('').+ const reverseString = str => [..str] .reverse() diff --git a/snippets/reverseString.md b/snippets/reverseString.md index befdd8bf5..ab48b45e4 100644 --- a/snippets/reverseString.md +++ b/snippets/reverseString.md @@ -9,6 +9,7 @@ Combine characters to get a string using `String.join('')`. + const reverseString = str => [..str] .reverse()