Travis build: 846
This commit is contained in:
@ -8838,14 +8838,14 @@ isUndefined(undefined); // true
|
||||
|
||||
### isValidJSON
|
||||
|
||||
Checks if the provided argument is a valid JSON.
|
||||
Checks if the provided string is a valid JSON.
|
||||
|
||||
Use `JSON.parse()` and a `try... catch` block to check if the provided argument is a valid JSON.
|
||||
Use `JSON.parse()` and a `try... catch` block to check if the provided string is a valid JSON.
|
||||
|
||||
```js
|
||||
const isValidJSON = obj => {
|
||||
const isValidJSON = str => {
|
||||
try {
|
||||
JSON.parse(obj);
|
||||
JSON.parse(str);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
||||
@ -176,9 +176,9 @@
|
||||
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">isSymbol</span><span class="token punctuation">(</span><span class="token function">Symbol</span><span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">));</span> <span class="token comment">// true</span>
|
||||
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="isundefined">isUndefined</h4><p>Returns <code>true</code> if the specified value is <code>undefined</code>, <code>false</code> otherwise.</p><p>Use the strict equality operator to check if the value and of <code>val</code> are equal to <code>undefined</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">isUndefined</span> <span class="token operator">=</span> val <span class="token operator">=></span> val <span class="token operator">===</span> undefined<span class="token punctuation">;</span>
|
||||
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">isUndefined</span><span class="token punctuation">(</span>undefined<span class="token punctuation">);</span> <span class="token comment">// true</span>
|
||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="isvalidjson">isValidJSON</h4><p>Checks if the provided argument is a valid JSON.</p><p>Use <code>JSON.parse()</code> and a <code>try... catch</code> block to check if the provided argument is a valid JSON.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">isValidJSON</span> <span class="token operator">=</span> obj <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="isvalidjson">isValidJSON</h4><p>Checks if the provided string is a valid JSON.</p><p>Use <code>JSON.parse()</code> and a <code>try... catch</code> block to check if the provided string is a valid JSON.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">isValidJSON</span> <span class="token operator">=</span> str <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||
<span class="token keyword">try</span> <span class="token punctuation">{</span>
|
||||
<span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>obj<span class="token punctuation">);</span>
|
||||
<span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>str<span class="token punctuation">);</span>
|
||||
<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;
|
||||
}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">e</span><span class="token punctuation">) {</span>
|
||||
<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;
|
||||
|
||||
@ -613,9 +613,9 @@ const isSymbol = val => typeof val === 'symbol';
|
||||
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
|
||||
const isUndefined = val => val === undefined;
|
||||
const isUpperCase = str => str === str.toUpperCase();
|
||||
const isValidJSON = obj => {
|
||||
const isValidJSON = str => {
|
||||
try {
|
||||
JSON.parse(obj);
|
||||
JSON.parse(str);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user