(
param: { name?: string; value?: string | number },
/** allow empty names and values */
strict?: boolean,
)
| 58 | * Build a querystring parameter from a param object |
| 59 | */ |
| 60 | export const buildQueryParameter = ( |
| 61 | param: { name?: string; value?: string | number }, |
| 62 | |
| 63 | /** allow empty names and values */ |
| 64 | strict?: boolean, |
| 65 | ) => { |
| 66 | strict = strict === undefined ? true : strict; |
| 67 | |
| 68 | // Skip non-name ones in strict mode |
| 69 | if (strict && !param.name) { |
| 70 | return ''; |
| 71 | } |
| 72 | |
| 73 | // Cast number values to strings |
| 74 | if (typeof param.value === 'number') { |
| 75 | param.value = String(param.value); |
| 76 | } |
| 77 | |
| 78 | if (!strict || param.value) { |
| 79 | // Don't encode ',' in values |
| 80 | const value = flexibleEncodeComponent(param.value || '').replace(/%2C/gi, ','); |
| 81 | const name = flexibleEncodeComponent(param.name || ''); |
| 82 | |
| 83 | return `${name}=${value}`; |
| 84 | } else { |
| 85 | return flexibleEncodeComponent(param.name); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Build a querystring from a list of name/value pairs |
no test coverage detected