Skip to content

Commit

Permalink
docs: add json docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aybrea committed Nov 3, 2024
1 parent 5bc4e60 commit 3ffee71
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/.vitepress/items/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export const utilItems = [
{ text: 'getScrollLeft', link: '/util/get-scroll-left' },
{ text: 'getParentScroller', link: '/util/get-parent-scroller' },
{ text: 'getAllParentScroller', link: '/util/get-all-parent-scroller' },
{ text: 'prettyJSONObject', link: '/util/pretty-JSON-object' },
{ text: 'tryParseJSON', link: '/util/try-parse-JSON' },
]
33 changes: 33 additions & 0 deletions docs/util/pretty-JSON-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# prettyJSONObject

Formats a `JSON` object with indentation for easy readability.

### Usage

```ts
import { prettyJSONObject } from 'rattail'

const jsonObject = { key: 'value', nested: { key: 'nestedValue' } }
const pretty = prettyJSONObject(jsonObject)
console.log(pretty)
/*
{
"key": "value",
"nested": {
"key": "nestedValue"
}
}
*/
```

### Arguments

| Arg | Type | Defaults |
| ------------ | -------- | -------- |
| `jsonObject` | `object` | |

### Return

| Type |
| -------- |
| `string` |
29 changes: 29 additions & 0 deletions docs/util/try-parse-JSON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# tryParseJSON

Attempts to parse a `JSON` string. If parsing fails, returns `undefined`.

### Usage

```ts
import { tryParseJSON } from 'rattail'

const jsonString = '{"key": "value"}'
const parsed = tryParseJSON(jsonString)
console.log(parsed) // { key: "value" }

const invalidJsonString = '{"key": value}'
const invalidParsed = tryParseJSON(invalidJsonString)
console.log(invalidParsed) // undefined
```

### Arguments

| Arg | Type | Defaults |
| ------ | -------- | -------- |
| `json` | `string` | |

### Return

| Type |
| --------------------- |
| `object \| undefined` |
33 changes: 33 additions & 0 deletions docs/zh/util/pretty-JSON-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# prettyJSONObject

格式化 `JSON` 对象,增加缩进以便于阅读。

### 使用

```ts
import { prettyJSONObject } from 'rattail'

const jsonObject = { key: 'value', nested: { key: 'nestedValue' } }
const pretty = prettyJSONObject(jsonObject)
console.log(pretty)
/*
{
"key": "value",
"nested": {
"key": "nestedValue"
}
}
*/
```

### 参数

| 参数 | 类型 | 默认值 |
| ------------ | -------- | ------ |
| `jsonObject` | `object` | |

### 返回值

| 类型 |
| -------- |
| `string` |
29 changes: 29 additions & 0 deletions docs/zh/util/try-parse-JSON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# tryParseJSON

尝试解析 `JSON` 字符串。如果解析失败,返回 `undefined`

### 使用

```ts
import { tryParseJSON } from 'rattail'

const jsonString = '{"key": "value"}'
const parsed = tryParseJSON(jsonString)
console.log(parsed) // { key: "value" }

const invalidJsonString = '{"key": value}'
const invalidParsed = tryParseJSON(invalidJsonString)
console.log(invalidParsed) // undefined
```

### 参数

| 参数 | 类型 | 默认值 |
| ------ | -------- | ------ |
| `json` | `string` | |

### 返回值

| 类型 |
| --------------------- |
| `object \| undefined` |

0 comments on commit 3ffee71

Please sign in to comment.