Skip to content

Commit

Permalink
fix(@142vip/utils): 增加JSON模块的封装,支持克隆、序列化、解析 (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl authored Dec 26, 2024
1 parent e6750bc commit 1f1ef10
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"commander": "12.1.0",
"dayjs": "1.11.11",
"inquirer": "7",
"klona": "2.0.6",
"nanoid": "3.3.8",
"qs": "6.13.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './docker'
export * from './qs'
export * from './dayjs'
export * from './nanoid'
export * from './json'
40 changes: 40 additions & 0 deletions packages/utils/src/core/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { klona } from 'klona/json'

/**
* json克隆复制
* 参考:https://www.npmjs.com/package/klona
*/
function clone<T>(json: T) {
return klona(json)
}

/**
* JSON序列化
* @param value
* @param replacer
* @param space
*/
function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string {
return JSON.stringify(value, replacer, space)
}

/**
* 解析JSON串
* @param originData
* @param defaultData
*/
function parse<T>(originData: string | undefined | null, defaultData: T): T {
if (originData == null || originData.length === 0) {
return defaultData
}
return JSON.parse(originData)
}

/**
* 处理JSON
*/
export const VipJSON = {
clone,
stringify,
parse,
}
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f1ef10

Please sign in to comment.