UNPKG

@wordpress/url

Version:
8 lines (7 loc) 4.32 kB
{ "version": 3, "sources": ["../src/get-query-args.ts"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { 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 value\n\t\t\t\t: // Otherwise, advance to the next object in the path, creating\n\t\t\t\t // it if it does not yet exist.\n\t\t\t\t object[ 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\tconst [ key, value = '' ] = keyValue\n\t\t\t\t\t.split( '=' )\n\t\t\t\t\t// Filtering avoids decoding as `undefined` for value, where\n\t\t\t\t\t// default is restored in destructuring assignment.\n\t\t\t\t\t.filter( Boolean )\n\t\t\t\t\t.map( safeDecodeURIComponent );\n\n\t\t\t\tif ( key ) {\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": ";AAGA,SAAS,8BAA8B;AACvC,SAAS,sBAAsB;AAa/B,SAAS,QAAS,QAA+B,MAAgB,OAAa;AAC7E,QAAM,SAAS,KAAK;AACpB,QAAM,YAAY,SAAS;AAC3B,WAAU,IAAI,GAAG,IAAI,QAAQ,KAAM;AAClC,QAAI,MAAM,KAAM,CAAE;AAElB,QAAK,CAAE,OAAO,MAAM,QAAS,MAAO,GAAI;AAGvC,YAAM,OAAO,OAAO,SAAS;AAAA,IAC9B;AAEA,UAAM,CAAE,aAAa,eAAe,WAAY,EAAE,SAAU,GAAI,IAC7D,IAAI,YAAY,IAChB;AAIH,UAAM,sBAAsB,CAAE,MAAO,OAAQ,KAAM,IAAI,CAAE,CAAE,CAAE;AAE7D,WAAQ,GAAI,I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