@jsenv/node-esm-resolution 1.0.1 → 1.0.3

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/src/url_utils.js CHANGED
@@ -1,62 +1,65 @@
1
1
  export const asDirectoryUrl = (url) => {
2
- const { pathname } = new URL(url)
2
+ const { pathname } = new URL(url);
3
3
  if (pathname.endsWith("/")) {
4
- return url
4
+ return url;
5
5
  }
6
- return new URL("./", url).href
7
- }
6
+ return new URL("./", url).href;
7
+ };
8
8
 
9
9
  export const getParentUrl = (url) => {
10
10
  if (url.startsWith("file://")) {
11
11
  // With node.js new URL('../', 'file:///C:/').href
12
12
  // returns "file:///C:/" instead of "file:///"
13
- const resource = url.slice("file://".length)
14
- const slashLastIndex = resource.lastIndexOf("/")
13
+ const resource = url.slice("file://".length);
14
+ const slashLastIndex = resource.lastIndexOf("/");
15
15
  if (slashLastIndex === -1) {
16
- return url
16
+ return url;
17
17
  }
18
- const lastCharIndex = resource.length - 1
18
+ const lastCharIndex = resource.length - 1;
19
19
  if (slashLastIndex === lastCharIndex) {
20
- const slashBeforeLastIndex = resource.lastIndexOf("/", slashLastIndex - 1)
20
+ const slashBeforeLastIndex = resource.lastIndexOf(
21
+ "/",
22
+ slashLastIndex - 1,
23
+ );
21
24
  if (slashBeforeLastIndex === -1) {
22
- return url
25
+ return url;
23
26
  }
24
- return `file://${resource.slice(0, slashBeforeLastIndex + 1)}`
27
+ return `file://${resource.slice(0, slashBeforeLastIndex + 1)}`;
25
28
  }
26
29
 
27
- return `file://${resource.slice(0, slashLastIndex + 1)}`
30
+ return `file://${resource.slice(0, slashLastIndex + 1)}`;
28
31
  }
29
- return new URL(url.endsWith("/") ? "../" : "./", url).href
30
- }
32
+ return new URL(url.endsWith("/") ? "../" : "./", url).href;
33
+ };
31
34
 
32
35
  export const isValidUrl = (url) => {
33
36
  try {
34
37
  // eslint-disable-next-line no-new
35
- new URL(url)
36
- return true
38
+ new URL(url);
39
+ return true;
37
40
  } catch (e) {
38
- return false
41
+ return false;
39
42
  }
40
- }
43
+ };
41
44
 
42
45
  export const urlToFilename = (url) => {
43
- const { pathname } = new URL(url)
46
+ const { pathname } = new URL(url);
44
47
  const pathnameBeforeLastSlash = pathname.endsWith("/")
45
48
  ? pathname.slice(0, -1)
46
- : pathname
47
- const slashLastIndex = pathnameBeforeLastSlash.lastIndexOf("/")
49
+ : pathname;
50
+ const slashLastIndex = pathnameBeforeLastSlash.lastIndexOf("/");
48
51
  const filename =
49
52
  slashLastIndex === -1
50
53
  ? pathnameBeforeLastSlash
51
- : pathnameBeforeLastSlash.slice(slashLastIndex + 1)
52
- return filename
53
- }
54
+ : pathnameBeforeLastSlash.slice(slashLastIndex + 1);
55
+ return filename;
56
+ };
54
57
 
55
58
  export const urlToExtension = (url) => {
56
- const filename = urlToFilename(url)
57
- const dotLastIndex = filename.lastIndexOf(".")
58
- if (dotLastIndex === -1) return ""
59
+ const filename = urlToFilename(url);
60
+ const dotLastIndex = filename.lastIndexOf(".");
61
+ if (dotLastIndex === -1) return "";
59
62
  // if (dotLastIndex === pathname.length - 1) return ""
60
- const extension = filename.slice(dotLastIndex)
61
- return extension
62
- }
63
+ const extension = filename.slice(dotLastIndex);
64
+ return extension;
65
+ };