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. Search for snippet...
Adapter ary call collectInto flip over overArgs pipeAsyncFunctions pipeFunctions promisify spreadOver unary Array chunk compact countBy countOccurrences deepFlatten difference differenceBy differenceWith drop dropRight dropRightWhile dropWhile everyNth filterNonUnique findLast findLastIndex flatten forEachRight groupBy head indexOfAll initial initialize2DArray initializeArrayWithRange initializeArrayWithRangeRight initializeArrayWithValues intersection intersectionBy intersectionWith isSorted join last longestItem mapObject maxN minN nthElement partition pull pullAtIndex pullAtValue pullBy reducedFilter reduceSuccessive reduceWhich remove sample sampleSize shuffle similarity sortedIndex sortedIndexBy sortedLastIndex sortedLastIndexBy symmetricDifference symmetricDifferenceBy symmetricDifferenceWith tail take takeRight takeRightWhile takeWhile union unionBy unionWith uniqueElements unzip unzipWith without xProd zip zipObject zipWith Browser arrayToHtmlList bottomVisible copyToClipboard createElement createEventHub currentURL detectDeviceType elementIsVisibleInViewport getScrollPosition getStyle hasClass hashBrowser hide httpsRedirect observeMutations off on onUserInputChange redirect runAsync scrollToTop setStyle show toggleClass UUIDGeneratorBrowser Date formatDuration getDaysDiffBetweenDates tomorrow Function attempt bind bindKey chainAsync compose composeRight curry defer delay functionName memoize negate once partial partialRight runPromisesInSeries sleep times unfold Math average averageBy clampNumber digitize distance elo factorial fibonacci gcd geometricProgression hammingDistance inRange isDivisible isEven isPrime lcm luhnCheck maxBy median minBy percentile powerset primes randomIntArrayInRange randomIntegerInRange randomNumberInRange round sdbm standardDeviation sum sumBy sumPower toSafeInteger Node atob btoa colorize hasFlags hashNode isTravisCI JSONToFile readFileLines untildify UUIDGeneratorNode Object bindAll deepClone defaults equals findKey findLastKey forOwn forOwnRight functions get invertKeyValues lowercaseKeys mapKeys mapValues matches matchesWith merge objectFromPairs objectToPairs omit omitBy orderBy pick pickBy shallowClone size transform truthCheckCollection String anagrams byteSize capitalize capitalizeEveryWord decapitalize escapeHTML escapeRegExp fromCamelCase isAbsoluteURL isLowerCase isUpperCase mask palindrome pluralize removeNonASCII reverseString sortCharactersInString splitLines stripHTMLTags toCamelCase toKebabCase toSnakeCase truncateString unescapeHTML URLJoin words Type getType is isArrayLike isBoolean isEmpty isFunction isNil isNull isNumber isObject isObjectLike isPlainObject isPrimitive isPromiseLike isString isSymbol isUndefined isValidJSON Utility castArray cloneRegExp coalesce coalesceFactory extendHex getURLParameters hexToRGB httpGet httpPost nthArg parseCookie prettyBytes randomHexColorCode RGBToHex serializeCookie timeTaken toDecimalMark toOrdinalSuffix validateNumber yesNo 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));
Show examples const firstTwoMax = ary ( Math. max, 2 );
[[ 2 , 6 , 'a' ], [ 8 , 4 , 6 ], [ 10 ]]. map ( x => firstTwoMax ( ... x));
📋 Copy to clipboard 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);
@@ -78,6 +78,14 @@ Object. assig
📋 Copy to clipboard over Creates a function that invokes each provided function with the arguments it receives and returns the results.
Use Array.map() and Function.apply() to apply each function to the given arguments.
const over = ( ... fns) => ( ... args) => fns. map ( fn => fn. apply ( null , args));
Show examples const minMax = over ( Math. min, Math. max);
minMax ( 1 , 2 , 3 , 4 , 5 );
+📋 Copy to clipboard overArgs Creates a function that invokes the provided function with its arguments transformed.
Use Array.map() to apply transforms to args in combination with the spread operator (...) to pass the transformed arguments to fn.
const overArgs = ( fn, transforms) => ( ... args) => fn ( ... args. map (( val, i) => transforms[ i]( val)));
+Show examples var func = overArgs (
+ function ( x, y) {
+ return [ x, y];
+ },
+ [ square, doubled]
+);
+func ( 9 , 3 );
📋 Copy to clipboard pipeAsyncFunctions Performs left-to-right function composition for asynchronous functions.
Use Array.reduce() with the spread operator (...) to perform left-to-right function composition using Promise.then(). The functions can return a combination of: simple values, Promise's, or they can be defined as async ones returning through await. All functions must be unary.
const pipeAsyncFunctions = ( ... fns) => arg => fns. reduce (( p, f) => p. then ( f), Promise. resolve ( arg));
Show examples const sum = pipeAsyncFunctions (
x => x + 1 ,
diff --git a/snippets/overArgs.md b/snippets/overArgs.md
index 0c54fa726..9a913e049 100644
--- a/snippets/overArgs.md
+++ b/snippets/overArgs.md
@@ -5,8 +5,7 @@ Creates a function that invokes the provided function with its arguments transfo
Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
-const overArgs = (fn, transforms) => (...args) =>
- fn(...args.map((val, i) => transforms[i](val)));
+const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
```
```js