consul/ui/packages/consul-ui/app/utils/http/create-query-params.js
John Cowen 2e4c9f5330
ui: Support Route optional parameters/segments (#10212)
Moves our URLs with 'optional namespace segment' into a separately abstracted 'optional URL segment' feature
2021-05-26 17:43:46 +01:00

28 lines
893 B
JavaScript

export default function(encode = encodeURIComponent) {
return function stringify(obj, parent) {
return Object.entries(obj)
.reduce(function(prev, [key, value], i) {
// if the value is undefined do nothing
if (typeof value === 'undefined') {
return prev;
}
let prop = encode(key);
// if we have a parent, prefix the property with that
if (typeof parent !== 'undefined') {
prop = `${parent}[${prop}]`;
}
// if the value is null just print the prop
if (value === null) {
return prev.concat(prop);
}
// anything nested, recur
if (typeof value === 'object') {
return prev.concat(stringify(value, prop));
}
// anything else print prop=value
return prev.concat(`${prop}=${encode(value)}`);
}, [])
.join('&');
};
}