From 1263719160a14f81820ddc64d3987e3d4f489259 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 65768653dd82e950d8bf2cda18d00f44d595768f 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 21d6886f8dec20dbe75174153cb1f0043f8f240f 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 edb4f46008a2706df33680dc434f75a1f41cf003 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 e6c74f263c32dfc7f1bebb1228b07b481ec31d9d 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 43e7c0a769894d9f580c191f77e53fa49946027a 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 ```