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 ] @@ -1166,6 +1166,8 @@ Foo.prototypeShow examplesgetType(new Set([1, 2, 3])); // 'set'isArray
Checks if the given argument is an array.
Use
Array.isArray()to check if a value is classified as an array.const isArray = val => Array.isArray(val);isArray([1]); // true +isArrayBuffer
Checks if value is classified as a ArrayBuffer object.
Use the
instanceofoperator to check if the provided value is aArrayBufferobject.const isArrayBuffer = val => val instanceof ArrayBuffer; +isArrayBuffer(new ArrayBuffer()); // trueisArrayLike
Checks if the provided argument is array-like (i.e. is iterable).
Use the spread operator (
...) to check if the provided argument is iterable inside atry... catchblock and the comma operator (,) to return the appropriate value.const isArrayLike = val => { try { return [...val], true; @@ -1182,6 +1184,8 @@ Foo.prototype📋 Copy to clipboardisFunction
Checks if the given argument is a function.
Use
typeofto check if a value is classified as a function primitive.const isFunction = val => typeof val === 'function';isFunction('x'); // false isFunction(x => x); // true +isMap
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()); // 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'; @@ -1212,10 +1216,16 @@ Foo.prototype// true isPromiseLike(null); // false isPromiseLike({}); // false +isRegExp
Checks if value is classified as a RegExp object.
Use the
instanceofoperator to check if the provided value is aRegExpobject.const isRegExp = val => val instanceof RegExp; +isRegExp(/./g); // true +isSet
Checks if value is classified as a Set object.
Use the
instanceofoperator to check if the provided value is aSetobject.const isSet = val => val instanceof Set; +isSet(new Set()); // trueisString
Checks if the given argument is a string.
Use
typeofto check if a value is classified as a string primitive.const isString = val => typeof val === 'string';isString('10'); // trueisSymbol
Checks if the given argument is a symbol.
Use
typeofto check if a value is classified as a symbol primitive.const isSymbol = val => typeof val === 'symbol';isSymbol(Symbol('x')); // true +isTypedArray
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()); // 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); @@ -1227,6 +1237,10 @@ Foo.prototypeShow examplesisValidJSON('{"name":"Adam","age":20}'); // true isValidJSON('{"name":"Adam",age:"20"}'); // false isValidJSON(null); // true +isWeakMap
Checks if value is classified as a WeakMap object.
Use the
instanceofoperator to check if the provided value is aWeakMapobject.const isWeakMap = val => val instanceof WeakMap; +isWeakMap(new WeakMap()); // true +isWeakSet
Checks if value is classified as a WeakSet object.
Use the
instanceofoperator to check if the provided value is aWeakSetobject.const isWeakSet = val => val instanceof WeakSet; +isWeakSet(new WeakSet()); // trueUtility
cloneRegExp
Clones a regular expression.
Use
new RegExp(),RegExp.sourceandRegExp.flagsto clone the given regular expression.const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);const regExp = /lorem ipsum/gi; const regExp2 = cloneRegExp(regExp); // /lorem ipsum/gi