-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbee6a2
commit 8bafd6f
Showing
8 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opentf/utils": minor | ||
--- | ||
|
||
Added isJSON types utility fn. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
> Checks whether the given string is a valid JSON plain object | ||
## Syntax | ||
|
||
```ts | ||
import { isJSON } from '@opentf/utils'; | ||
|
||
isJSON(str: string): boolean; | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
// Falsy cases | ||
// *********** | ||
|
||
isJSON(""); //=> false | ||
|
||
isJSON("undefined"); //=> false | ||
|
||
isJSON("null"); //=> false | ||
|
||
isJSON("true"); //=> false | ||
|
||
isJSON("false"); //=> false | ||
|
||
isJSON("1"); //=> false | ||
|
||
isJSON("12345"); //=> false | ||
|
||
isJSON("abc"); //=> false | ||
|
||
isJSON("{a}"); //=> false | ||
|
||
isJSON("{a:}"); //=> false | ||
|
||
isJSON("{a:1}"); //=> false | ||
|
||
isJSON('{"a": undefined}'); //=> false | ||
|
||
isJSON("[]"); //=> false | ||
|
||
// Truthy cases | ||
// ************ | ||
|
||
isJSON("{}"); //=> true | ||
|
||
isJSON('{"a":1}'); //=> true | ||
|
||
isJSON( | ||
'{"a": "abc", "b": true, "c": false, "d": [{"e": 1}, null, 5.0], "f": {}}' | ||
); //=> true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { isJSON } from '../../src'; | ||
|
||
describe('Types > isJSON', () => { | ||
test('invalid cases', () => { | ||
expect(isJSON()).toBeFalsy(); | ||
expect(isJSON('')).toBeFalsy(); | ||
expect(isJSON('undefined')).toBeFalsy(); | ||
expect(isJSON('null')).toBeFalsy(); | ||
expect(isJSON('true')).toBeFalsy(); | ||
expect(isJSON('false')).toBeFalsy(); | ||
expect(isJSON('1')).toBeFalsy(); | ||
expect(isJSON('12345')).toBeFalsy(); | ||
expect(isJSON('abc')).toBeFalsy(); | ||
expect(isJSON('{a}')).toBeFalsy(); | ||
expect(isJSON('{a:}')).toBeFalsy(); | ||
expect(isJSON('{a:1}')).toBeFalsy(); | ||
expect(isJSON('{"a": undefined}')).toBeFalsy(); | ||
expect(isJSON('[]')).toBeFalsy(); | ||
}); | ||
|
||
test('valid cases', () => { | ||
expect(isJSON('{}')).toBeTruthy(); | ||
expect(isJSON('{"a":1}')).toBeTruthy(); | ||
expect( | ||
isJSON( | ||
'{"a": "abc", "b": true, "c": false, "d": [{"e": 1}, null, 5.0], "f": {}}' | ||
) | ||
).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import isObj from './isObj'; | ||
/** | ||
* Checks whether the given string is a valid JSON plain object | ||
* | ||
* @example | ||
* | ||
* isJSON("null") //=> false | ||
* | ||
* isJSON("[]") //=> false | ||
* | ||
* isJSON("{}") //=> true | ||
*/ | ||
|
||
export default function isJSON(str: string) { | ||
try { | ||
const o = JSON.parse(str); | ||
return isObj(o); | ||
} catch (error) { | ||
return false; | ||
} | ||
} |