Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD : isArrayLike.md and isValidJSON.md] #438

Merged
merged 7 commits into from Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions snippets/isArrayLike.md
@@ -0,0 +1,23 @@
### 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
```
22 changes: 22 additions & 0 deletions snippets/isValidJSON.md
@@ -0,0 +1,22 @@
### 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
```