(input, filter, options)
| 765 | } |
| 766 | |
| 767 | export function pick(input, filter, options) { |
| 768 | // The keys array is a filter, not an order. Filtering with a predicate keeps the parsed object's key order, which matters with `sort: false`. Integer-index keys are enumerated first in numeric order, and repeated keys are grouped together. |
| 769 | if (Array.isArray(filter)) { |
| 770 | const filterSet = new Set(filter); |
| 771 | // eslint-disable-next-line unicorn/no-useless-recursion -- The array case is the same operation as the predicate case, so it delegates instead of duplicating the loop. |
| 772 | return pick(input, key => filterSet.has(key), options); |
| 773 | } |
| 774 | |
| 775 | options = definedOptions(options); |
| 776 | |
| 777 | // `parseNumbers`, `parseBooleans`, and `types` only apply to the `value` the predicate receives. The URL is written back from the plain string values, so a value like `007` is kept as-is instead of being rewritten as `7`. |
| 778 | // The query is decoded exactly when it is encoded again, so a value like `a%20b` is not percent-encoded a second time. When the output is not encoded, the input is not decoded either, since decoding without re-encoding would corrupt it. The `decode` option is ignored: decoding the predicate's view differently from the output would give the two views different keys, so a percent-encoded key, or a `+` in a key, would not find its value. |
| 779 | // An `encode` that is not a boolean is read for its truthiness, the same way `stringify()` reads it, so that `null` turns encoding off. Reading it as nullish instead would decode the query and then write it back unencoded, which puts a decoded `&` or `#` into the URL. `definedOptions()` has already dropped an `encode` that is `undefined`, so that is the only way to mean the default. |
| 780 | const shouldDecode = options.encode === undefined ? true : Boolean(options.encode); |
| 781 | const {url, query} = parseUrl(input, { |
| 782 | ...options, |
| 783 | decode: shouldDecode, |
| 784 | parseNumbers: false, |
| 785 | parseBooleans: false, |
| 786 | types: undefined, |
| 787 | }); |
| 788 | const typedQuery = parse(extract(input), {...options, decode: shouldDecode}); |
| 789 | |
| 790 | // Keep the fragment identifier as-is. Decoding and re-encoding it could change it. |
| 791 | return stringifyUrl({ |
| 792 | url: url + getHash(input), |
| 793 | query: includeKeys(query, key => filter(key, typedQuery[key])), |
| 794 | }, options); |
| 795 | } |
| 796 | |
| 797 | export function exclude(input, filter, options) { |
| 798 | if (Array.isArray(filter)) { |
no test coverage detected