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 ]
@@ -849,6 +849,17 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'
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 => !!val && Array.isArray(val);
isArray(null); // false
isArray([1]); // true
+
isArrayLike
Checks if the provided argument is array-like (i.e. is iterable).
Use Array.from() and a try... catch block to check if the provided argument is array-like.
const isArrayLike = arr => {
+ try {
+ Array.from(arr);
+ return true;
+ } catch (e) {
+ return false;
+ }
+};
+
isArrayLike(document.querySelector('.className')); // true
+isArrayLike('abc'); // true
+isArrayLike(null); // false
isBoolean
Checks if the given argument is a native boolean element.
Use typeof to check if a value is classified as a boolean primitive.
const isBoolean = val => typeof val === 'boolean';
isBoolean(null); // false
isBoolean(false); // true
@@ -867,6 +878,16 @@ isString('10'); // true
isSymbol
Checks if the given argument is a symbol.
Use typeof to check if a value is classified as a symbol primitive.
const isSymbol = val => typeof val === 'symbol';
isSymbol('x'); // false
isSymbol(Symbol('x')); // true
+
isValidJSON
Checks if the provided argument is a valid JSON.
Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON.
const isValidJSON = obj => {
+ try {
+ JSON.parse(obj);
+ return true;
+ } catch (e) {
+ return false;
+ }
+};
+
isValidJSON('{"name":"Adam","age":20}'); // true
+isValidJSON('{"name":"Adam",age:"20"}'); // false
randomHexColorCode
Generates a random hexadecimal color code.
Use Math.random to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using toString(16).
const randomHexColorCode = () => {
let n = ((Math.random() * 0xfffff) | 0).toString(16);
return '#' + (n.length !== 6 ? ((Math.random() * 0xf) | 0).toString(16) + n : n);
@@ -915,25 +936,4 @@ console.log(sdbm('age')); // 808122783
yesNo('yes'); // true
yesNo('No'); // false
yesNo('Foo', true); // true
-
Uncategorized
isArrayLike
Checks if the provided argument is array-like (i.e. is iterable).
Use Array.from() and a try... catch block to check if the provided argument is array-like.
const isArrayLike = arr => {
- try {
- Array.from(arr);
- return true;
- } catch (e) {
- return false;
- }
-};
-
isArrayLike(document.querySelector('.className')); // true
-isArrayLike('abc'); // true
-isArrayLike(null); // false
-
isValidJSON
Checks if the provided argument is a valid JSON.
Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON.
const isValidJSON = obj => {
- try {
- JSON.parse(obj);
- return true;
- } catch (e) {
- return false;
- }
-};
-
isValidJSON('{"name":"Adam","age":20}'); // true
-isValidJSON('{"name":"Adam",age:"20"}'); // false
\ No newline at end of file