UNPKG

@wordpress/url

Version:
67 lines (65 loc) 2.61 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/url/src/get-query-args.ts var get_query_args_exports = {}; __export(get_query_args_exports, { getQueryArgs: () => getQueryArgs }); module.exports = __toCommonJS(get_query_args_exports); var import_safe_decode_uri_component = require("./safe-decode-uri-component.cjs"); var import_get_query_string = require("./get-query-string.cjs"); function setPath(object, path, value) { const length = path.length; const lastIndex = length - 1; for (let i = 0; i < length; i++) { let key = path[i]; if (!key && Array.isArray(object)) { key = object.length.toString(); } key = ["__proto__", "constructor", "prototype"].includes(key) ? key.toUpperCase() : key; const isNextKeyArrayIndex = !isNaN(Number(path[i + 1])); object[key] = i === lastIndex ? ( // If at end of path, assign the intended value. value ) : ( // Otherwise, advance to the next object in the path, creating // it if it does not yet exist. object[key] || (isNextKeyArrayIndex ? [] : {}) ); if (Array.isArray(object[key]) && !isNextKeyArrayIndex) { object[key] = { ...object[key] }; } object = object[key]; } } function getQueryArgs(url) { return ((0, import_get_query_string.getQueryString)(url) || "").replace(/\+/g, "%20").split("&").reduce((accumulator, keyValue) => { const [key, value = ""] = keyValue.split("=").filter(Boolean).map(import_safe_decode_uri_component.safeDecodeURIComponent); if (key) { const segments = key.replace(/\]/g, "").split("["); setPath(accumulator, segments, value); } return accumulator; }, /* @__PURE__ */ Object.create(null)); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getQueryArgs }); //# sourceMappingURL=get-query-args.cjs.map