Skip to content

Commit

Permalink
fix: spliceUrlParam remove encodeURIComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Aug 9, 2024
1 parent 0809f34 commit 6b1ed35
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1534,40 +1534,45 @@ declare function parseUrlParam(url: string, covert?: boolean): Record<string, un
Splice URL parameters (single layer only)

> v5.20.0 Breaking change: remove encodeURIComponent
> v5.21.0 Breaking change: covert support boolean & SpliceUrlParamOptions

- Since: `5.3.0`

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ----------------- | -------------------------------------------------------------- | --------- | -------------- | -------- | ------- |
| params | json object | `object` | - | `true` | - |
| covert | Convert a null value type (null/undefined/) to an empty string | `boolean` | `true`/`false` | `false` | `false` |
| withQuestionsMark | Splicing a question mark | `boolean` | `true`/`false` | `false` | `true` |
| Parameters | Description | Type | Optional | Required | Default |
| ---------- | --------------------------------------------------------------------------------------- | ------------------------------------ | -------- | -------- | ------- |
| params | json object | `object` | - | `true` | - |
| covert | Convert a null value type (null/undefined/) to an empty string or spliceUrlParamOptions | `boolean` \| `SpliceUrlParamOptions` | - | `false` | `false` |

- Returns: `string`

- Example:

```ts
spliceUrlParam({ key1: '100', key2: 'true', key3: 'null', key4: 'undefined', key5: '测试' })
spliceUrlParam({ key1: '100', key2: true, key3: null, key4: undefined, key5: '测试' })
// ?key1=100&key2=true&key3=null&key4=undefined&key5=测试
spliceUrlParam({ key1: '100', key2: 'true', key3: 'null', key4: 'undefined' }, true)
spliceUrlParam({ key1: '100', key2: true, key3: null, key4: undefined }, true)
// ?key1=100&key2=true&key3=&key4=
spliceUrlParam({ key1: '100', key2: 'true', key3: 'null', key4: 'undefined' }, true, false)
spliceUrlParam({ key1: '100', key2: true, key3: null, key4: undefined }, { covert: true, withQuestionsMark: false })
// key1=100&key2=true&key3=&key4=
```

- Types:

```ts
declare function spliceUrlParam(
params: Record<string, unknown>,
covert?: boolean,
declare function spliceUrlParam<T extends Record<string, unknown>>(
params: T,
covert?: SpliceUrlParamOptions | boolean
): string
declare interface SpliceUrlParamOptions {
covert?: boolean
encode?: boolean
withQuestionsMark?: boolean
): string | null
}
```

#### safeParse
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.20.0",
"version": "5.21.0",
"packageManager": "pnpm@9.1.3",
"main": "dist/index.cjs.js",
"module": "dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export { default as osVersion, type OsVersion } from './osVersion'
export { default as browserVersion, type BrowserVersion } from './browserVersion'
export { default as compareVersion } from './compareVersion'
export { default as parseUrlParam } from './parseUrlParam'
export { default as spliceUrlParam } from './spliceUrlParam'
export { default as spliceUrlParam, type SpliceUrlParamOptions } from './spliceUrlParam'
export { default as safeParse } from './safeParse'
export { default as safeStringify } from './safeStringify'
export { default as getDirParam, type DirParamType } from './getDirParam'
Expand Down
36 changes: 25 additions & 11 deletions src/spliceUrlParam.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
export interface SpliceUrlParamOptions {
covert?: boolean
encode?: boolean
withQuestionsMark?: boolean
}

/**
* splice url params
*
* @example
* ```js
* spliceUrlParam('\{"key1":"100","key2":"true","key3":"null","key4":"undefined","key5":"测试"\}')
* spliceUrlParam('\{"key1":"100","key2":true,"key3":null,"key4":undefined,"key5":"测试"\}')
* // ?key1=100&key2=true&key3=null&key4=undefined&key5=测试
*
* spliceUrlParam('\{"key1":"100","key2":"true","key3":"null","key4":"undefined"\}', true)
* spliceUrlParam('\{"key1":"100","key2":true,"key3":null,"key4":undefined\}', true)
* // ?key1=100&key2=true&key3=&key4=
*
* spliceUrlParam('\{"key1":"100","key2":"true","key3":"null","key4":"undefined"\}', true, false)
* spliceUrlParam('\{"key1":"100","key2":true,"key3":null,"key4":undefined\}', true, false)
* // key1=100&key2=true&key3=&key4=
* ```
* @since 5.3.0
* @param params - json object
* @param covert - Convert a null value type (null/undefined/) to an empty string
* @param withQuestionsMark - Splicing a question mark, default: true
* @param covert - Convert a null value type (null/undefined/) to an empty string, default: false
* @returns - result
*/
function spliceUrlParam<T extends Record<string, unknown>>(
params: T,
covert = false,
withQuestionsMark = true
) {
covert: SpliceUrlParamOptions | boolean = false
): string {
if (!params) {
console.info('params is required')
return ''
}
let encode = false,
withQuestionsMark = true,
key: keyof typeof params

if (typeof covert === 'object') {
encode = covert.encode ?? false
withQuestionsMark = covert.withQuestionsMark ?? true
covert = covert.covert ?? false
}

let key: keyof typeof params
const result: string[] = []
for (key in params) {
typeof key === 'string' &&
result.push(`${key}=${'' + (covert ? params[key] ?? '' : params[key])}`)
if (typeof key === 'string') {
const val = '' + (covert ? params[key] ?? '' : params[key])
result.push(`${key}=${encode ? encodeURIComponent(val) : val}`)
}
}

if (withQuestionsMark) return '?' + result.join('&')
Expand Down

0 comments on commit 6b1ed35

Please sign in to comment.