{"version":3,"file":"build-query-string.cjs","names":[],"sources":["../src/build-query-string.ts"],"sourcesContent":["/**\n * Generates URL-encoded query string using input query data.\n *\n * It is intended to behave equivalent as PHP's `http_build_query`, configured\n * with encoding type PHP_QUERY_RFC3986 (spaces as `%20`).\n *\n * @example\n * ```js\n * const queryString = buildQueryString( {\n *    simple: 'is ok',\n *    arrays: [ 'are', 'fine', 'too' ],\n *    objects: {\n *       evenNested: {\n *          ok: 'yes',\n *       },\n *    },\n * } );\n * // \"simple=is%20ok&arrays%5B0%5D=are&arrays%5B1%5D=fine&arrays%5B2%5D=too&objects%5BevenNested%5D%5Bok%5D=yes\"\n * ```\n *\n * @param data Data to encode.\n *\n * @return Query string.\n */\nexport function buildQueryString( data: Record< string, unknown > ): string {\n\tlet string = '';\n\n\tconst stack: Array< [ string, unknown ] > = Object.entries( data );\n\n\tlet pair;\n\twhile ( ( pair = stack.shift() ) ) {\n\t\tlet [ key, value ] = pair;\n\n\t\t// Support building deeply nested data, from array or object values.\n\t\tconst hasNestedData =\n\t\t\tArray.isArray( value ) || ( value && value.constructor === Object );\n\n\t\tif ( hasNestedData ) {\n\t\t\t// Push array or object values onto the stack as composed of their\n\t\t\t// original key and nested index or key, retaining order by a\n\t\t\t// combination of Array#reverse and Array#unshift onto the stack.\n\t\t\tconst valuePairs = Object.entries( value as object ).reverse();\n\t\t\tfor ( const [ member, memberValue ] of valuePairs ) {\n\t\t\t\tstack.unshift( [ `${ key }[${ member }]`, memberValue ] );\n\t\t\t}\n\t\t} else if ( value !== undefined ) {\n\t\t\t// Null is treated as special case, equivalent to empty string.\n\t\t\tif ( value === null ) {\n\t\t\t\tvalue = '';\n\t\t\t}\n\n\t\t\tstring +=\n\t\t\t\t'&' +\n\t\t\t\t[ key, String( value ) ].map( encodeURIComponent ).join( '=' );\n\t\t}\n\t}\n\n\t// Loop will concatenate with leading `&`, but it's only expected for all\n\t// but the first query parameter. This strips the leading `&`, while still\n\t// accounting for the case that the string may in-fact be empty.\n\treturn string.substr( 1 );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,iBAAkB,MAA0C;CAC3E,IAAI,SAAS;CAEb,MAAM,QAAsC,OAAO,QAAS,IAAK;CAEjE,IAAI;CACJ,OAAU,OAAO,MAAM,MAAM,GAAM;EAClC,IAAI,CAAE,KAAK,SAAU;EAMrB,IAFC,MAAM,QAAS,KAAM,KAAO,SAAS,MAAM,gBAAgB,QAEvC;GAIpB,MAAM,aAAa,OAAO,QAAS,KAAgB,CAAC,CAAC,QAAQ;GAC7D,KAAM,MAAM,CAAE,QAAQ,gBAAiB,YACtC,MAAM,QAAS,CAAE,GAAI,IAAK,GAAI,OAAQ,IAAI,WAAY,CAAE;EAE1D,OAAO,IAAK,UAAU,KAAA,GAAY;GAEjC,IAAK,UAAU,MACd,QAAQ;GAGT,UACC,MACA,CAAE,KAAK,OAAQ,KAAM,CAAE,CAAC,CAAC,IAAK,kBAAmB,CAAC,CAAC,KAAM,GAAI;EAC/D;CACD;CAKA,OAAO,OAAO,OAAQ,CAAE;AACzB"}