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 ] @@ -161,11 +161,18 @@ Object.assiginitialize2DArray
Initializes a 2D array of given width and height and value.
Use
Array.map()to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default tonull.const initialize2DArray = (w, h, val = null) => Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val));initialize2DArray(2, 2, 0); // [[0,0], [0,0]] -initializeArrayWithRange
Initializes an array containing the numbers in the specified range where
startandendare inclusive with there common differencestep.Use
Array.from(Math.ceil((end+1-start)/step))to create an array of the desired length(the amounts of elements is equal to(end-start)/stepor(end+1-start)/stepfor inclusive end),Array.map()to fill with the desired values in a range. You can omitstartto use a default value of0. You can omitstepto use a default value of1.const initializeArrayWithRange = (end, start = 0, step = 1) => +initializeArrayWithRange
Initializes an array containing the numbers in the specified range where
startandendare inclusive with their common differencestep.Use
Array.from(Math.ceil((end+1-start)/step))to create an array of the desired length(the amounts of elements is equal to(end-start)/stepor(end+1-start)/stepfor inclusive end),Array.map()to fill with the desired values in a range. You can omitstartto use a default value of0. You can omitstepto use a default value of1.const initializeArrayWithRange = (end, start = 0, step = 1) => Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i) => i * step + start);initializeArrayWithRange(5); // [0,1,2,3,4,5] initializeArrayWithRange(7, 3); // [3,4,5,6,7] initializeArrayWithRange(9, 0, 2); // [0,2,4,6,8] +initializeArrayWithRangeRight
Initializes an array containing the numbers in the specified range (in reverse) where
startandendare inclusive with their common differencestep.Use
Array.from(Math.ceil((end+1-start)/step))to create an array of the desired length(the amounts of elements is equal to(end-start)/stepor(end+1-start)/stepfor inclusive end),Array.map()to fill with the desired values in a range. You can omitstartto use a default value of0. You can omitstepto use a default value of1.const initializeArrayWithRangeRight = (end, start = 0, step = 1) => + Array.from({ length: Math.ceil((end + 1 - start) / step) }).map( + (v, i, arr) => (arr.length - i - 1) * step + start + ); +initializeArrayWithRangeRight(5); // [5,4,3,2,1,0] +initializeArrayWithRangeRight(7, 3); // [7,6,5,4,3] +initializeArrayWithRangeRight(9, 0, 2); // [8,6,4,2,0]initializeArrayWithValues
Initializes and fills an array with the specified values.
Use
Array(n)to create an array of the desired length,fill(v)to fill it with the desired values. You can omitvalto use a default value of0.const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);initializeArrayWithValues(5, 2); // [2,2,2,2,2]intersection
Returns a list of elements that exist in both arrays.
Create a
Setfromb, then useArray.filter()onato only keep values contained inb.const intersection = (a, b) => { @@ -1186,6 +1193,9 @@ Foo.prototypeisFunction(x => x); // trueisMap
Checks if value is classified as a Map object.
Use the
instanceofoperator to check if the provided value is aMapobject.const isMap = val => val instanceof Map;isMap(new Map()); // true +isNil
Returns
trueif the specified value isnullorundefined,falseotherwise.Use the strict equality operator to check if the value and of
valare equal tonullorundefined.const isNil = val => val === undefined || val === null; +isNil(null); // true +isNil(undefined); // trueisNull
Returns
trueif the specified value isnull,falseotherwise.Use the strict equality operator to check if the value and of
valare equal tonull.const isNull = val => val === null;isNull(null); // trueisNumber
Checks if the given argument is a number.
Use
typeofto check if a value is classified as a number primitive.const isNumber = val => typeof val === 'number'; @@ -1226,6 +1236,8 @@ Foo.prototypeShow examplesisSymbol(Symbol('x')); // trueisTypedArray
Checks if value is classified as a TypedArray object.
Use the
instanceofoperator to check if the provided value is aTypedArrayobject.const isTypedArray = val => val instanceof TypedArray;isTypedArray(new TypedArray()); // true +isUndefined
Returns
trueif the specified value isundefined,falseotherwise.Use the strict equality operator to check if the value and of
valare equal toundefined.const isUndefined = val => val === undefined; +isUndefined(undefined); // trueisValidJSON
Checks if the provided argument is a valid JSON.
Use
JSON.parse()and atry... catchblock to check if the provided argument is a valid JSON.const isValidJSON = obj => { try { JSON.parse(obj); diff --git a/snippets/initializeArrayWithRangeRight.md b/snippets/initializeArrayWithRangeRight.md index 0b73c0197..608352c4d 100644 --- a/snippets/initializeArrayWithRangeRight.md +++ b/snippets/initializeArrayWithRangeRight.md @@ -8,7 +8,9 @@ You can omit `step` to use a default value of `1`. ```js const initializeArrayWithRangeRight = (end, start = 0, step = 1) => - Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i, arr) => (arr.length - i - 1) * step + start); + Array.from({ length: Math.ceil((end + 1 - start) / step) }).map( + (v, i, arr) => (arr.length - i - 1) * step + start + ); ``` ```js