diff --git a/README.md b/README.md index 477bd5b8d..d980e1337 100644 --- a/README.md +++ b/README.md @@ -4246,13 +4246,13 @@ isSymbol(Symbol('x')); // true 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 and non-null. +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 !!obj; + return true; } catch (e) { return false; } @@ -4265,6 +4265,7 @@ const isValidJSON = obj => { ```js isValidJSON('{"name":"Adam","age":20}'); // true isValidJSON('{"name":"Adam",age:"20"}'); // false +isValidJSON(null); // true ``` diff --git a/docs/index.html b/docs/index.html index fcffcb709..9571d93b4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -882,16 +882,17 @@ 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 and non-null.

const isValidJSON = obj => {
+

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 !!obj;
+    return true;
   } catch (e) {
     return false;
   }
 };
 
isValidJSON('{"name":"Adam","age":20}'); // true
 isValidJSON('{"name":"Adam",age:"20"}'); // false
+isValidJSON(null); // true
 

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);
diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md
index 5b671e67b..51b9270a2 100644
--- a/snippets/isValidJSON.md
+++ b/snippets/isValidJSON.md
@@ -18,5 +18,5 @@ const isValidJSON = obj => {
 ```js
 isValidJSON('{"name":"Adam","age":20}'); // true
 isValidJSON('{"name":"Adam",age:"20"}'); // false
-isValidJSON(null) // true
+isValidJSON(null); // true
 ```