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 2, 2024
1 parent 9be6ecf commit 089787a
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
mathItems,
utilItems,
fileItems,
jsonItems,
} from './items'

function withI18n(items: { link: string; text: string }[], locale: 'zh') {
Expand Down Expand Up @@ -77,6 +78,10 @@ export default defineConfig({
text: '工具',
items: withI18n(utilItems, 'zh'),
},
{
text: 'JSON',
items: withI18n(jsonItems, 'zh'),
},
],

docFooter: {
Expand Down Expand Up @@ -139,6 +144,10 @@ export default defineConfig({
text: 'Util',
items: utilItems,
},
{
text: 'JSON',
items: jsonItems,
},
],

socialLinks: [{ icon: 'github', link: 'https://github.com/varletjs/rattail' }],
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './function'
export * from './math'
export * from './util'
export * from './file'
export * from './json'
4 changes: 4 additions & 0 deletions docs/.vitepress/items/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const jsonItems = [
{ text: 'tryParseJSON', link: '/json/try-parse-JSON' },
{ text: 'prettyJSONObject', link: '/json/pretty-JSON-object' },
]
33 changes: 33 additions & 0 deletions docs/json/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/json/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` |
23 changes: 23 additions & 0 deletions docs/zh/element/classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# classes

根据给定条件生成类名列表,或直接返回类名。

### 使用

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

const classList = classes('active', [true, 'visible', 'hidden'])
```

### 参数

| 参数 | 类型 | 默认值 |
| --------- | --------- | ------ |
| `classes` | `Classes` | |

### 返回值

| 类型 |
| ------- |
| `any[]` |
33 changes: 33 additions & 0 deletions docs/zh/json/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/json/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 089787a

Please sign in to comment.