123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- 'use strict';
- const strictUriEncode = require('strict-uri-encode');
- const decodeComponent = require('decode-uri-component');
- const splitOnFirst = require('split-on-first');
- const filterObject = require('filter-obj');
- const isNullOrUndefined = value => value === null || value === undefined;
- function encoderForArrayFormat(options) {
- switch (options.arrayFormat) {
- case 'index':
- return key => (result, value) => {
- const index = result.length;
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
- if (value === null) {
- return [...result, [encode(key, options), '[', index, ']'].join('')];
- }
- return [
- ...result,
- [encode(key, options), '[', encode(index, options), ']=', encode(value, options)].join('')
- ];
- };
- case 'bracket':
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
- if (value === null) {
- return [...result, [encode(key, options), '[]'].join('')];
- }
- return [...result, [encode(key, options), '[]=', encode(value, options)].join('')];
- };
- case 'comma':
- case 'separator':
- return key => (result, value) => {
- if (value === null || value === undefined || value.length === 0) {
- return result;
- }
- if (result.length === 0) {
- return [[encode(key, options), '=', encode(value, options)].join('')];
- }
- return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
- };
- default:
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
- if (value === null) {
- return [...result, encode(key, options)];
- }
- return [...result, [encode(key, options), '=', encode(value, options)].join('')];
- };
- }
- }
- function parserForArrayFormat(options) {
- let result;
- switch (options.arrayFormat) {
- case 'index':
- return (key, value, accumulator) => {
- result = /\[(\d*)\]$/.exec(key);
- key = key.replace(/\[\d*\]$/, '');
- if (!result) {
- accumulator[key] = value;
- return;
- }
- if (accumulator[key] === undefined) {
- accumulator[key] = {};
- }
- accumulator[key][result[1]] = value;
- };
- case 'bracket':
- return (key, value, accumulator) => {
- result = /(\[\])$/.exec(key);
- key = key.replace(/\[\]$/, '');
- if (!result) {
- accumulator[key] = value;
- return;
- }
- if (accumulator[key] === undefined) {
- accumulator[key] = [value];
- return;
- }
- accumulator[key] = [].concat(accumulator[key], value);
- };
- case 'comma':
- case 'separator':
- return (key, value, accumulator) => {
- const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
- const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
- value = isEncodedArray ? decode(value, options) : value;
- const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
- accumulator[key] = newValue;
- };
- default:
- return (key, value, accumulator) => {
- if (accumulator[key] === undefined) {
- accumulator[key] = value;
- return;
- }
- accumulator[key] = [].concat(accumulator[key], value);
- };
- }
- }
- function validateArrayFormatSeparator(value) {
- if (typeof value !== 'string' || value.length !== 1) {
- throw new TypeError('arrayFormatSeparator must be single character string');
- }
- }
- function encode(value, options) {
- if (options.encode) {
- return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
- }
- return value;
- }
- function decode(value, options) {
- if (options.decode) {
- return decodeComponent(value);
- }
- return value;
- }
- function keysSorter(input) {
- if (Array.isArray(input)) {
- return input.sort();
- }
- if (typeof input === 'object') {
- return keysSorter(Object.keys(input))
- .sort((a, b) => Number(a) - Number(b))
- .map(key => input[key]);
- }
- return input;
- }
- function removeHash(input) {
- const hashStart = input.indexOf('#');
- if (hashStart !== -1) {
- input = input.slice(0, hashStart);
- }
- return input;
- }
- function getHash(url) {
- let hash = '';
- const hashStart = url.indexOf('#');
- if (hashStart !== -1) {
- hash = url.slice(hashStart);
- }
- return hash;
- }
- function extract(input) {
- input = removeHash(input);
- const queryStart = input.indexOf('?');
- if (queryStart === -1) {
- return '';
- }
- return input.slice(queryStart + 1);
- }
- function parseValue(value, options) {
- if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
- value = Number(value);
- } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
- value = value.toLowerCase() === 'true';
- }
- return value;
- }
- function parse(query, options) {
- options = Object.assign({
- decode: true,
- sort: true,
- arrayFormat: 'none',
- arrayFormatSeparator: ',',
- parseNumbers: false,
- parseBooleans: false
- }, options);
- validateArrayFormatSeparator(options.arrayFormatSeparator);
- const formatter = parserForArrayFormat(options);
- // Create an object with no prototype
- const ret = Object.create(null);
- if (typeof query !== 'string') {
- return ret;
- }
- query = query.trim().replace(/^[?#&]/, '');
- if (!query) {
- return ret;
- }
- for (const param of query.split('&')) {
- if (param === '') {
- continue;
- }
- let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
- // Missing `=` should be `null`:
- // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
- value = value === undefined ? null : ['comma', 'separator'].includes(options.arrayFormat) ? value : decode(value, options);
- formatter(decode(key, options), value, ret);
- }
- for (const key of Object.keys(ret)) {
- const value = ret[key];
- if (typeof value === 'object' && value !== null) {
- for (const k of Object.keys(value)) {
- value[k] = parseValue(value[k], options);
- }
- } else {
- ret[key] = parseValue(value, options);
- }
- }
- if (options.sort === false) {
- return ret;
- }
- return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
- const value = ret[key];
- if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
- // Sort object keys, not values
- result[key] = keysSorter(value);
- } else {
- result[key] = value;
- }
- return result;
- }, Object.create(null));
- }
- exports.extract = extract;
- exports.parse = parse;
- exports.stringify = (object, options) => {
- if (!object) {
- return '';
- }
- options = Object.assign({
- encode: true,
- strict: true,
- arrayFormat: 'none',
- arrayFormatSeparator: ','
- }, options);
- validateArrayFormatSeparator(options.arrayFormatSeparator);
- const shouldFilter = key => (
- (options.skipNull && isNullOrUndefined(object[key])) ||
- (options.skipEmptyString && object[key] === '')
- );
- const formatter = encoderForArrayFormat(options);
- const objectCopy = {};
- for (const key of Object.keys(object)) {
- if (!shouldFilter(key)) {
- objectCopy[key] = object[key];
- }
- }
- const keys = Object.keys(objectCopy);
- if (options.sort !== false) {
- keys.sort(options.sort);
- }
- return keys.map(key => {
- const value = object[key];
- if (value === undefined) {
- return '';
- }
- if (value === null) {
- return encode(key, options);
- }
- if (Array.isArray(value)) {
- return value
- .reduce(formatter(key), [])
- .join('&');
- }
- return encode(key, options) + '=' + encode(value, options);
- }).filter(x => x.length > 0).join('&');
- };
- exports.parseUrl = (url, options) => {
- options = Object.assign({
- decode: true
- }, options);
- const [url_, hash] = splitOnFirst(url, '#');
- return Object.assign(
- {
- url: url_.split('?')[0] || '',
- query: parse(extract(url), options)
- },
- options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}
- );
- };
- exports.stringifyUrl = (object, options) => {
- options = Object.assign({
- encode: true,
- strict: true
- }, options);
- const url = removeHash(object.url).split('?')[0] || '';
- const queryFromUrl = exports.extract(object.url);
- const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false});
- const query = Object.assign(parsedQueryFromUrl, object.query);
- let queryString = exports.stringify(query, options);
- if (queryString) {
- queryString = `?${queryString}`;
- }
- let hash = getHash(object.url);
- if (object.fragmentIdentifier) {
- hash = `#${encode(object.fragmentIdentifier, options)}`;
- }
- return `${url}${queryString}${hash}`;
- };
- exports.pick = (input, filter, options) => {
- options = Object.assign({
- parseFragmentIdentifier: true
- }, options);
- const {url, query, fragmentIdentifier} = exports.parseUrl(input, options);
- return exports.stringifyUrl({
- url,
- query: filterObject(query, filter),
- fragmentIdentifier
- }, options);
- };
- exports.exclude = (input, filter, options) => {
- const exclusionFilter = Array.isArray(filter) ? key => !filter.includes(key) : (key, value) => !filter(key, value);
- return exports.pick(input, exclusionFilter, options);
- };
|