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); @@ -747,6 +747,15 @@ document.bodyShow examplesonUserInputChange(type => { console.log('The user is now using', type, 'as an input method.'); }); +prefix
Returns the prefixed version (if necessary) of a CSS property that the browser supports.
Use
Array.findIndex()on an array of vendor prefix strings to test ifdocument.bodyhas one of them defined in itsCSSStyleDeclarationobject, otherwise returnnull. UseString.charAt()andString.toUpperCase()to capitalize the property, which will be appended to the vendor prefix string.const prefix = prop => { + const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1); + const prefixes = ['', 'webkit', 'moz', 'ms', 'o']; + const i = prefixes.findIndex( + prefix => typeof document.body.style[prefix ? prefix + capitalizedProp : prop] !== 'undefined' + ); + return i !== -1 ? (i === 0 ? prop : prefixes[i] + capitalizedProp) : null; +}; +prefix('appearance'); // 'appearance' on a supported browser, otherwise 'webkitAppearance', 'mozAppearance', 'msAppearance' or 'oAppearance'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; diff --git a/snippets/prefix.md b/snippets/prefix.md index b5704f7b5..65d009c35 100644 --- a/snippets/prefix.md +++ b/snippets/prefix.md @@ -9,9 +9,11 @@ Use `String.charAt()` and `String.toUpperCase()` to capitalize the property, whi const prefix = prop => { const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1); const prefixes = ['', 'webkit', 'moz', 'ms', 'o']; - const i = prefixes.findIndex(prefix => typeof document.body.style[(prefix ? prefix + capitalizedProp : prop)] !== 'undefined'); - return i !== -1 ? i === 0 ? prop : prefixes[i] + capitalizedProp : null; -} + const i = prefixes.findIndex( + prefix => typeof document.body.style[prefix ? prefix + capitalizedProp : prop] !== 'undefined' + ); + return i !== -1 ? (i === 0 ? prop : prefixes[i] + capitalizedProp) : null; +}; ``` ```js