From 417c5e2d66003fc9dac64de7369c3c5ec0a40a37 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sun, 31 Dec 2017 18:23:01 +0530 Subject: [PATCH 1/6] add isArrayLike and isValidJSON --- snippets/isArrayLike.md | 21 +++++++++++++++++++++ snippets/isValidJSON.md | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 snippets/isArrayLike.md create mode 100644 snippets/isValidJSON.md diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md new file mode 100644 index 000000000..2a5c9ad09 --- /dev/null +++ b/snippets/isArrayLike.md @@ -0,0 +1,21 @@ +### isArrayLike + +Checks if the provided argument is `arrayLike` i.e. is iterable. + +```js +const arr = (arr) => { + try{ + Array.from(arr); + return true; + } + catch(e){ + return false; + } +} +``` + +```js +isArrayLike(document.querySelector('.className')) // true +isArrayLike('abc') // true +isArrayLike(null) // false +``` diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md new file mode 100644 index 000000000..af9781a23 --- /dev/null +++ b/snippets/isValidJSON.md @@ -0,0 +1,21 @@ +### isValidJSON + +Checks if the provided argument is an valid JSON. + + + +```js +const arr = (obj) => { + try{ + JSON.parse(obj); + return true; + } + catch(e){ + return false; + } +} +``` + +```js + +``` From 27e74ce5cc1284b52b024ad2715abaf5e68888e2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:00:55 +0200 Subject: [PATCH 2/6] Update isArrayLike.md --- snippets/isArrayLike.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 2a5c9ad09..4230bd58d 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -1,6 +1,8 @@ ### isArrayLike -Checks if the provided argument is `arrayLike` i.e. is iterable. +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 arr = (arr) => { From 8e28d90220114c2a0f52f678bc40c383bfdefd08 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:01:34 +0200 Subject: [PATCH 3/6] Update isValidJSON.md --- snippets/isValidJSON.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index af9781a23..e81748be9 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -1,8 +1,8 @@ ### isValidJSON -Checks if the provided argument is an valid JSON. - +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 arr = (obj) => { From 8214ee3bece31a64457bd432f6460920d925be69 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:02:05 +0200 Subject: [PATCH 4/6] Update isArrayLike.md --- snippets/isArrayLike.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 4230bd58d..83c8acb81 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -5,7 +5,7 @@ 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 arr = (arr) => { +const isArrayLike = arr => { try{ Array.from(arr); return true; From 9bdd11dfd1cf0de0a7d3f0fa52b6f74b938f8eb6 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:02:19 +0200 Subject: [PATCH 5/6] Update isValidJSON.md --- snippets/isValidJSON.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index e81748be9..2a7c91971 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -5,7 +5,7 @@ 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 arr = (obj) => { +const isValidJSON = obj => { try{ JSON.parse(obj); return true; From bf464d2759f9dd5b7358dde9b54ec2836ce68c9d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:03:54 +0200 Subject: [PATCH 6/6] Update isValidJSON.md --- snippets/isValidJSON.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index 2a7c91971..56211961b 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -17,5 +17,6 @@ const isValidJSON = obj => { ``` ```js - +isValidJSON('{"name":"Adam","age":20}'); // true +isValidJSON('{"name":"Adam",age:"20"}'); // false ```