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 rearg spreadOver unary Array bifurcate bifurcateBy 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 getColonTimeFromDate getDaysDiffBetweenDates getMeridiemSuffixOfInteger tomorrow Function attempt bind bindKey chainAsync compose composeRight converge curry debounce defer delay functionName memoize negate once partial partialRight runPromisesInSeries sleep throttle times uncurry unfold Math average averageBy clampNumber degreesToRads digitize distance elo factorial fibonacci gcd geometricProgression hammingDistance inRange isDivisible isEven isPrime lcm luhnCheck maxBy median minBy percentile powerset primes radsToDegrees 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 flattenObject forOwn forOwnRight functions get invertKeyValues lowercaseKeys mapKeys mapValues matches matchesWith merge objectFromPairs objectToPairs omit omitBy orderBy pick pickBy shallowClone size transform truthCheckCollection unflattenObject 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 toCurrency 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);
@@ -1000,6 +1000,8 @@ console. log<
📋 Copy to clipboard clampNumber Clamps num within the inclusive range specified by the boundary values a and b.
If num falls within the range, return num. Otherwise, return the nearest number in the range.
const clampNumber = ( num, a, b) => Math. max ( Math. min ( num, Math. max ( a, b)), Math. min ( a, b));
Show examples clampNumber ( 2 , 3 , 5 );
clampNumber ( 1 , - 1 , - 5 );
+📋 Copy to clipboard degreesToRads Converts an angle from degrees to radians.
Use Math.PI and the degree to radian formula to convert the angle from degrees to radians.
const degreesToRads = deg => deg * Math. PI / 180.0 ;
+Show examples degreesToRads ( 90.0 );
📋 Copy to clipboard digitize Converts a number to an array of digits.
Convert the number to a string, using the spread operator (...) to build an array. Use Array.map() and parseInt() to transform each value to an integer.
const digitize = n => [ ... ` ${ n} ` ]. map ( i => parseInt ( i));
Show examples digitize ( 123 );
📋 Copy to clipboard distance Returns the distance between two points.
Use Math.hypot() to calculate the Euclidean distance between two points.
const distance = ( x0, y0, x1, y1) => Math. hypot ( x1 - x0, y1 - y0);
@@ -1123,6 +1125,8 @@ own individual rating by supplying it as the third argument.
return arr;
};
Show examples primes ( 10 );
+📋 Copy to clipboard radsToDegrees Converts an angle from radians to degrees.
Use Math.PI and the radian to degree formula to convert the angle from radians to degrees.
const radsToDegrees = rad => rad * 180.0 / Math. PI;
+Show examples radsToDegrees ( Math. PI / 2 );
📋 Copy to clipboard randomIntArrayInRange Returns an array of n random integers in the specified range.
Use Array.from() to create an empty array of the specific length, Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.
const randomIntArrayInRange = ( min, max, n = 1 ) =>
Array. from ({ length: n }, () => Math. floor ( Math. random () * ( max - min + 1 )) + min);
Show examples randomIntArrayInRange ( 12 , 35 , 10 );