MCPcopy Create free account
hub / github.com/sindresorhus/query-string / encode

Function encode

base.js:223–239  ·  view source on GitHub ↗
(value, options)

Source from the content-addressed store, hash-verified

221}
222
223function encode(value, options) {
224 const type = typeof value;
225 if (type === 'function' || type === 'symbol' || (type === 'object' && value !== null)) {
226 throw new TypeError(`Unsupported value type: ${type}. Supported types are string, number, bigint, boolean, null, and undefined. Use the \`replacer\` option to transform other types.`);
227 }
228
229 if (options.encode) {
230 try {
231 return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
232 } catch {
233 // A lone surrogate cannot be encoded as UTF-8, so `encodeURIComponent` throws for it. It is replaced with U+FFFD, like `URLSearchParams` and the `URL` hash setter do. `\p{Surrogate}` matches only a surrogate that is not part of a pair, so a valid pair is left alone.
234 return encode(String(value).replaceAll(/\p{Surrogate}/gv, '\u{FFFD}'), options);
235 }
236 }
237
238 return value;
239}
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.
242function encodeArrayItem(value, options) {

Callers 3

encoderForArrayFormatFunction · 0.85
encodeArrayItemFunction · 0.85
stringifyFunction · 0.85

Calls 1

strictUriEncodeFunction · 0.85

Tested by

no test coverage detected