Skip to content

Commit

Permalink
docs: add json docs (#20)
Browse files Browse the repository at this point in the history
* fix: unify zh docs subtitle

* docs: add json docs
  • Loading branch information
Aybrea authored Nov 3, 2024
1 parent 7936fb5 commit a972e82
Show file tree
Hide file tree
Showing 25 changed files with 146 additions and 20 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` |
2 changes: 1 addition & 1 deletion docs/zh/collection/merge-with.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

递归合并两个对象,可通过回调函数自定义合并逻辑。

### 用法
### 使用

```ts
import { mergeWith } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/collection/merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

递归合并两个对象。

### 用法
### 使用

```ts
import { merge } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/file/to-array-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`File` 对象转换为 `ArrayBuffer`

### 用法
### 使用

```ts
import { toArrayBuffer } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/file/to-data-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`File` 对象转换为 Data URL 字符串。

### 用法
### 使用

```ts
import { toDataURL } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/file/to-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`File` 对象转换为文本字符串。

### 用法
### 使用

```ts
import { toText } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/cancel-animation-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

使用给定的句柄取消 `requestAnimationFrame` 请求,并使用 `clearTimeout` 作为回退选项。

### 用法
### 使用

```ts
import { cancelAnimationFrame } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

### 用法
### 使用

```ts
import { classes } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/create-namespace-fn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

创建一个命名空间函数,用于生成 BEM 样式的组件和类名。

### 用法
### 使用

```ts
import { createNamespaceFn } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/double-raf.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

创建一个基于 `Promise` 的双重 `requestAnimationFrame`,在两帧之后 `resolved`

### 用法
### 使用

```ts
import { doubleRaf } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-all-parent-scroller.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

获取元素的所有可滚动父级元素,包含 `window` 作为最后一项。

### 用法
### 使用

```ts
import { getAllParentScroller } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-parent-scroller.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

查找元素的最近可滚动父级元素。如果没有找到滚动父级,则返回 `window`

### 用法
### 使用

```ts
import { getParentScroller } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-rect.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

获取元素或窗口的尺寸和位置,返回一个 `DOMRect` 对象。

### 用法
### 使用

```ts
import { getRect } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-scroll-left.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

获取元素或窗口的水平滚动位置。

### 用法
### 使用

```ts
import { getScrollLeft } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-scroll-top.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

获取元素或窗口的垂直滚动位置。

### 用法
### 使用

```ts
import { getScrollTop } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/get-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

获取给定 DOM 元素的计算 CSS 样式。

### 用法
### 使用

```ts
import { getStyle } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/in-viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

判断元素是否在视口内可见。

### 用法
### 使用

```ts
import { inViewport } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/mitt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

事件调度器,集成了 [mitt](https://github.com/developit/mitt)

### 用法
### 使用

```ts
import { mitt } from 'rattail'
Expand Down
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` |
2 changes: 1 addition & 1 deletion docs/zh/util/prevent-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

阻止事件的默认行为(如果该事件可取消)。

### 用法
### 使用

```ts
import { preventDefault } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/raf.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

创建一个基于 `Promise``requestAnimationFrame`,在下一帧时 `resolved`

### 用法
### 使用

```ts
import { raf } from 'rattail'
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/util/request-animation-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

提供跨浏览器兼容的 `requestAnimationFrame` 函数,并使用 `setTimeout` 作为回退选项。

### 用法
### 使用

```ts
import { requestAnimationFrame } from 'rattail'
Expand Down
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 a972e82

Please sign in to comment.