Skip to content

Commit

Permalink
feat: add isJSON util fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Feb 12, 2024
1 parent bbee6a2 commit 8bafd6f
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/warm-news-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Added isJSON types utility fn.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ await sleep(1000); // It suspends the exection for 1 second.

- [isObj](https://js-utils.pages.dev/docs/Object/isObj)
- [isNumber](https://js-utils.pages.dev/docs/Types/isNumber)
- [isJSON](https://js-utils.pages.dev/docs/Types/isJSON)

## License

Expand Down
53 changes: 53 additions & 0 deletions apps/website/docs/Types/isJSON.mdx
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
```
1 change: 1 addition & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ await sleep(1000); // It suspends the exection for 1 second.

- [isObj](https://js-utils.pages.dev/Object/isObj)
- [isNumber](https://js-utils.pages.dev/Types/isNumber)
- [isJSON](https://js-utils.pages.dev/docs/Types/isJSON)

## License

Expand Down
30 changes: 30 additions & 0 deletions packages/utils/__tests__/types/isJSON.ts
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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function fun(x?: any) {
return x;
}

describe('Object', () => {
describe('Types', () => {
test('isObj', () => {
expect(isObj(undefined)).toBeFalsy();
expect(isObj(null)).toBeFalsy();
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { default as percentageOf } from './maths/percentageOf';
// Types
export { default as isNumber } from './types/isNumber';
export { default as isObj } from './types/isObj';
export { default as isJSON } from './types/isJSON';

// Object
export { default as getInObj } from './object/getInObj';
Expand Down
21 changes: 21 additions & 0 deletions packages/utils/src/types/isJSON.ts
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;
}
}

0 comments on commit 8bafd6f

Please sign in to comment.