@wordpress/url 3.2.2-next.5df0cd52b7.0 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +34 -1
- package/build/add-query-args.js +4 -1
- package/build/add-query-args.js.map +1 -1
- package/build/filter-url-for-display.js +2 -1
- package/build/filter-url-for-display.js.map +1 -1
- package/build/get-filename.js +32 -0
- package/build/get-filename.js.map +1 -0
- package/build/get-query-args.js +2 -2
- package/build/get-query-args.js.map +1 -1
- package/build/index.js +60 -44
- package/build/index.js.map +1 -1
- package/build/normalize-path.js +34 -0
- package/build/normalize-path.js.map +1 -0
- package/build/remove-query-args.js +6 -1
- package/build/remove-query-args.js.map +1 -1
- package/build-module/add-query-args.js +4 -1
- package/build-module/add-query-args.js.map +1 -1
- package/build-module/filter-url-for-display.js +2 -1
- package/build-module/filter-url-for-display.js.map +1 -1
- package/build-module/get-filename.js +25 -0
- package/build-module/get-filename.js.map +1 -0
- package/build-module/get-query-args.js +2 -2
- package/build-module/get-query-args.js.map +1 -1
- package/build-module/index.js +2 -0
- package/build-module/index.js.map +1 -1
- package/build-module/normalize-path.js +27 -0
- package/build-module/normalize-path.js.map +1 -0
- package/build-module/remove-query-args.js +6 -1
- package/build-module/remove-query-args.js.map +1 -1
- package/build-types/get-filename.d.ts +15 -0
- package/build-types/get-filename.d.ts.map +1 -0
- package/build-types/get-query-arg.d.ts.map +1 -1
- package/build-types/get-query-args.d.ts +2 -4
- package/build-types/get-query-args.d.ts.map +1 -1
- package/build-types/index.d.ts +2 -0
- package/build-types/normalize-path.d.ts +11 -0
- package/build-types/normalize-path.d.ts.map +1 -0
- package/package.json +4 -5
- package/src/get-filename.js +25 -0
- package/src/index.js +2 -0
- package/src/normalize-path.js +34 -0
- package/src/test/index.js +62 -0
- package/tsconfig.tsbuildinfo +1 -953
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/url/src/get-query-args.js"],"names":["getQueryString","setPath","object","path","value","length","lastIndex","i","key","Array","isArray","toString","isNextKeyArrayIndex","isNaN","Number","getQueryArgs","url","replace","split","reduce","accumulator","keyValue","filter","Boolean","map","decodeURIComponent","segments"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAT,QAA+B,oBAA/B;AAEA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,OAAT,CAAkBC,MAAlB,EAA0BC,IAA1B,EAAgCC,KAAhC,EAAwC;AACvC,QAAMC,MAAM,GAAGF,IAAI,CAACE,MAApB;AACA,QAAMC,SAAS,GAAGD,MAAM,GAAG,CAA3B;;AACA,OAAM,IAAIE,CAAC,GAAG,CAAd,EAAiBA,CAAC,GAAGF,MAArB,EAA6BE,CAAC,EAA9B,EAAmC;AAClC,QAAIC,GAAG,GAAGL,IAAI,CAAEI,CAAF,CAAd;;AAEA,QAAK,CAAEC,GAAF,IAASC,KAAK,CAACC,OAAN,CAAeR,MAAf,CAAd,EAAwC;AACvC;AACA;AACAM,MAAAA,GAAG,GAAGN,MAAM,CAACG,MAAP,CAAcM,QAAd,EAAN;AACA,KAPiC,CASlC;AACA;;;AACA,UAAMC,mBAAmB,GAAG,CAAEC,KAAK,CAAEC,MAAM,CAAEX,IAAI,CAAEI,CAAC,GAAG,CAAN,CAAN,CAAR,CAAnC;AAEAL,IAAAA,MAAM,CAAEM,GAAF,CAAN,GACCD,CAAC,KAAKD,SAAN,GACG;AACAF,IAAAA,KAFH,GAGG;AACA;AACAF,IAAAA,MAAM,CAAEM,GAAF,CAAN,KAAmBI,mBAAmB,GAAG,EAAH,GAAQ,EAA9C,CANJ;;AAQA,QAAKH,KAAK,CAACC,OAAN,CAAeR,MAAM,CAAEM,GAAF,CAArB,KAAkC,CAAEI,mBAAzC,EAA+D;AAC9D;AACA;AACAV,MAAAA,MAAM,CAAEM,GAAF,CAAN,GAAgB,EAAE,GAAGN,MAAM,CAAEM,GAAF;AAAX,OAAhB;AACA,KAzBiC,CA2BlC;;;AACAN,IAAAA,MAAM,GAAGA,MAAM,CAAEM,GAAF,CAAf;AACA;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASO,YAAT,CAAuBC,GAAvB,EAA6B;AACnC,SACC,CAAEhB,cAAc,CAAEgB,GAAF,CAAd,IAAyB,EAA3B,
|
|
1
|
+
{"version":3,"sources":["@wordpress/url/src/get-query-args.js"],"names":["getQueryString","setPath","object","path","value","length","lastIndex","i","key","Array","isArray","toString","isNextKeyArrayIndex","isNaN","Number","getQueryArgs","url","replace","split","reduce","accumulator","keyValue","filter","Boolean","map","decodeURIComponent","segments"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAT,QAA+B,oBAA/B;AAEA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,OAAT,CAAkBC,MAAlB,EAA0BC,IAA1B,EAAgCC,KAAhC,EAAwC;AACvC,QAAMC,MAAM,GAAGF,IAAI,CAACE,MAApB;AACA,QAAMC,SAAS,GAAGD,MAAM,GAAG,CAA3B;;AACA,OAAM,IAAIE,CAAC,GAAG,CAAd,EAAiBA,CAAC,GAAGF,MAArB,EAA6BE,CAAC,EAA9B,EAAmC;AAClC,QAAIC,GAAG,GAAGL,IAAI,CAAEI,CAAF,CAAd;;AAEA,QAAK,CAAEC,GAAF,IAASC,KAAK,CAACC,OAAN,CAAeR,MAAf,CAAd,EAAwC;AACvC;AACA;AACAM,MAAAA,GAAG,GAAGN,MAAM,CAACG,MAAP,CAAcM,QAAd,EAAN;AACA,KAPiC,CASlC;AACA;;;AACA,UAAMC,mBAAmB,GAAG,CAAEC,KAAK,CAAEC,MAAM,CAAEX,IAAI,CAAEI,CAAC,GAAG,CAAN,CAAN,CAAR,CAAnC;AAEAL,IAAAA,MAAM,CAAEM,GAAF,CAAN,GACCD,CAAC,KAAKD,SAAN,GACG;AACAF,IAAAA,KAFH,GAGG;AACA;AACAF,IAAAA,MAAM,CAAEM,GAAF,CAAN,KAAmBI,mBAAmB,GAAG,EAAH,GAAQ,EAA9C,CANJ;;AAQA,QAAKH,KAAK,CAACC,OAAN,CAAeR,MAAM,CAAEM,GAAF,CAArB,KAAkC,CAAEI,mBAAzC,EAA+D;AAC9D;AACA;AACAV,MAAAA,MAAM,CAAEM,GAAF,CAAN,GAAgB,EAAE,GAAGN,MAAM,CAAEM,GAAF;AAAX,OAAhB;AACA,KAzBiC,CA2BlC;;;AACAN,IAAAA,MAAM,GAAGA,MAAM,CAAEM,GAAF,CAAf;AACA;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASO,YAAT,CAAuBC,GAAvB,EAA6B;AACnC,SACC,CAAEhB,cAAc,CAAEgB,GAAF,CAAd,IAAyB,EAA3B,CACC;AACA;AACA;AACA;AAJD,IAKEC,OALF,CAKW,KALX,EAKkB,KALlB,EAMEC,KANF,CAMS,GANT,EAOEC,MAPF,CAOU,CAAEC,WAAF,EAAeC,QAAf,KAA6B;AACrC,UAAM,CAAEb,GAAF,EAAOJ,KAAK,GAAG,EAAf,IAAsBiB,QAAQ,CAClCH,KAD0B,CACnB,GADmB,EAE3B;AACA;AAH2B,KAI1BI,MAJ0B,CAIlBC,OAJkB,EAK1BC,GAL0B,CAKrBC,kBALqB,CAA5B;;AAOA,QAAKjB,GAAL,EAAW;AACV,YAAMkB,QAAQ,GAAGlB,GAAG,CAACS,OAAJ,CAAa,KAAb,EAAoB,EAApB,EAAyBC,KAAzB,CAAgC,GAAhC,CAAjB;AACAjB,MAAAA,OAAO,CAAEmB,WAAF,EAAeM,QAAf,EAAyBtB,KAAzB,CAAP;AACA;;AAED,WAAOgB,WAAP;AACA,GArBF,EAqBI,EArBJ,CADD;AAwBA","sourcesContent":["/**\n * Internal dependencies\n */\nimport { getQueryString } from './get-query-string';\n\n/** @typedef {import('./get-query-arg').QueryArgParsed} QueryArgParsed */\n\n/**\n * @typedef {Record<string,QueryArgParsed>} QueryArgs\n */\n\n/**\n * Sets a value in object deeply by a given array of path segments. Mutates the\n * object reference.\n *\n * @param {Record<string,*>} object Object in which to assign.\n * @param {string[]} path Path segment at which to set value.\n * @param {*} value Value to set.\n */\nfunction setPath( object, path, value ) {\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\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 {string} 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 {QueryArgs} Query args object.\n */\nexport function getQueryArgs( url ) {\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( decodeURIComponent );\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}, {} )\n\t);\n}\n"]}
|
package/build-module/index.js
CHANGED
|
@@ -22,4 +22,6 @@ export { safeDecodeURI } from './safe-decode-uri';
|
|
|
22
22
|
export { safeDecodeURIComponent } from './safe-decode-uri-component';
|
|
23
23
|
export { filterURLForDisplay } from './filter-url-for-display';
|
|
24
24
|
export { cleanForSlug } from './clean-for-slug';
|
|
25
|
+
export { getFilename } from './get-filename';
|
|
26
|
+
export { normalizePath } from './normalize-path';
|
|
25
27
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/url/src/index.js"],"names":["isURL","isEmail","getProtocol","isValidProtocol","getAuthority","isValidAuthority","getPath","isValidPath","getQueryString","buildQueryString","isValidQueryString","getPathAndQueryString","getFragment","isValidFragment","addQueryArgs","getQueryArg","getQueryArgs","hasQueryArg","removeQueryArgs","prependHTTP","safeDecodeURI","safeDecodeURIComponent","filterURLForDisplay","cleanForSlug"],"mappings":"AAAA,SAASA,KAAT,QAAsB,UAAtB;AACA,SAASC,OAAT,QAAwB,YAAxB;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,YAAT,QAA6B,iBAA7B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AACA,SAASC,OAAT,QAAwB,YAAxB;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,cAAT,QAA+B,oBAA/B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AACA,SAASC,kBAAT,QAAmC,yBAAnC;AACA,SAASC,qBAAT,QAAsC,6BAAtC;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,YAAT,QAA6B,kBAA7B;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,YAAT,QAA6B,kBAA7B;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,aAAT,QAA8B,mBAA9B;AACA,SAASC,sBAAT,QAAuC,6BAAvC;AACA,SAASC,mBAAT,QAAoC,0BAApC;AACA,SAASC,YAAT,QAA6B,kBAA7B","sourcesContent":["export { isURL } from './is-url';\nexport { isEmail } from './is-email';\nexport { getProtocol } from './get-protocol';\nexport { isValidProtocol } from './is-valid-protocol';\nexport { getAuthority } from './get-authority';\nexport { isValidAuthority } from './is-valid-authority';\nexport { getPath } from './get-path';\nexport { isValidPath } from './is-valid-path';\nexport { getQueryString } from './get-query-string';\nexport { buildQueryString } from './build-query-string';\nexport { isValidQueryString } from './is-valid-query-string';\nexport { getPathAndQueryString } from './get-path-and-query-string';\nexport { getFragment } from './get-fragment';\nexport { isValidFragment } from './is-valid-fragment';\nexport { addQueryArgs } from './add-query-args';\nexport { getQueryArg } from './get-query-arg';\nexport { getQueryArgs } from './get-query-args';\nexport { hasQueryArg } from './has-query-arg';\nexport { removeQueryArgs } from './remove-query-args';\nexport { prependHTTP } from './prepend-http';\nexport { safeDecodeURI } from './safe-decode-uri';\nexport { safeDecodeURIComponent } from './safe-decode-uri-component';\nexport { filterURLForDisplay } from './filter-url-for-display';\nexport { cleanForSlug } from './clean-for-slug';\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/url/src/index.js"],"names":["isURL","isEmail","getProtocol","isValidProtocol","getAuthority","isValidAuthority","getPath","isValidPath","getQueryString","buildQueryString","isValidQueryString","getPathAndQueryString","getFragment","isValidFragment","addQueryArgs","getQueryArg","getQueryArgs","hasQueryArg","removeQueryArgs","prependHTTP","safeDecodeURI","safeDecodeURIComponent","filterURLForDisplay","cleanForSlug","getFilename","normalizePath"],"mappings":"AAAA,SAASA,KAAT,QAAsB,UAAtB;AACA,SAASC,OAAT,QAAwB,YAAxB;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,YAAT,QAA6B,iBAA7B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AACA,SAASC,OAAT,QAAwB,YAAxB;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,cAAT,QAA+B,oBAA/B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AACA,SAASC,kBAAT,QAAmC,yBAAnC;AACA,SAASC,qBAAT,QAAsC,6BAAtC;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,YAAT,QAA6B,kBAA7B;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,YAAT,QAA6B,kBAA7B;AACA,SAASC,WAAT,QAA4B,iBAA5B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,aAAT,QAA8B,mBAA9B;AACA,SAASC,sBAAT,QAAuC,6BAAvC;AACA,SAASC,mBAAT,QAAoC,0BAApC;AACA,SAASC,YAAT,QAA6B,kBAA7B;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,aAAT,QAA8B,kBAA9B","sourcesContent":["export { isURL } from './is-url';\nexport { isEmail } from './is-email';\nexport { getProtocol } from './get-protocol';\nexport { isValidProtocol } from './is-valid-protocol';\nexport { getAuthority } from './get-authority';\nexport { isValidAuthority } from './is-valid-authority';\nexport { getPath } from './get-path';\nexport { isValidPath } from './is-valid-path';\nexport { getQueryString } from './get-query-string';\nexport { buildQueryString } from './build-query-string';\nexport { isValidQueryString } from './is-valid-query-string';\nexport { getPathAndQueryString } from './get-path-and-query-string';\nexport { getFragment } from './get-fragment';\nexport { isValidFragment } from './is-valid-fragment';\nexport { addQueryArgs } from './add-query-args';\nexport { getQueryArg } from './get-query-arg';\nexport { getQueryArgs } from './get-query-args';\nexport { hasQueryArg } from './has-query-arg';\nexport { removeQueryArgs } from './remove-query-args';\nexport { prependHTTP } from './prepend-http';\nexport { safeDecodeURI } from './safe-decode-uri';\nexport { safeDecodeURIComponent } from './safe-decode-uri-component';\nexport { filterURLForDisplay } from './filter-url-for-display';\nexport { cleanForSlug } from './clean-for-slug';\nexport { getFilename } from './get-filename';\nexport { normalizePath } from './normalize-path';\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a path, returns a normalized path where equal query parameter values
|
|
3
|
+
* will be treated as identical, regardless of order they appear in the original
|
|
4
|
+
* text.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} path Original path.
|
|
7
|
+
*
|
|
8
|
+
* @return {string} Normalized path.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizePath(path) {
|
|
11
|
+
const splitted = path.split('?');
|
|
12
|
+
const query = splitted[1];
|
|
13
|
+
const base = splitted[0];
|
|
14
|
+
|
|
15
|
+
if (!query) {
|
|
16
|
+
return base;
|
|
17
|
+
} // 'b=1&c=2&a=5'
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
return base + '?' + query // [ 'b=1', 'c=2', 'a=5' ]
|
|
21
|
+
.split('&') // [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
|
|
22
|
+
.map(entry => entry.split('=')) // [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
|
|
23
|
+
.sort((a, b) => a[0].localeCompare(b[0])) // [ 'a=5', 'b=1', 'c=2' ]
|
|
24
|
+
.map(pair => pair.join('=')) // 'a=5&b=1&c=2'
|
|
25
|
+
.join('&');
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=normalize-path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/url/src/normalize-path.js"],"names":["normalizePath","path","splitted","split","query","base","map","entry","sort","a","b","localeCompare","pair","join"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAT,CAAwBC,IAAxB,EAA+B;AACrC,QAAMC,QAAQ,GAAGD,IAAI,CAACE,KAAL,CAAY,GAAZ,CAAjB;AACA,QAAMC,KAAK,GAAGF,QAAQ,CAAE,CAAF,CAAtB;AACA,QAAMG,IAAI,GAAGH,QAAQ,CAAE,CAAF,CAArB;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAOC,IAAP;AACA,GANoC,CAQrC;;;AACA,SACCA,IAAI,GACJ,GADA,GAEAD,KAAK,CACJ;AADI,GAEHD,KAFF,CAES,GAFT,EAGC;AAHD,GAIEG,GAJF,CAISC,KAAF,IAAaA,KAAK,CAACJ,KAAN,CAAa,GAAb,CAJpB,EAKC;AALD,GAMEK,IANF,CAMQ,CAAEC,CAAF,EAAKC,CAAL,KAAYD,CAAC,CAAE,CAAF,CAAD,CAAOE,aAAP,CAAsBD,CAAC,CAAE,CAAF,CAAvB,CANpB,EAOC;AAPD,GAQEJ,GARF,CAQSM,IAAF,IAAYA,IAAI,CAACC,IAAL,CAAW,GAAX,CARnB,EASC;AATD,GAUEA,IAVF,CAUQ,GAVR,CAHD;AAeA","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 {string} path Original path.\n *\n * @return {string} Normalized path.\n */\nexport function normalizePath( path ) {\n\tconst splitted = path.split( '?' );\n\tconst query = splitted[ 1 ];\n\tconst base = splitted[ 0 ];\n\tif ( ! query ) {\n\t\treturn base;\n\t}\n\n\t// 'b=1&c=2&a=5'\n\treturn (\n\t\tbase +\n\t\t'?' +\n\t\tquery\n\t\t\t// [ 'b=1', 'c=2', 'a=5' ]\n\t\t\t.split( '&' )\n\t\t\t// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]\n\t\t\t.map( ( entry ) => entry.split( '=' ) )\n\t\t\t// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]\n\t\t\t.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )\n\t\t\t// [ 'a=5', 'b=1', 'c=2' ]\n\t\t\t.map( ( pair ) => pair.join( '=' ) )\n\t\t\t// 'a=5&b=1&c=2'\n\t\t\t.join( '&' )\n\t);\n}\n"]}
|
|
@@ -17,7 +17,7 @@ import { buildQueryString } from './build-query-string';
|
|
|
17
17
|
* @return {string} Updated URL.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
export function removeQueryArgs(url
|
|
20
|
+
export function removeQueryArgs(url) {
|
|
21
21
|
const queryStringIndex = url.indexOf('?');
|
|
22
22
|
|
|
23
23
|
if (queryStringIndex === -1) {
|
|
@@ -26,6 +26,11 @@ export function removeQueryArgs(url, ...args) {
|
|
|
26
26
|
|
|
27
27
|
const query = getQueryArgs(url);
|
|
28
28
|
const baseURL = url.substr(0, queryStringIndex);
|
|
29
|
+
|
|
30
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
31
|
+
args[_key - 1] = arguments[_key];
|
|
32
|
+
}
|
|
33
|
+
|
|
29
34
|
args.forEach(arg => delete query[arg]);
|
|
30
35
|
const queryString = buildQueryString(query);
|
|
31
36
|
return queryString ? baseURL + '?' + queryString : baseURL;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/url/src/remove-query-args.js"],"names":["getQueryArgs","buildQueryString","removeQueryArgs","url","
|
|
1
|
+
{"version":3,"sources":["@wordpress/url/src/remove-query-args.js"],"names":["getQueryArgs","buildQueryString","removeQueryArgs","url","queryStringIndex","indexOf","query","baseURL","substr","args","forEach","arg","queryString"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,QAA6B,kBAA7B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,eAAT,CAA0BC,GAA1B,EAAyC;AAC/C,QAAMC,gBAAgB,GAAGD,GAAG,CAACE,OAAJ,CAAa,GAAb,CAAzB;;AACA,MAAKD,gBAAgB,KAAK,CAAC,CAA3B,EAA+B;AAC9B,WAAOD,GAAP;AACA;;AAED,QAAMG,KAAK,GAAGN,YAAY,CAAEG,GAAF,CAA1B;AACA,QAAMI,OAAO,GAAGJ,GAAG,CAACK,MAAJ,CAAY,CAAZ,EAAeJ,gBAAf,CAAhB;;AAP+C,oCAAPK,IAAO;AAAPA,IAAAA,IAAO;AAAA;;AAQ/CA,EAAAA,IAAI,CAACC,OAAL,CAAgBC,GAAF,IAAW,OAAOL,KAAK,CAAEK,GAAF,CAArC;AACA,QAAMC,WAAW,GAAGX,gBAAgB,CAAEK,KAAF,CAApC;AACA,SAAOM,WAAW,GAAGL,OAAO,GAAG,GAAV,GAAgBK,WAAnB,GAAiCL,OAAnD;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport { getQueryArgs } from './get-query-args';\nimport { buildQueryString } from './build-query-string';\n\n/**\n * Removes arguments from the query string of the url\n *\n * @param {string} url URL.\n * @param {...string} args Query Args.\n *\n * @example\n * ```js\n * const newUrl = removeQueryArgs( 'https://wordpress.org?foo=bar&bar=baz&baz=foobar', 'foo', 'bar' ); // https://wordpress.org?baz=foobar\n * ```\n *\n * @return {string} Updated URL.\n */\nexport function removeQueryArgs( url, ...args ) {\n\tconst queryStringIndex = url.indexOf( '?' );\n\tif ( queryStringIndex === -1 ) {\n\t\treturn url;\n\t}\n\n\tconst query = getQueryArgs( url );\n\tconst baseURL = url.substr( 0, queryStringIndex );\n\targs.forEach( ( arg ) => delete query[ arg ] );\n\tconst queryString = buildQueryString( query );\n\treturn queryString ? baseURL + '?' + queryString : baseURL;\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the filename part of the URL.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} url The full URL.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* const filename1 = getFilename( 'http://localhost:8080/this/is/a/test.jpg' ); // 'test.jpg'
|
|
9
|
+
* const filename2 = getFilename( '/this/is/a/test.png' ); // 'test.png'
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @return {string|void} The filename part of the URL.
|
|
13
|
+
*/
|
|
14
|
+
export function getFilename(url: string): string | void;
|
|
15
|
+
//# sourceMappingURL=get-filename.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-filename.d.ts","sourceRoot":"","sources":["../src/get-filename.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,iCAVW,MAAM,GAQL,MAAM,GAAC,IAAI,CAatB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-query-arg.d.ts","sourceRoot":"","sources":["../src/get-query-arg.js"],"names":[],"mappings":"AAKA;;GAEG;AAEH;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,iCAVW,MAAM,OACN,MAAM,GAOL,cAAc,GAAC,IAAI,CAI9B"}
|
|
1
|
+
{"version":3,"file":"get-query-arg.d.ts","sourceRoot":"","sources":["../src/get-query-arg.js"],"names":[],"mappings":"AAKA;;GAEG;AAEH;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,iCAVW,MAAM,OACN,MAAM,GAOL,cAAc,GAAC,IAAI,CAI9B;;;;6BAlBY,MAAM,GAAC,MAAM,EAAE,GAAC,cAAc"}
|
|
@@ -13,8 +13,6 @@
|
|
|
13
13
|
* @return {QueryArgs} Query args object.
|
|
14
14
|
*/
|
|
15
15
|
export function getQueryArgs(url: string): QueryArgs;
|
|
16
|
-
export type QueryArgParsed =
|
|
17
|
-
export type QueryArgs =
|
|
18
|
-
[x: string]: import("./get-query-arg").QueryArgParsed;
|
|
19
|
-
};
|
|
16
|
+
export type QueryArgParsed = import('./get-query-arg').QueryArgParsed;
|
|
17
|
+
export type QueryArgs = Record<string, QueryArgParsed>;
|
|
20
18
|
//# sourceMappingURL=get-query-args.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-query-args.d.ts","sourceRoot":"","sources":["../src/get-query-args.js"],"names":[],"mappings":"AAsDA;;;;;;;;;;;;;GAaG;AACH,kCAVW,MAAM,GAQL,SAAS,CA2BpB"}
|
|
1
|
+
{"version":3,"file":"get-query-args.d.ts","sourceRoot":"","sources":["../src/get-query-args.js"],"names":[],"mappings":"AAsDA;;;;;;;;;;;;;GAaG;AACH,kCAVW,MAAM,GAQL,SAAS,CA2BpB;6BAxFa,OAAO,iBAAiB,EAAE,cAAc;wBAGzC,OAAO,MAAM,EAAC,cAAc,CAAC"}
|
package/build-types/index.d.ts
CHANGED
|
@@ -22,4 +22,6 @@ export { safeDecodeURI } from "./safe-decode-uri";
|
|
|
22
22
|
export { safeDecodeURIComponent } from "./safe-decode-uri-component";
|
|
23
23
|
export { filterURLForDisplay } from "./filter-url-for-display";
|
|
24
24
|
export { cleanForSlug } from "./clean-for-slug";
|
|
25
|
+
export { getFilename } from "./get-filename";
|
|
26
|
+
export { normalizePath } from "./normalize-path";
|
|
25
27
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a path, returns a normalized path where equal query parameter values
|
|
3
|
+
* will be treated as identical, regardless of order they appear in the original
|
|
4
|
+
* text.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} path Original path.
|
|
7
|
+
*
|
|
8
|
+
* @return {string} Normalized path.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizePath(path: string): string;
|
|
11
|
+
//# sourceMappingURL=normalize-path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize-path.d.ts","sourceRoot":"","sources":["../src/normalize-path.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,oCAJW,MAAM,GAEL,MAAM,CA0BjB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/url",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "WordPress URL utilities.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -27,12 +27,11 @@
|
|
|
27
27
|
"types": "build-types",
|
|
28
28
|
"sideEffects": false,
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@babel/runtime": "^7.
|
|
31
|
-
"lodash": "^4.17.21"
|
|
32
|
-
"react-native-url-polyfill": "^1.1.2"
|
|
30
|
+
"@babel/runtime": "^7.16.0",
|
|
31
|
+
"lodash": "^4.17.21"
|
|
33
32
|
},
|
|
34
33
|
"publishConfig": {
|
|
35
34
|
"access": "public"
|
|
36
35
|
},
|
|
37
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "9a1dd3474d937468e4cf9caf9886ad61ef0a8f50"
|
|
38
37
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the filename part of the URL.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} url The full URL.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* const filename1 = getFilename( 'http://localhost:8080/this/is/a/test.jpg' ); // 'test.jpg'
|
|
9
|
+
* const filename2 = getFilename( '/this/is/a/test.png' ); // 'test.png'
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @return {string|void} The filename part of the URL.
|
|
13
|
+
*/
|
|
14
|
+
export function getFilename( url ) {
|
|
15
|
+
let filename;
|
|
16
|
+
try {
|
|
17
|
+
filename = new URL( url, 'http://example.com' ).pathname
|
|
18
|
+
.split( '/' )
|
|
19
|
+
.pop();
|
|
20
|
+
} catch ( error ) {}
|
|
21
|
+
|
|
22
|
+
if ( filename ) {
|
|
23
|
+
return filename;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.js
CHANGED
|
@@ -22,3 +22,5 @@ export { safeDecodeURI } from './safe-decode-uri';
|
|
|
22
22
|
export { safeDecodeURIComponent } from './safe-decode-uri-component';
|
|
23
23
|
export { filterURLForDisplay } from './filter-url-for-display';
|
|
24
24
|
export { cleanForSlug } from './clean-for-slug';
|
|
25
|
+
export { getFilename } from './get-filename';
|
|
26
|
+
export { normalizePath } from './normalize-path';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a path, returns a normalized path where equal query parameter values
|
|
3
|
+
* will be treated as identical, regardless of order they appear in the original
|
|
4
|
+
* text.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} path Original path.
|
|
7
|
+
*
|
|
8
|
+
* @return {string} Normalized path.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizePath( path ) {
|
|
11
|
+
const splitted = path.split( '?' );
|
|
12
|
+
const query = splitted[ 1 ];
|
|
13
|
+
const base = splitted[ 0 ];
|
|
14
|
+
if ( ! query ) {
|
|
15
|
+
return base;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// 'b=1&c=2&a=5'
|
|
19
|
+
return (
|
|
20
|
+
base +
|
|
21
|
+
'?' +
|
|
22
|
+
query
|
|
23
|
+
// [ 'b=1', 'c=2', 'a=5' ]
|
|
24
|
+
.split( '&' )
|
|
25
|
+
// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
|
|
26
|
+
.map( ( entry ) => entry.split( '=' ) )
|
|
27
|
+
// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
|
|
28
|
+
.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )
|
|
29
|
+
// [ 'a=5', 'b=1', 'c=2' ]
|
|
30
|
+
.map( ( pair ) => pair.join( '=' ) )
|
|
31
|
+
// 'a=5&b=1&c=2'
|
|
32
|
+
.join( '&' )
|
|
33
|
+
);
|
|
34
|
+
}
|
package/src/test/index.js
CHANGED
|
@@ -29,6 +29,8 @@ import {
|
|
|
29
29
|
filterURLForDisplay,
|
|
30
30
|
cleanForSlug,
|
|
31
31
|
getQueryArgs,
|
|
32
|
+
getFilename,
|
|
33
|
+
normalizePath,
|
|
32
34
|
} from '../';
|
|
33
35
|
import wptData from './fixtures/wpt-data';
|
|
34
36
|
|
|
@@ -240,6 +242,7 @@ describe( 'isValidPath', () => {
|
|
|
240
242
|
expect( isValidPath( 'relative/path' ) ).toBe( true );
|
|
241
243
|
expect( isValidPath( 'slightly/longer/path/' ) ).toBe( true );
|
|
242
244
|
expect( isValidPath( 'path/with/percent%20encoding' ) ).toBe( true );
|
|
245
|
+
expect( isValidPath( '/' ) ).toBe( true );
|
|
243
246
|
} );
|
|
244
247
|
|
|
245
248
|
it( 'returns false if the path is invalid', () => {
|
|
@@ -252,6 +255,42 @@ describe( 'isValidPath', () => {
|
|
|
252
255
|
} );
|
|
253
256
|
} );
|
|
254
257
|
|
|
258
|
+
describe( 'getFilename', () => {
|
|
259
|
+
it( 'returns the filename part of the URL', () => {
|
|
260
|
+
expect( getFilename( 'https://wordpress.org/image.jpg' ) ).toBe(
|
|
261
|
+
'image.jpg'
|
|
262
|
+
);
|
|
263
|
+
expect(
|
|
264
|
+
getFilename( 'https://wordpress.org/image.jpg?query=test' )
|
|
265
|
+
).toBe( 'image.jpg' );
|
|
266
|
+
expect( getFilename( 'https://wordpress.org/image.jpg#anchor' ) ).toBe(
|
|
267
|
+
'image.jpg'
|
|
268
|
+
);
|
|
269
|
+
expect(
|
|
270
|
+
getFilename( 'http://localhost:8080/a/path/to/an/image.jpg' )
|
|
271
|
+
).toBe( 'image.jpg' );
|
|
272
|
+
expect( getFilename( '/path/to/an/image.jpg' ) ).toBe( 'image.jpg' );
|
|
273
|
+
expect( getFilename( 'path/to/an/image.jpg' ) ).toBe( 'image.jpg' );
|
|
274
|
+
expect( getFilename( '/image.jpg' ) ).toBe( 'image.jpg' );
|
|
275
|
+
expect( getFilename( 'image.jpg' ) ).toBe( 'image.jpg' );
|
|
276
|
+
} );
|
|
277
|
+
|
|
278
|
+
it( 'returns undefined when the provided value does not contain a filename', () => {
|
|
279
|
+
expect( getFilename( 'http://localhost:8080/' ) ).toBe( undefined );
|
|
280
|
+
expect( getFilename( 'http://localhost:8080/a/path/' ) ).toBe(
|
|
281
|
+
undefined
|
|
282
|
+
);
|
|
283
|
+
expect( getFilename( 'http://localhost:8080/?query=test' ) ).toBe(
|
|
284
|
+
undefined
|
|
285
|
+
);
|
|
286
|
+
expect( getFilename( 'http://localhost:8080/#anchor' ) ).toBe(
|
|
287
|
+
undefined
|
|
288
|
+
);
|
|
289
|
+
expect( getFilename( 'a/path/' ) ).toBe( undefined );
|
|
290
|
+
expect( getFilename( '/' ) ).toBe( undefined );
|
|
291
|
+
} );
|
|
292
|
+
} );
|
|
293
|
+
|
|
255
294
|
describe( 'getQueryString', () => {
|
|
256
295
|
it( 'returns the query string of a URL', () => {
|
|
257
296
|
expect(
|
|
@@ -947,3 +986,26 @@ describe( 'cleanForSlug', () => {
|
|
|
947
986
|
expect( cleanForSlug( null ) ).toBe( '' );
|
|
948
987
|
} );
|
|
949
988
|
} );
|
|
989
|
+
|
|
990
|
+
describe( 'normalizePath', () => {
|
|
991
|
+
it( 'returns same value if no query parameters', () => {
|
|
992
|
+
const path = '/foo/bar';
|
|
993
|
+
|
|
994
|
+
expect( normalizePath( path ) ).toBe( path );
|
|
995
|
+
} );
|
|
996
|
+
|
|
997
|
+
it( 'returns a stable path', () => {
|
|
998
|
+
const abc = normalizePath( '/foo/bar?a=5&b=1&c=2' );
|
|
999
|
+
const bca = normalizePath( '/foo/bar?b=1&c=2&a=5' );
|
|
1000
|
+
const bac = normalizePath( '/foo/bar?b=1&a=5&c=2' );
|
|
1001
|
+
const acb = normalizePath( '/foo/bar?a=5&c=2&b=1' );
|
|
1002
|
+
const cba = normalizePath( '/foo/bar?c=2&b=1&a=5' );
|
|
1003
|
+
const cab = normalizePath( '/foo/bar?c=2&a=5&b=1' );
|
|
1004
|
+
|
|
1005
|
+
expect( abc ).toBe( bca );
|
|
1006
|
+
expect( bca ).toBe( bac );
|
|
1007
|
+
expect( bac ).toBe( acb );
|
|
1008
|
+
expect( acb ).toBe( cba );
|
|
1009
|
+
expect( cba ).toBe( cab );
|
|
1010
|
+
} );
|
|
1011
|
+
} );
|