(value, options)
| 240 | |
| 241 | // `parse()` only splits on a literal separator, so a separator inside an item is percent-encoded. `encodeURIComponent` leaves some ASCII characters as-is, like `-`, `.`, `_`, and `~`, so the pieces of the item between the separators are encoded separately and joined with the percent-encoded separator. The encoded item is not searched for the separator, since a separator that is a hex digit would clash with the `%XX` escapes. |
| 242 | function encodeArrayItem(value, options) { |
| 243 | // `encode()` validates the value type first, since `String()` would hide an unsupported type. |
| 244 | const encodedValue = encode(value, options); |
| 245 | const stringValue = String(value); |
| 246 | const {arrayFormatSeparator} = options; |
| 247 | if (!options.encode || !stringValue.includes(arrayFormatSeparator)) { |
| 248 | return encodedValue; |
| 249 | } |
| 250 | |
| 251 | const percentEncodedSeparator = encodeURIComponent(arrayFormatSeparator); |
| 252 | // `encodeURIComponent` leaves a few characters as-is, like `-`, `.`, `_`, `~`, and `*`, so the separator has to be encoded by hand for those. |
| 253 | const encodedSeparator = percentEncodedSeparator === arrayFormatSeparator |
| 254 | ? `%${arrayFormatSeparator.codePointAt(0).toString(16).toUpperCase().padStart(2, '0')}` |
| 255 | : percentEncodedSeparator; |
| 256 | |
| 257 | return stringValue.split(arrayFormatSeparator).map(piece => encode(piece, options)).join(encodedSeparator); |
| 258 | } |
| 259 | |
| 260 | // One percent sign, anywhere. Text between two of these is copied in one go, so a value with no escapes in it costs a single native scan instead of a step per character. A value that holds no escape at all, which is the common case, is returned as it is. |
| 261 | // The `lastIndex` of this and `percentEscape` is shared state, which is only safe because nothing that a caller controls runs while `decodeComponent()` is on the stack. Every user callback, which is a `types` function, a `sort` comparator, a `pick()` filter, and a `replacer`, runs after the loop that decodes the parameters. A callback, a comparator, or an `await` inside `decodeComponent()` would corrupt the two regexes, so set `lastIndex` right before each `exec` and do not reach for a `matchAll` or a `String#replace` here. |
no test coverage detected