@jsenv/urls 2.7.3 → 2.8.0

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.
Files changed (28) hide show
  1. package/package.json +9 -4
  2. package/src/{resource_to_parts.js → composition/resource_to_parts.js} +1 -1
  3. package/src/composition/set_url_part.js +90 -0
  4. package/src/{url_to_extension.js → composition/url_to_extension.js} +1 -1
  5. package/src/{filesystem_path_to_url.js → file_and_path/filesystem_path_to_url.js} +0 -1
  6. package/src/{resolve_directory_url.js → file_and_path/resolve_directory_url.js} +1 -1
  7. package/src/{yield_ancestor_urls.js → file_and_path/yield_ancestor_urls.js} +1 -1
  8. package/src/main_browser.js +1 -0
  9. package/src/main_node.js +4 -0
  10. package/src/main_shared.js +51 -0
  11. package/src/{url_utils.js → query_params/query_params.js} +0 -91
  12. package/src/url_to_relative_url.js +1 -1
  13. package/src/main.js +0 -53
  14. /package/src/{common_pathname.js → composition/common_pathname.js} +0 -0
  15. /package/src/{internal → composition}/pathname_to_extension.js +0 -0
  16. /package/src/{url_to_basename.js → composition/url_to_basename.js} +0 -0
  17. /package/src/{url_to_filename.js → composition/url_to_filename.js} +0 -0
  18. /package/src/{url_to_origin.js → composition/url_to_origin.js} +0 -0
  19. /package/src/{url_to_parent_url.js → composition/url_to_parent_url.js} +0 -0
  20. /package/src/{url_to_pathname.js → composition/url_to_pathname.js} +0 -0
  21. /package/src/{url_to_resource.js → composition/url_to_resource.js} +0 -0
  22. /package/src/{url_to_scheme.js → composition/url_to_scheme.js} +0 -0
  23. /package/src/{caller_position.js → file_and_path/caller_position.js} +0 -0
  24. /package/src/{is_filesystem_path.js → file_and_path/is_filesystem_path.js} +0 -0
  25. /package/src/{resolve_url.js → file_and_path/resolve_url.js} +0 -0
  26. /package/src/{url_is_inside_of.js → file_and_path/url_is_inside_of.js} +0 -0
  27. /package/src/{url_to_filesystem_path.js → file_and_path/url_to_filesystem_path.js} +0 -0
  28. /package/src/{windows_file_path_utils.js → file_and_path/windows_file_path_utils.js} +0 -0
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@jsenv/urls",
3
- "version": "2.7.3",
3
+ "version": "2.8.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/jsenv/core",
8
- "directory": "packages/independent/tooling/urls"
8
+ "directory": "packages/tooling/urls"
9
9
  },
10
10
  "publishConfig": {
11
11
  "access": "public"
@@ -17,7 +17,8 @@
17
17
  "type": "module",
18
18
  "exports": {
19
19
  ".": {
20
- "import": "./src/main.js"
20
+ "browser": "./src/main_browser.js",
21
+ "node": "./src/main_node.js"
21
22
  },
22
23
  "./*": "./*"
23
24
  },
@@ -26,6 +27,10 @@
26
27
  ],
27
28
  "sideEffects": false,
28
29
  "dependencies": {
29
- "@jsenv/humanize": "1.5.1"
30
+ "@jsenv/humanize": "1.6.0"
31
+ },
32
+ "devDependencies": {
33
+ "@jsenv/assert": "../assert",
34
+ "@jsenv/urls": "../urls"
30
35
  }
31
36
  }
@@ -1,4 +1,4 @@
1
- import { pathnameToExtension } from "./internal/pathname_to_extension.js";
1
+ import { pathnameToExtension } from "./pathname_to_extension.js";
2
2
 
3
3
  export const resourceToParts = (resource) => {
4
4
  const searchSeparatorIndex = resource.indexOf("?");
@@ -0,0 +1,90 @@
1
+ import { filenameToBasename } from "./url_to_basename.js";
2
+ import { urlToExtension } from "./url_to_extension.js";
3
+ import { pathnameToFilename } from "./url_to_filename.js";
4
+ import { urlToOrigin } from "./url_to_origin.js";
5
+ import { urlToResource } from "./url_to_resource.js";
6
+
7
+ export const setUrlExtension = (url, extension) => {
8
+ const origin = urlToOrigin(url);
9
+ const currentExtension = urlToExtension(url);
10
+ if (typeof extension === "function") {
11
+ extension = extension(currentExtension);
12
+ }
13
+ const resource = urlToResource(url);
14
+ const [pathname, search] = resource.split("?");
15
+ const pathnameWithoutExtension = currentExtension
16
+ ? pathname.slice(0, -currentExtension.length)
17
+ : pathname;
18
+ let newPathname;
19
+ if (pathnameWithoutExtension.endsWith("/")) {
20
+ newPathname = pathnameWithoutExtension.slice(0, -1);
21
+ newPathname += extension;
22
+ newPathname += "/";
23
+ } else {
24
+ newPathname = pathnameWithoutExtension;
25
+ newPathname += extension;
26
+ }
27
+ return `${origin}${newPathname}${search ? `?${search}` : ""}`;
28
+ };
29
+
30
+ export const setUrlFilename = (url, filename) => {
31
+ const parentPathname = new URL("./", url).pathname;
32
+ return transformUrlPathname(url, (pathname) => {
33
+ if (typeof filename === "function") {
34
+ filename = filename(pathnameToFilename(pathname));
35
+ }
36
+ return `${parentPathname}${filename}`;
37
+ });
38
+ };
39
+
40
+ export const setUrlBasename = (url, basename) => {
41
+ return setUrlFilename(url, (filename) => {
42
+ if (typeof basename === "function") {
43
+ basename = basename(filenameToBasename(filename));
44
+ }
45
+ return `${basename}${urlToExtension(url)}`;
46
+ });
47
+ };
48
+
49
+ const transformUrlPathname = (url, transformer) => {
50
+ if (typeof url === "string") {
51
+ const urlObject = new URL(url);
52
+ const { pathname } = urlObject;
53
+ const pathnameTransformed = transformer(pathname);
54
+ if (pathnameTransformed === pathname) {
55
+ return url;
56
+ }
57
+ let { origin } = urlObject;
58
+ // origin is "null" for "file://" urls with Node.js
59
+ if (origin === "null" && urlObject.href.startsWith("file:")) {
60
+ origin = "file://";
61
+ }
62
+ const { search, hash } = urlObject;
63
+ const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
64
+ return urlWithPathnameTransformed;
65
+ }
66
+ const pathnameTransformed = transformer(url.pathname);
67
+ url.pathname = pathnameTransformed;
68
+ return url;
69
+ };
70
+ export const ensurePathnameTrailingSlash = (url) => {
71
+ return transformUrlPathname(url, (pathname) => {
72
+ return pathname.endsWith("/") ? pathname : `${pathname}/`;
73
+ });
74
+ };
75
+ export const removePathnameTrailingSlash = (url) => {
76
+ return transformUrlPathname(url, (pathname) => {
77
+ return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
78
+ });
79
+ };
80
+
81
+ export const asUrlUntilPathname = (url) => {
82
+ const urlObject = new URL(url);
83
+ let { origin, pathname } = urlObject;
84
+ // origin is "null" for "file://" urls with Node.js
85
+ if (origin === "null" && urlObject.href.startsWith("file:")) {
86
+ origin = "file://";
87
+ }
88
+ const urlUntilPathname = `${origin}${pathname}`;
89
+ return urlUntilPathname;
90
+ };
@@ -1,4 +1,4 @@
1
- import { pathnameToExtension } from "./internal/pathname_to_extension.js";
1
+ import { pathnameToExtension } from "./pathname_to_extension.js";
2
2
  import { urlToPathname } from "./url_to_pathname.js";
3
3
 
4
4
  export const urlToExtension = (url) => {
@@ -1,5 +1,4 @@
1
1
  import { pathToFileURL } from "node:url";
2
-
3
2
  import { isFileSystemPath } from "./is_filesystem_path.js";
4
3
 
5
4
  export const fileSystemPathToUrl = (value) => {
@@ -1,5 +1,5 @@
1
+ import { ensurePathnameTrailingSlash } from "../composition/set_url_part.js";
1
2
  import { resolveUrl } from "./resolve_url.js";
2
- import { ensurePathnameTrailingSlash } from "./url_utils.js";
3
3
 
4
4
  export const resolveDirectoryUrl = (specifier, baseUrl) => {
5
5
  const url = resolveUrl(specifier, baseUrl);
@@ -1,4 +1,4 @@
1
- import { ensurePathnameTrailingSlash } from "./url_utils.js";
1
+ import { ensurePathnameTrailingSlash } from "../composition/set_url_part.js";
2
2
 
3
3
  export function* yieldAncestorUrls(url, rootUrl, { yieldSelf } = {}) {
4
4
  url = String(url);
@@ -0,0 +1 @@
1
+ export * from "./main_shared.js";
@@ -0,0 +1,4 @@
1
+ export { getCallerPosition } from "./file_and_path/caller_position.js";
2
+ export { fileSystemPathToUrl } from "./file_and_path/filesystem_path_to_url.js";
3
+ export { urlToFileSystemPath } from "./file_and_path/url_to_filesystem_path.js";
4
+ export * from "./main_shared.js";
@@ -0,0 +1,51 @@
1
+ // tslint:disable:ordered-imports
2
+
3
+ export { DATA_URL } from "./data_url.js";
4
+ export { stringifyUrlSite } from "./url_trace.js";
5
+ export {
6
+ setUrlFilename,
7
+ setUrlBasename,
8
+ setUrlExtension,
9
+ ensurePathnameTrailingSlash,
10
+ removePathnameTrailingSlash,
11
+ asUrlUntilPathname,
12
+ } from "./composition/set_url_part.js";
13
+ export {
14
+ asUrlWithoutSearch,
15
+ asSpecifierWithoutSearch,
16
+ isValidUrl,
17
+ normalizeUrl,
18
+ injectQueryParamsIntoSpecifier,
19
+ injectQueryParams,
20
+ injectQueryParamWithoutEncoding,
21
+ injectQueryParamIntoSpecifierWithoutEncoding,
22
+ renderUrlOrRelativeUrlFilename,
23
+ } from "./query_params/query_params.js";
24
+ // composition
25
+ export { moveUrl } from "./move_url.js";
26
+ export { urlToBasename } from "./composition/url_to_basename.js";
27
+ export { urlToExtension } from "./composition/url_to_extension.js";
28
+ export { urlToFilename } from "./composition/url_to_filename.js";
29
+ export { urlToOrigin } from "./composition/url_to_origin.js";
30
+ export { urlToParentUrl } from "./composition/url_to_parent_url.js";
31
+ export { urlToPathname } from "./composition/url_to_pathname.js";
32
+ export { urlToRelativeUrl } from "./url_to_relative_url.js";
33
+ export { urlToResource } from "./composition/url_to_resource.js";
34
+ export { urlToScheme } from "./composition/url_to_scheme.js";
35
+ export { getCommonPathname } from "./composition/common_pathname.js";
36
+ export {
37
+ resourceToParts,
38
+ resourceToPathname,
39
+ resourceToExtension,
40
+ } from "./composition/resource_to_parts.js";
41
+ // file_and_path
42
+ export {
43
+ startsWithWindowsDriveLetter,
44
+ windowsFilePathToUrl,
45
+ replaceBackSlashesWithSlashes,
46
+ } from "./file_and_path/windows_file_path_utils.js";
47
+ export { isFileSystemPath } from "./file_and_path/is_filesystem_path.js";
48
+ export { resolveUrl } from "./file_and_path/resolve_url.js";
49
+ export { resolveDirectoryUrl } from "./file_and_path/resolve_directory_url.js";
50
+ export { urlIsInsideOf } from "./file_and_path/url_is_inside_of.js";
51
+ export { yieldAncestorUrls } from "./file_and_path/yield_ancestor_urls.js";
@@ -1,9 +1,3 @@
1
- import { filenameToBasename } from "./url_to_basename.js";
2
- import { urlToExtension } from "./url_to_extension.js";
3
- import { pathnameToFilename } from "./url_to_filename.js";
4
- import { urlToOrigin } from "./url_to_origin.js";
5
- import { urlToResource } from "./url_to_resource.js";
6
-
7
1
  export const asUrlWithoutSearch = (url) => {
8
2
  url = String(url);
9
3
  if (url.includes("?")) {
@@ -155,88 +149,3 @@ export const renderUrlOrRelativeUrlFilename = (urlOrRelativeUrl, renderer) => {
155
149
  });
156
150
  return `${beforeFilename}${newFilename}${afterQuestion}`;
157
151
  };
158
-
159
- export const setUrlExtension = (url, extension) => {
160
- const origin = urlToOrigin(url);
161
- const currentExtension = urlToExtension(url);
162
- if (typeof extension === "function") {
163
- extension = extension(currentExtension);
164
- }
165
- const resource = urlToResource(url);
166
- const [pathname, search] = resource.split("?");
167
- const pathnameWithoutExtension = currentExtension
168
- ? pathname.slice(0, -currentExtension.length)
169
- : pathname;
170
- let newPathname;
171
- if (pathnameWithoutExtension.endsWith("/")) {
172
- newPathname = pathnameWithoutExtension.slice(0, -1);
173
- newPathname += extension;
174
- newPathname += "/";
175
- } else {
176
- newPathname = pathnameWithoutExtension;
177
- newPathname += extension;
178
- }
179
- return `${origin}${newPathname}${search ? `?${search}` : ""}`;
180
- };
181
-
182
- export const setUrlFilename = (url, filename) => {
183
- const parentPathname = new URL("./", url).pathname;
184
- return transformUrlPathname(url, (pathname) => {
185
- if (typeof filename === "function") {
186
- filename = filename(pathnameToFilename(pathname));
187
- }
188
- return `${parentPathname}${filename}`;
189
- });
190
- };
191
-
192
- export const setUrlBasename = (url, basename) => {
193
- return setUrlFilename(url, (filename) => {
194
- if (typeof basename === "function") {
195
- basename = basename(filenameToBasename(filename));
196
- }
197
- return `${basename}${urlToExtension(url)}`;
198
- });
199
- };
200
-
201
- const transformUrlPathname = (url, transformer) => {
202
- if (typeof url === "string") {
203
- const urlObject = new URL(url);
204
- const { pathname } = urlObject;
205
- const pathnameTransformed = transformer(pathname);
206
- if (pathnameTransformed === pathname) {
207
- return url;
208
- }
209
- let { origin } = urlObject;
210
- // origin is "null" for "file://" urls with Node.js
211
- if (origin === "null" && urlObject.href.startsWith("file:")) {
212
- origin = "file://";
213
- }
214
- const { search, hash } = urlObject;
215
- const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
216
- return urlWithPathnameTransformed;
217
- }
218
- const pathnameTransformed = transformer(url.pathname);
219
- url.pathname = pathnameTransformed;
220
- return url;
221
- };
222
- export const ensurePathnameTrailingSlash = (url) => {
223
- return transformUrlPathname(url, (pathname) => {
224
- return pathname.endsWith("/") ? pathname : `${pathname}/`;
225
- });
226
- };
227
- export const removePathnameTrailingSlash = (url) => {
228
- return transformUrlPathname(url, (pathname) => {
229
- return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
230
- });
231
- };
232
-
233
- export const asUrlUntilPathname = (url) => {
234
- const urlObject = new URL(url);
235
- let { origin, pathname } = urlObject;
236
- // origin is "null" for "file://" urls with Node.js
237
- if (origin === "null" && urlObject.href.startsWith("file:")) {
238
- origin = "file://";
239
- }
240
- const urlUntilPathname = `${origin}${pathname}`;
241
- return urlUntilPathname;
242
- };
@@ -1,4 +1,4 @@
1
- import { getCommonPathname } from "./common_pathname.js";
1
+ import { getCommonPathname } from "./composition/common_pathname.js";
2
2
 
3
3
  export const urlToRelativeUrl = (
4
4
  url,
package/src/main.js DELETED
@@ -1,53 +0,0 @@
1
- // tslint:disable:ordered-imports
2
-
3
- export { DATA_URL } from "./data_url.js";
4
- export { stringifyUrlSite } from "./url_trace.js";
5
- export {
6
- asUrlWithoutSearch,
7
- asSpecifierWithoutSearch,
8
- isValidUrl,
9
- normalizeUrl,
10
- injectQueryParamsIntoSpecifier,
11
- injectQueryParams,
12
- injectQueryParamWithoutEncoding,
13
- injectQueryParamIntoSpecifierWithoutEncoding,
14
- renderUrlOrRelativeUrlFilename,
15
- setUrlFilename,
16
- setUrlBasename,
17
- setUrlExtension,
18
- ensurePathnameTrailingSlash,
19
- removePathnameTrailingSlash,
20
- asUrlUntilPathname,
21
- } from "./url_utils.js";
22
- export {
23
- startsWithWindowsDriveLetter,
24
- windowsFilePathToUrl,
25
- replaceBackSlashesWithSlashes,
26
- } from "./windows_file_path_utils.js";
27
- export { getCallerPosition } from "./caller_position.js";
28
-
29
- export { fileSystemPathToUrl } from "./filesystem_path_to_url.js";
30
- export { isFileSystemPath } from "./is_filesystem_path.js";
31
- export { resolveDirectoryUrl } from "./resolve_directory_url.js";
32
- export { moveUrl } from "./move_url.js";
33
- export { resolveUrl } from "./resolve_url.js";
34
- export { urlIsInsideOf } from "./url_is_inside_of.js";
35
- export { urlToBasename } from "./url_to_basename.js";
36
- export { urlToExtension } from "./url_to_extension.js";
37
- export { urlToFilename } from "./url_to_filename.js";
38
- export { urlToFileSystemPath } from "./url_to_filesystem_path.js";
39
- export { urlToOrigin } from "./url_to_origin.js";
40
- export { urlToParentUrl } from "./url_to_parent_url.js";
41
- export { urlToPathname } from "./url_to_pathname.js";
42
- export { urlToRelativeUrl } from "./url_to_relative_url.js";
43
- export { urlToResource } from "./url_to_resource.js";
44
- export { urlToScheme } from "./url_to_scheme.js";
45
-
46
- export { getCommonPathname } from "./common_pathname.js";
47
- export {
48
- resourceToParts,
49
- resourceToPathname,
50
- resourceToExtension,
51
- } from "./resource_to_parts.js";
52
-
53
- export { yieldAncestorUrls } from "./yield_ancestor_urls.js";