* Formats a query parameter. * * @param key The key of the query parameter to format. * @param value The value of the query parameter to format. * @param stringFormattingFn The function used to format string values. Can * be used to encode o
(key, value, stringFormattingFn)
| 362 | * @returns *[] the formatted [key, value] pair. |
| 363 | */ |
| 364 | function formatQueryValue(key, value, stringFormattingFn) { |
| 365 | // TODO: Probably refactor code to replace `switch(true)` with `if`/`else`. |
| 366 | switch (true) { |
| 367 | case typeof value === 'number': // fall-through |
| 368 | case typeof value === 'boolean': |
| 369 | value = value.toString() |
| 370 | break |
| 371 | case value === null: |
| 372 | case value === undefined: |
| 373 | value = '' |
| 374 | break |
| 375 | case typeof value === 'string': |
| 376 | if (stringFormattingFn) { |
| 377 | value = stringFormattingFn(value) |
| 378 | } |
| 379 | break |
| 380 | case value instanceof RegExp: |
| 381 | break |
| 382 | case Array.isArray(value): { |
| 383 | value = value.map(function (val, idx) { |
| 384 | return formatQueryValue(idx, val, stringFormattingFn)[1] |
| 385 | }) |
| 386 | break |
| 387 | } |
| 388 | case typeof value === 'object': { |
| 389 | value = Object.entries(value).reduce(function (acc, [subKey, subVal]) { |
| 390 | const subPair = formatQueryValue(subKey, subVal, stringFormattingFn) |
| 391 | acc[subPair[0]] = subPair[1] |
| 392 | |
| 393 | return acc |
| 394 | }, {}) |
| 395 | break |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (stringFormattingFn) key = stringFormattingFn(key) |
| 400 | return [key, value] |
| 401 | } |
| 402 | |
| 403 | function isStream(obj) { |
| 404 | return ( |
nothing calls this directly
no outgoing calls
no test coverage detected