diff --git a/README.md b/README.md index f2b0a62bc..cb94d656d 100644 --- a/README.md +++ b/README.md @@ -244,12 +244,14 @@ * [`getType`](#gettype) * [`hexToRGB`](#hextorgb) * [`isArray`](#isarray) +* [`isArrayLike`](#isarraylike) * [`isBoolean`](#isboolean) * [`isFunction`](#isfunction) * [`isNull`](#isnull) * [`isNumber`](#isnumber) * [`isString`](#isstring) * [`isSymbol`](#issymbol) +* [`isValidJSON`](#isvalidjson) * [`randomHexColorCode`](#randomhexcolorcode) * [`RGBToHex`](#rgbtohex) * [`sdbm`](#sdbm) @@ -261,16 +263,6 @@ -### _Uncategorized_ - -
-View contents - -* [`isArrayLike`](#isarraylike) -* [`isValidJSON`](#isvalidjson) - -
- --- ## 🔌 Adapter @@ -4057,6 +4049,37 @@ isArray([1]); // true
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const isArrayLike = arr => { + try { + Array.from(arr); + return true; + } catch (e) { + return false; + } +}; +``` + +
+Examples + +```js +isArrayLike(document.querySelector('.className')); // true +isArrayLike('abc'); // true +isArrayLike(null); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isBoolean Checks if the given argument is a native boolean element. @@ -4194,6 +4217,36 @@ isSymbol(Symbol('x')); // true
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const isValidJSON = obj => { + try { + JSON.parse(obj); + return true; + } catch (e) { + return false; + } +}; +``` + +
+Examples + +```js +isValidJSON('{"name":"Adam","age":20}'); // true +isValidJSON('{"name":"Adam",age:"20"}'); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### randomHexColorCode Generates a random hexadecimal color code. @@ -4404,59 +4457,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _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. - -```js -const isArrayLike = arr => { - try { - Array.from(arr); - return true; - } catch (e) { - return false; - } -}; -``` - -```js -isArrayLike(document.querySelector('.className')); // true -isArrayLike('abc'); // true -isArrayLike(null); // false -``` - -
[⬆ back to top](#table-of-contents) - - -### 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. - -```js -const isValidJSON = obj => { - try { - JSON.parse(obj); - return true; - } catch (e) { - return false; - } -}; -``` - -```js -isValidJSON('{"name":"Adam","age":20}'); // true -isValidJSON('{"name":"Adam",age:"20"}'); // false -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index ebbc9bc33..a46afcda6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 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);
+    }

 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