diff --git a/docs/.vitepress/items/util.ts b/docs/.vitepress/items/util.ts index 6474e24..2dda5e4 100644 --- a/docs/.vitepress/items/util.ts +++ b/docs/.vitepress/items/util.ts @@ -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' }, ] diff --git a/docs/util/pretty-JSON-object.md b/docs/util/pretty-JSON-object.md new file mode 100644 index 0000000..703212b --- /dev/null +++ b/docs/util/pretty-JSON-object.md @@ -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` | diff --git a/docs/util/try-parse-JSON.md b/docs/util/try-parse-JSON.md new file mode 100644 index 0000000..359d77d --- /dev/null +++ b/docs/util/try-parse-JSON.md @@ -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` | diff --git a/docs/zh/util/pretty-JSON-object.md b/docs/zh/util/pretty-JSON-object.md new file mode 100644 index 0000000..716f8e4 --- /dev/null +++ b/docs/zh/util/pretty-JSON-object.md @@ -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` | diff --git a/docs/zh/util/try-parse-JSON.md b/docs/zh/util/try-parse-JSON.md new file mode 100644 index 0000000..0302896 --- /dev/null +++ b/docs/zh/util/try-parse-JSON.md @@ -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` |