UNPKG

@wordpress/url

Version:
8 lines (7 loc) 1.72 kB
{ "version": 3, "sources": ["../src/normalize-path.ts"], "sourcesContent": ["/**\n * Given a path, returns a normalized path where equal query parameter values\n * will be treated as identical, regardless of order they appear in the original\n * text.\n *\n * @param path Original path.\n *\n * @return Normalized path.\n */\nexport function normalizePath( path: string ): string {\n\tconst split = path.split( '?' );\n\tconst query = split[ 1 ];\n\tconst base = split[ 0 ];\n\tif ( ! query ) {\n\t\treturn base;\n\t}\n\n\t// 'b=1%2C2&c=2&a=5'\n\treturn (\n\t\tbase +\n\t\t'?' +\n\t\tquery\n\t\t\t// [ 'b=1%2C2', 'c=2', 'a=5' ]\n\t\t\t.split( '&' )\n\t\t\t// [ [ 'b, '1%2C2' ], [ 'c', '2' ], [ 'a', '5' ] ]\n\t\t\t.map( ( entry ) => entry.split( '=' ) )\n\t\t\t// [ [ 'b', '1,2' ], [ 'c', '2' ], [ 'a', '5' ] ]\n\t\t\t.map( ( pair ) => pair.map( decodeURIComponent ) )\n\t\t\t// [ [ 'a', '5' ], [ 'b, '1,2' ], [ 'c', '2' ] ]\n\t\t\t.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )\n\t\t\t// [ [ 'a', '5' ], [ 'b, '1%2C2' ], [ 'c', '2' ] ]\n\t\t\t.map( ( pair ) => pair.map( encodeURIComponent ) )\n\t\t\t// [ 'a=5', 'b=1%2C2', 'c=2' ]\n\t\t\t.map( ( pair ) => pair.join( '=' ) )\n\t\t\t// 'a=5&b=1%2C2&c=2'\n\t\t\t.join( '&' )\n\t);\n}\n"], "mappings": ";AASO,SAAS,cAAe,MAAuB;AACrD,QAAM,QAAQ,KAAK,MAAO,GAAI;AAC9B,QAAM,QAAQ,MAAO,CAAE;AACvB,QAAM,OAAO,MAAO,CAAE;AACtB,MAAK,CAAE,OAAQ;AACd,WAAO;AAAA,EACR;AAGA,SACC,OACA,MACA,MAEE,MAAO,GAAI,EAEX,IAAK,CAAE,UAAW,MAAM,MAAO,GAAI,CAAE,EAErC,IAAK,CAAE,SAAU,KAAK,IAAK,kBAAmB,CAAE,EAEhD,KAAM,CAAE,GAAG,MAAO,EAAG,CAAE,EAAE,cAAe,EAAG,CAAE,CAAE,CAAE,EAEjD,IAAK,CAAE,SAAU,KAAK,IAAK,kBAAmB,CAAE,EAEhD,IAAK,CAAE,SAAU,KAAK,KAAM,GAAI,CAAE,EAElC,KAAM,GAAI;AAEd;", "names": [] }