-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@142vip/utils): 增加
JSON
模块的封装,支持克隆、序列化、解析 (#259)
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.