diff --git a/README.md b/README.md index 036edc432..f2b0a62bc 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,16 @@ +### _Uncategorized_ + +
+View contents + +* [`isArrayLike`](#isarraylike) +* [`isValidJSON`](#isvalidjson) + +
+ --- ## 🔌 Adapter @@ -4394,6 +4404,59 @@ 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 5c06dd1eb..ebbc9bc33 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 ]
@@ -915,4 +915,25 @@ 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 diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 83c8acb81..36eae12a9 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -6,18 +6,17 @@ Use `Array.from()` and a `try... catch` block to check if the provided argument ```js const isArrayLike = arr => { - try{ + try { Array.from(arr); return true; - } - catch(e){ + } catch (e) { return false; } -} +}; ``` ```js -isArrayLike(document.querySelector('.className')) // true -isArrayLike('abc') // true -isArrayLike(null) // false +isArrayLike(document.querySelector('.className')); // true +isArrayLike('abc'); // true +isArrayLike(null); // false ``` diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index 56211961b..e9898caee 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -6,14 +6,13 @@ Use `JSON.parse()` and a `try... catch` block to check if the provided argument ```js const isValidJSON = obj => { - try{ + try { JSON.parse(obj); return true; - } - catch(e){ + } catch (e) { return false; } -} +}; ``` ```js diff --git a/tag_database b/tag_database index e2bb75c7e..c02508fff 100644 --- a/tag_database +++ b/tag_database @@ -66,6 +66,7 @@ inRange:math intersection:array isArmstrongNumber:math isArray:utility +isArrayLike:uncategorized isBoolean:utility isDivisible:math isEven:math @@ -75,6 +76,7 @@ isNumber:utility isPrime:math isString:utility isSymbol:utility +isValidJSON:uncategorized JSONToDate:date JSONToFile:node last:array