-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isJSONObject, isJSONValue, isJSONArray): Implement `isJSONObject…
…`, `isJSONValue`, `isJSONArray` (#563) * feat:init * feat * fix:use prettier * feat(isPlainObject): Update signature of isPlainObject * feat: Add isJSONValue, isJSONArray, and refactor isJSONObject * remove from functions * docs: Update docs * docs: Update docs * docs: Update docs * docs: Update docs * docs: Update docs * docs: Update docs * docs: Update docs --------- Co-authored-by: Sojin Park <[email protected]> Co-authored-by: Sojin Park <[email protected]>
- Loading branch information
1 parent
0c7d889
commit ded5675
Showing
26 changed files
with
608 additions
and
24 deletions.
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,12 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { isJSONObject as isJSONObjectToolkit } from 'es-toolkit'; | ||
|
||
describe('isJSONObject', () => { | ||
bench('es-toolkit/isJSONObject', () => { | ||
isJSONObjectToolkit({ nested: { boolean: true, array: [1, 2, 3], string: 'test', null: null } }); | ||
isJSONObjectToolkit({ date: new Date() }); | ||
isJSONObjectToolkit({ nested: { a: function* () {} } }); | ||
isJSONObjectToolkit(undefined); | ||
isJSONObjectToolkit(/test/); | ||
}); | ||
}); |
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,28 @@ | ||
# isJSONArray | ||
|
||
与えられた値が有効なJSON配列かどうかを確認します。 | ||
|
||
有効なJSON配列は、すべての項目が[有効なJSON値](./isJSONValue.md)である配列として定義されます。 | ||
|
||
## インターフェース | ||
|
||
```typescript | ||
function isJSONArray(value: unknown): value is any[]; | ||
``` | ||
|
||
### パラメータ | ||
|
||
- `value` (`unknown`): 確認する値。 | ||
|
||
### 戻り値 | ||
|
||
(`value is any[]`): 値が有効なJSON配列であれば`true`、それ以外の場合は`false`。 | ||
|
||
## 例 | ||
|
||
```typescript | ||
console.log(isJSONArray([1, 2, 3])); // true | ||
console.log(isJSONArray(['string', null, true])); // true | ||
console.log(isJSONArray([1, 2, () => {}])); // false | ||
console.log(isJSONArray('not an array')); // false | ||
``` |
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,27 @@ | ||
# isJSONObject | ||
|
||
値がJSONオブジェクトかどうかを確認します。 | ||
|
||
有効なJSONオブジェクトとは、キーが文字列で、値が[有効なJSON値](./isJSONValue.md)を持つオブジェクトです。 | ||
|
||
## インターフェース | ||
|
||
```typescript | ||
function isJSONObject(obj: unknown): obj is Record<string, any>; | ||
``` | ||
|
||
### パラメータ | ||
|
||
- `obj` (`unknown`): 確認する値。 | ||
|
||
### 戻り値 | ||
|
||
(`obj is Record<string, any>`): `obj`がJSONオブジェクトである場合は`true`、それ以外の場合は`false`。 | ||
|
||
## 例 | ||
|
||
```typescript | ||
isJSONObject({ nested: { boolean: true, array: [1, 2, 3], string: 'test', null: null } }); // true | ||
isJSONObject({ regexp: /test/ }); // false | ||
isJSONObject(123); // false | ||
``` |
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,39 @@ | ||
# isJSONValue | ||
|
||
与えられた値が有効なJSON値かどうかを確認します。 | ||
|
||
有効なJSON値とは、次のいずれかを指します。 | ||
|
||
- [JSONオブジェクト](./isJSONObject.md)(文字列キーと有効なJSON値を持つオブジェクト) | ||
- [JSON配列](./isJSONArray.md)(有効なJSON値の配列) | ||
- 文字列(`string`) | ||
- 数字(`number`) | ||
- 真偽値(`boolean`) | ||
- `null` | ||
|
||
## インターフェース | ||
|
||
```typescript | ||
function isJSONValue(value: unknown): value is Record<string, any> | any[] | string | number | boolean | null; | ||
``` | ||
|
||
### パラメータ | ||
|
||
- `value` (`unknown`): チェックする値。 | ||
|
||
### 戻り値 | ||
|
||
(`value is Record<string, any> | any[] | string | number | boolean | null`): 値が有効なJSON値であれば`true`、そうでなければ`false`。 | ||
|
||
## 例 | ||
|
||
```typescript | ||
console.log(isJSONValue(null)); // true | ||
console.log(isJSONValue({ key: 'value' })); // true | ||
console.log(isJSONValue([1, 2, 3])); // true | ||
console.log(isJSONValue('Hello')); // true | ||
console.log(isJSONValue(42)); // true | ||
console.log(isJSONValue(true)); // true | ||
console.log(isJSONValue(undefined)); // false | ||
console.log(isJSONValue(() => {})); // false | ||
``` |
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,28 @@ | ||
# isJSONArray | ||
|
||
주어진 값이 유효한 JSON 배열인지 확인해요. | ||
|
||
유효한 JSON 배열이란, 모든 항목이 [유효한 JSON 값](./isJSONValue.md)인 배열이에요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function isJSONArray(value: unknown): value is any[]; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `value` (`unknown`): 확인할 값. | ||
|
||
### 반환 값 | ||
|
||
(`value is any[]`): 값이 유효한 JSON 배열이면 `true`, 그렇지 않으면 `false`. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
console.log(isJSONArray([1, 2, 3])); // true | ||
console.log(isJSONArray(['string', null, true])); // true | ||
console.log(isJSONArray([1, 2, () => {}])); // false | ||
console.log(isJSONArray('not an array')); // false | ||
``` |
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,27 @@ | ||
# isJSONObject | ||
|
||
값이 JSON 객체인지 확인해요. | ||
|
||
유효한 JSON 객체란, 키로 문자열을 가지고, 값으로 [유효한 JSON 값](./isJSONValue.md)을 가진 객체예요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function isJSONObject(obj: unknown): obj is Record<string, any>; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `obj` (`unknown`): 확인할 값. | ||
|
||
### 반환 값 | ||
|
||
(`obj is Record<string, any>`): `obj`가 JSON 객체이면 `true`, 그렇지 않으면 `false`. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
isJSONObject({ nested: { boolean: true, array: [1, 2, 3], string: 'test', null: null } }); // true | ||
isJSONObject({ regexp: /test/ }); // false | ||
isJSONObject(123); // false | ||
``` |
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,39 @@ | ||
# isJSONValue | ||
|
||
주어진 값이 유효한 JSON 값인지 확인해요. | ||
|
||
유효한 JSON 값이란, 다음 중 하나를 말해요. | ||
|
||
- [JSON 객체](./isJSONObject.md) (문자열 키와 유효한 JSON 값을 가진 객체) | ||
- [JSON 배열](./isJSONArray.md) (유효한 JSON 값의 배열) | ||
- 문자열 (`string`) | ||
- 숫자 (`number`) | ||
- 참값 (`boolean`) | ||
- `null` | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function isJSONValue(value: unknown): value is Record<string, any> | any[] | string | number | boolean | null; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `value` (`unknown`): 확인할 값. | ||
|
||
### 반환 값 | ||
|
||
(`value is Record<string, any> | any[] | string | number | boolean | null`): 값이 유효한 JSON 값이면 `true`, 그렇지 않으면 `false`. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
console.log(isJSONValue(null)); // true | ||
console.log(isJSONValue({ key: 'value' })); // true | ||
console.log(isJSONValue([1, 2, 3])); // true | ||
console.log(isJSONValue('Hello')); // true | ||
console.log(isJSONValue(42)); // true | ||
console.log(isJSONValue(true)); // true | ||
console.log(isJSONValue(undefined)); // false | ||
console.log(isJSONValue(() => {})); // false | ||
``` |
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,28 @@ | ||
# isJSONArray | ||
|
||
Checks if a given value is a valid JSON array. | ||
|
||
A valid JSON array is defined as an array where all items are [valid JSON values](./isJSONValue.md). | ||
|
||
## Signature | ||
|
||
```typescript | ||
function isJSONArray(value: unknown): value is any[]; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value` (`unknown`): The value to check. | ||
|
||
### Returns | ||
|
||
(`value is any[]`): True if the value is a valid JSON array, otherwise false. | ||
|
||
## Examples | ||
|
||
```typescript | ||
console.log(isJSONArray([1, 2, 3])); // true | ||
console.log(isJSONArray(['string', null, true])); // true | ||
console.log(isJSONArray([1, 2, () => {}])); // false | ||
console.log(isJSONArray('not an array')); // false | ||
``` |
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,28 @@ | ||
# isJSONObject | ||
|
||
Checks if a value is a JSON object. | ||
|
||
A valid JSON object is defined as an object with string keys and valid [JSON values](./isJSONValue.md). | ||
|
||
## Signature | ||
|
||
```typescript | ||
function isJSONObject(obj: unknown): obj is Record<string, any>; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value` (`unknown`): The value to check. | ||
|
||
### Returns | ||
|
||
(`obj is Record<string, any>`): True if the value is a JSON object, otherwise false. | ||
|
||
## Examples | ||
|
||
```typescript | ||
console.log(isJSONObject({ nested: { boolean: true, array: [1, 2, 3], string: 'test', number: 1, null: null } })); // true | ||
console.log(isJSONObject({})); // true | ||
console.log(isJSONObject({ regexp: /test/ })); // false | ||
console.log(isJSONObject(123)); // false | ||
``` |
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,39 @@ | ||
# isJSONValue | ||
|
||
Checks if a given value is a valid JSON value. | ||
|
||
A valid JSON value can be: | ||
|
||
- [a JSON object](./isJSONObject.md) (an object with string keys and valid JSON values) | ||
- [a JSON array](./isJSONArray.md) (an array of valid JSON values) | ||
- a `string` | ||
- a `number` | ||
- a `boolean` | ||
- `null` | ||
|
||
## Signature | ||
|
||
```typescript | ||
function isJSONValue(value: unknown): value is Record<string, any> | any[] | string | number | boolean | null; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value` (`unknown`): The value to check. | ||
|
||
### Returns | ||
|
||
(`value is Record<string, any> | any[] | string | number | boolean | null`): True if the value is a valid JSON value, otherwise false. | ||
|
||
## Examples | ||
|
||
```typescript | ||
console.log(isJSONValue(null)); // true | ||
console.log(isJSONValue({ key: 'value' })); // true | ||
console.log(isJSONValue([1, 2, 3])); // true | ||
console.log(isJSONValue('Hello')); // true | ||
console.log(isJSONValue(42)); // true | ||
console.log(isJSONValue(true)); // true | ||
console.log(isJSONValue(undefined)); // false | ||
console.log(isJSONValue(() => {})); // false | ||
``` |
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
Oops, something went wrong.