Skip to content

Commit

Permalink
release 5.17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Jan 30, 2024
1 parent 84fca53 commit e0320d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change logs

## 2024.01.21 v5.17.1

1. `safeParse` `safeStringify` supports converting bigint

## 2024.01.22 v5.17.0

1. `randomColor` supports a range of custom color values, which can be used to customize the generation of dark, light, warm, etc.
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,8 @@ declare function spliceUrlParam(

Secure parsing of JSON strings

> support BigInt since `v5.17.1`

- Since: `5.16.0`

- Arguments:
Expand All @@ -1588,8 +1590,8 @@ Secure parsing of JSON strings
safeParse('100')
// 100
safeParse('{"a":"undefined","b":"NaN","c":"Infinity"}')
// { b: NaN, c: Infinity }
safeParse('{"a":"undefined","b":"NaN","c":"Infinity","d":"9007199254740993"}')
// { b: NaN, c: Infinity, d: 9007199254740993n }
```

- Types:
Expand All @@ -1602,6 +1604,8 @@ declare function safeParse(data: string, covert?: boolean): any

Secure stringify of JSON Object

> support BigInt since `v5.17.1`

- Since: `5.16.0`

- Arguments:
Expand All @@ -1628,8 +1632,8 @@ safeStringify(NaN)
safeStringify(Infinity)
// "Infinity"
safeStringify({ a: undefined, b: NaN, c: Infinity })
// {"a":"undefined","b":"NaN","c":"Infinity"}
safeStringify({ a: undefined, b: NaN, c: Infinity, d: BigInt(Number.MAX_SAFE_INTEGER) + 2n })
// {"a":"undefined","b":"NaN","c":"Infinity","d":"9007199254740993"}
```

- Types:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-cool",
"description": "Collection of common JavaScript / TypeScript utilities",
"version": "5.17.0",
"version": "5.17.1",
"packageManager": "pnpm@8.9.2",
"main": "dist/index.cjs.js",
"module": "dist/index.esm-bundler.js",
Expand Down
14 changes: 10 additions & 4 deletions src/safeParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* safeParse('100')
* // 100
*
* safeParse('{"a":"undefined","b":"NaN","c":"Infinity"}')
* // { b: NaN, c: Infinity }
* safeParse('{"a":"undefined","b":"NaN","c":"Infinity","d":"9007199254740993"}')
* // { b: NaN, c: Infinity, d: 9007199254740993n }
* ```
* @param data - JSON string
* @param covert - Whether to convert data, default: true
Expand All @@ -21,9 +21,15 @@ function safeParse(data: string, covert = true): any {
'-Infinity': -Infinity
}
return JSON.parse(data, (key, val) => {
if (covert && ['Infinity', '-Infinity', 'undefined', 'NaN'].includes(val)) {
if (covert && ['Infinity', '-Infinity', 'undefined', 'NaN'].includes(val))
return VALUE_MAP[val as keyof typeof VALUE_MAP]
}
else if (
typeof val === 'string' &&
/^(\-|\+)?\d+(\.\d+)?$/.test(val) &&
!Number.isSafeInteger(+val)
)
return BigInt(val)

return val
})
}
Expand Down
10 changes: 7 additions & 3 deletions src/safeStringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
* safeStringify(Infinity)
* // "Infinity"
*
* safeStringify({ a: undefined, b: NaN, c: Infinity })
* // {"a":"undefined","b":"NaN","c":"Infinity"}
* safeStringify({ a: undefined, b: NaN, c: Infinity, d: BigInt(Number.MAX_SAFE_INTEGER) + 2n })
* // {"a":"undefined","b":"NaN","c":"Infinity","d":"9007199254740993"}
* ```
* @param data - JSON Object
* @param covert - Whether to convert data, default: true
* @returns - JSON String
*/
function safeStringify(data: any, covert = true): string {
return JSON.stringify(data, (key, val) => {
if (covert && [Infinity, -Infinity, undefined, NaN].includes(val)) return String(val)
if (covert) {
if ([Infinity, -Infinity, undefined, NaN].includes(val)) return String(val)
else if (typeof val === 'number' && !Number.isSafeInteger(val))
return String(BigInt(val))
} else if (typeof val === 'bigint') return String(val)
return val
})
}
Expand Down

0 comments on commit e0320d5

Please sign in to comment.