{"version":3,"file":"get-query-args.mjs","names":[],"sources":["../src/get-query-args.ts"],"sourcesContent":["import { safeDecodeURIComponent } from './safe-decode-uri-component';\nimport { getQueryString } from './get-query-string';\nimport type { QueryArgParsed } from './get-query-arg';\n\ntype QueryArgs = Record< string, QueryArgParsed >;\n\n/**\n * Sets a value in object deeply by a given array of path segments. Mutates the\n * object reference.\n *\n * @param object Object in which to assign.\n * @param path   Path segment at which to set value.\n * @param value  Value to set.\n */\nfunction setPath( object: Record< string, any >, path: string[], value: any ) {\n\tconst length = path.length;\n\tconst lastIndex = length - 1;\n\tfor ( let i = 0; i < length; i++ ) {\n\t\tlet key = path[ i ];\n\n\t\tif ( ! key && Array.isArray( object ) ) {\n\t\t\t// If key is empty string and next value is array, derive key from\n\t\t\t// the current length of the array.\n\t\t\tkey = object.length.toString();\n\t\t}\n\n\t\tkey = [ '__proto__', 'constructor', 'prototype' ].includes( key )\n\t\t\t? key.toUpperCase()\n\t\t\t: key;\n\n\t\t// If the next key in the path is numeric (or empty string), it will be\n\t\t// created as an array. Otherwise, it will be created as an object.\n\t\tconst isNextKeyArrayIndex = ! isNaN( Number( path[ i + 1 ] ) );\n\n\t\tobject[ key ] =\n\t\t\ti === lastIndex\n\t\t\t\t? // If at end of path, assign the intended value.\n\t\t\t\t\tvalue\n\t\t\t\t: // Otherwise, advance to the next object in the path, creating\n\t\t\t\t\t// it if it does not yet exist.\n\t\t\t\t\tobject[ key ] || ( isNextKeyArrayIndex ? [] : {} );\n\n\t\tif ( Array.isArray( object[ key ] ) && ! isNextKeyArrayIndex ) {\n\t\t\t// If we current key is non-numeric, but the next value is an\n\t\t\t// array, coerce the value to an object.\n\t\t\tobject[ key ] = { ...object[ key ] };\n\t\t}\n\n\t\t// Update working reference object to the next in the path.\n\t\tobject = object[ key ];\n\t}\n}\n\n/**\n * Returns an object of query arguments of the given URL. If the given URL is\n * invalid or has no querystring, an empty object is returned.\n *\n * @param url URL.\n *\n * @example\n * ```js\n * const foo = getQueryArgs( 'https://wordpress.org?foo=bar&bar=baz' );\n * // { \"foo\": \"bar\", \"bar\": \"baz\" }\n * ```\n *\n * @return Query args object.\n */\nexport function getQueryArgs( url: string ): QueryArgs {\n\treturn (\n\t\t( getQueryString( url ) || '' )\n\t\t\t// Normalize space encoding, accounting for PHP URL encoding\n\t\t\t// corresponding to `application/x-www-form-urlencoded`.\n\t\t\t//\n\t\t\t// See: https://tools.ietf.org/html/rfc1866#section-8.2.1\n\t\t\t.replace( /\\+/g, '%20' )\n\t\t\t.split( '&' )\n\t\t\t.reduce( ( accumulator, keyValue ) => {\n\t\t\t\t// Only the first `=` separates the key from the value. Any\n\t\t\t\t// further occurrences are part of the value itself, as in\n\t\t\t\t// base64 padding or a nested URL.\n\t\t\t\tconst separatorIndex = keyValue.indexOf( '=' );\n\t\t\t\tconst hasValue = separatorIndex !== -1;\n\t\t\t\tconst key = safeDecodeURIComponent(\n\t\t\t\t\thasValue ? keyValue.slice( 0, separatorIndex ) : keyValue\n\t\t\t\t);\n\n\t\t\t\t// A pair without a key, such as `=value`, is malformed and has\n\t\t\t\t// nothing to assign to.\n\t\t\t\tif ( key ) {\n\t\t\t\t\tconst value = hasValue\n\t\t\t\t\t\t? safeDecodeURIComponent(\n\t\t\t\t\t\t\t\tkeyValue.slice( separatorIndex + 1 )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t: '';\n\t\t\t\t\tconst segments = key.replace( /\\]/g, '' ).split( '[' );\n\t\t\t\t\tsetPath( accumulator, segments, value );\n\t\t\t\t}\n\n\t\t\t\treturn accumulator;\n\t\t\t}, Object.create( null ) )\n\t);\n}\n"],"mappings":";;;;;;;;;;;AAcA,SAAS,QAAS,QAA+B,MAAgB,OAAa;CAC7E,MAAM,SAAS,KAAK;CACpB,MAAM,YAAY,SAAS;CAC3B,KAAM,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAM;EAClC,IAAI,MAAM,KAAM;EAEhB,IAAK,CAAE,OAAO,MAAM,QAAS,MAAO,GAGnC,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