Skip to content

Commit

Permalink
feat(isJSONObject, isJSONValue, isJSONArray): Implement `isJSONObject…
Browse files Browse the repository at this point in the history
…`, `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
3 people authored Sep 29, 2024
1 parent 0c7d889 commit ded5675
Show file tree
Hide file tree
Showing 26 changed files with 608 additions and 24 deletions.
12 changes: 12 additions & 0 deletions benchmarks/performance/isJSONObject.bench.ts
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/);
});
});
12 changes: 9 additions & 3 deletions docs/.vitepress/libs/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
"some",
"sortBy"
],
"date": ["now"],
"date": [
"now"
],
"function": [
"after",
"ary",
Expand Down Expand Up @@ -194,7 +196,11 @@
"sum",
"sumBy"
],
"number": ["clamp", "inRange", "random"],
"number": [
"clamp",
"inRange",
"random"
],
"object": [
"assign",
"assignIn",
Expand Down Expand Up @@ -308,4 +314,4 @@
"toPath",
"uniqueId"
]
}
}
28 changes: 28 additions & 0 deletions docs/ja/reference/predicate/isJSONArray.md
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
```
27 changes: 27 additions & 0 deletions docs/ja/reference/predicate/isJSONObject.md
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
```
39 changes: 39 additions & 0 deletions docs/ja/reference/predicate/isJSONValue.md
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
```
6 changes: 3 additions & 3 deletions docs/ja/reference/predicate/isPlainObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
## インターフェース

```typescript
function isPlainObject(object: object): boolean;
function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
```

### パラメータ

- `object` (`object`): 検査する値。
- `value` (`unknown`): 検査する値。

### 戻り値

(`boolean`): 値がプレーンオブジェクトの場合はtrue
(`value is Record<PropertyKey, any>`): 値がプレーンオブジェクトの場合は`true`

##

Expand Down
28 changes: 28 additions & 0 deletions docs/ko/reference/predicate/isJSONArray.md
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
```
27 changes: 27 additions & 0 deletions docs/ko/reference/predicate/isJSONObject.md
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
```
39 changes: 39 additions & 0 deletions docs/ko/reference/predicate/isJSONValue.md
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
```
6 changes: 3 additions & 3 deletions docs/ko/reference/predicate/isPlainObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
## 인터페이스

```typescript
function isPlainObject(object: object): boolean;
function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
```

### 파라미터

- `object` (`object`): 검사할 값.
- `value` (`unknown`): 검사할 값.

### 반환 값

(`boolean`): 값이 순수 객체이면 true.
(`value is Record<PropertyKey, any>`): 값이 순수 객체이면 `true`.

## 예시

Expand Down
28 changes: 28 additions & 0 deletions docs/reference/predicate/isJSONArray.md
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
```
28 changes: 28 additions & 0 deletions docs/reference/predicate/isJSONObject.md
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
```
39 changes: 39 additions & 0 deletions docs/reference/predicate/isJSONValue.md
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
```
6 changes: 3 additions & 3 deletions docs/reference/predicate/isPlainObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ Checks if a given value is a plain object.
## Signature

```typescript
function isPlainObject(object: object): boolean;
function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
```

### Parameters

- `object` (`object`): The value to check.
- `value` (`unknown`): The value to check.

### Returns

(`boolean`): True if the value is a plain object, otherwise false.
(`value is Record<PropertyKey, any>`): True if the value is a plain object, otherwise false.

## Examples

Expand Down
Loading

0 comments on commit ded5675

Please sign in to comment.