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 ] @@ -1205,10 +1205,20 @@ Foo.prototype📋 Copy to clipboardType
getType
Returns the native type of a value.
Returns lowercased constructor name of value,
"undefined"or"null"if value isundefinedornull.const getType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();getType(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()); // true +is
Checks if the provided value is of the specified type (doesn't work with literals).
Use the
instanceofoperator to check if the provided value is of the specifiedtype.const is = (type, val) => val instanceof type; +is(Array, [1]); // true +is(ArrayBuffer, new ArrayBuffer()); // true +is(Map, new Map()); // true +is(RegExp, /./g); // true +is(Set, new Set()); // true +is(WeakMap, new WeakMap()); // true +is(WeakSet, new WeakSet()); // true +is(String, ''); // false +is(String, new String('')); // true +is(Number, 1); // false +is(Number, new Number(1)); // true +is(Boolean, true); // false +is(Boolean, new Boolean(true)); // 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; @@ -1225,8 +1235,6 @@ 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()); // trueisNil
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); // true @@ -1260,16 +1268,10 @@ 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()); // trueisUndefined
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 => { @@ -1283,10 +1285,6 @@ 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 diff --git a/snippets/is.md b/snippets/is.md index de07e6070..bd7bb70b5 100644 --- a/snippets/is.md +++ b/snippets/is.md @@ -9,7 +9,7 @@ const is = (type, val) => val instanceof type; ``` ```js -is(Array,[1]); // true +is(Array, [1]); // true is(ArrayBuffer, new ArrayBuffer()); // true is(Map, new Map()); // true is(RegExp, /./g); // true