@cspell/url 8.17.0 → 8.17.2

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/dist/url.mjs DELETED
@@ -1,187 +0,0 @@
1
- const isURLRegEx = /^(\w[\w-]{1,63}:\/|data:|stdin:)/i;
2
- /**
3
- * Try to make a URL.
4
- * @param url
5
- * @param relativeTo - optional URL, if given, url will be parsed as relative.
6
- * @returns a URL
7
- */
8
- export function toURL(url, relativeTo) {
9
- return normalizeWindowsUrl(url instanceof URL ? url : new URL(url, relativeTo));
10
- }
11
- /**
12
- * Try to determine the parent directory URL of the uri.
13
- * If it is not a hierarchical URL, then it will return the URL.
14
- * @param url - url to extract the dirname from.
15
- * @returns a URL
16
- */
17
- export function urlParent(url) {
18
- url = toURL(url);
19
- if (url.protocol === 'data:') {
20
- return url;
21
- }
22
- const hasTrailingSlash = url.pathname.endsWith('/');
23
- if (!url.pathname.startsWith('/')) {
24
- const parts = url.pathname.split('/').slice(0, hasTrailingSlash ? -2 : -1);
25
- let pathname = parts.join('/');
26
- pathname = (pathname && pathname + '/') || '';
27
- return new URL(url.protocol + (url.host ? '//' + url.host : '') + pathname + url.search + url.hash);
28
- }
29
- return new URL(hasTrailingSlash ? '..' : '.', url);
30
- }
31
- /**
32
- * Alias of {@link urlParent}
33
- * Try to determine the parent directory URL of the uri.
34
- * If it is not a hierarchical URL, then it will return the URL.
35
- * @param url - url to extract the dirname from.
36
- * @returns a URL
37
- */
38
- export const urlDirname = urlParent;
39
- /**
40
- * return the basename (last portion of the URL pathname) of a path. It does NOT remove the trailing slash.
41
- * @param path - URL pathname to extract the basename from.
42
- */
43
- export function basenameOfUrlPathname(path) {
44
- const adj = path.endsWith('/') ? 2 : 0;
45
- const idx = path.lastIndexOf('/', path.length - adj);
46
- return idx >= 0 ? path.slice(idx + 1) : path;
47
- }
48
- export function isUrlLike(filename) {
49
- return filename instanceof URL || isURLRegEx.test(filename);
50
- }
51
- export function isNotUrlLike(filename) {
52
- return !isUrlLike(filename);
53
- }
54
- /**
55
- * Check if `url` is a URL instance.
56
- * @returns
57
- */
58
- export function isURL(url) {
59
- return url instanceof URL;
60
- }
61
- /**
62
- *
63
- * @param url - url to check
64
- * @param protocol - protocol to check against - e.g. 'file:', 'http:', 'https:'
65
- * @returns
66
- */
67
- export function hasProtocol(url, protocol) {
68
- protocol = protocol.endsWith(':') ? protocol : protocol + ':';
69
- return typeof url === 'string' ? url.startsWith(protocol) : url.protocol === protocol;
70
- }
71
- /**
72
- * Attempts to add a trailing slash to the URL pathname if it does not already have one.
73
- * Some If the pathname doesn't start with a `/`, a trailing slash is not added.
74
- * @param url - a URL
75
- * @returns
76
- */
77
- export function addTrailingSlash(url) {
78
- if (url.pathname.endsWith('/'))
79
- return url;
80
- const urlWithSlash = new URL(url.href);
81
- urlWithSlash.pathname += '/';
82
- return urlWithSlash;
83
- }
84
- /**
85
- * Remove the filename at the end of the URL pathname.
86
- * If the URL pathname ends with a `/`, it is considered a directory and the URL is returned as is.
87
- * If the URL pathname does not start with a `/`, it is considered an non-regular URL and the URL is returned as is.
88
- * @param url
89
- * @returns
90
- */
91
- export function urlRemoveFilename(url) {
92
- // Test if it is already a directory or it is not possible to remove the filename.
93
- if (url.pathname.endsWith('/') || !url.pathname.startsWith('/'))
94
- return url;
95
- return new URL('./', url);
96
- }
97
- /**
98
- * Extract the filename from the URL pathname.
99
- *
100
- * ```ts
101
- * url.href === new URL(urlFilename(url), urlRemoveFilename(url)).href
102
- * ```
103
- * @param url - URL to extract the filename from.
104
- * @returns the filename or empty string if the URL pathname ends with a `/`.
105
- */
106
- export function urlFilename(url) {
107
- if (!url.pathname.startsWith('/'))
108
- return '';
109
- const lastSlash = url.pathname.lastIndexOf('/');
110
- return url.pathname.slice(lastSlash + 1);
111
- }
112
- /**
113
- * Calculate the relative path to go from `urlFrom` to `urlTo`.
114
- * The protocol is not evaluated. Only the `url.pathname` is used.
115
- * @param urlFrom
116
- * @param urlTo
117
- * @returns the relative path
118
- */
119
- export function urlRelative(urlFrom, urlTo) {
120
- return urlToUrlRelative(toURL(urlFrom), toURL(urlTo));
121
- }
122
- /**
123
- * Calculate the relative path to go from `urlFrom` to `urlTo`.
124
- * The protocol is not evaluated. Only the `url.pathname` is used.
125
- * @param urlFrom
126
- * @param urlTo
127
- * @returns the relative path
128
- */
129
- export function urlToUrlRelative(urlFrom, urlTo) {
130
- let pFrom = urlFrom.pathname;
131
- const pTo = urlTo.pathname;
132
- if (pFrom === pTo)
133
- return '';
134
- pFrom = pFrom.endsWith('/') ? pFrom : new URL('./', urlFrom).pathname;
135
- if (pTo.startsWith(pFrom))
136
- return decodeURIComponent(pTo.slice(pFrom.length));
137
- const p0 = pFrom;
138
- const p1 = pTo;
139
- if (p1.startsWith(p0)) {
140
- return decodeURIComponent(p0 === p1 ? '' : p1.slice(p0.lastIndexOf('/') + 1));
141
- }
142
- const p0Parts = p0.split('/').slice(0, -1); // drop the last segment.
143
- const p1Parts = p1.split('/');
144
- let i = 0;
145
- for (i = 0; i < p0Parts.length && i < p1Parts.length - 1 && p0Parts[i] === p1Parts[i]; ++i) {
146
- // empty
147
- }
148
- const rel = '../'.repeat(p0Parts.length - i) + p1Parts.slice(i).join('/');
149
- return decodeURIComponent(rel.length < p1.length ? rel : p1);
150
- }
151
- export const regExpWindowsPath = /^[\\/]([a-zA-Z]:[\\/])/;
152
- export const regExpEncodedColon = /%3[aA]/g;
153
- const badUncLocalhostUrl = /^(\/+[a-zA-Z])\$/;
154
- /**
155
- * Ensure that a windows file url is correctly formatted with a capitol letter for the drive.
156
- *
157
- * @param url - URL to check.
158
- * @returns a new URL if modified or converted from a string.
159
- */
160
- export function normalizeWindowsUrl(url) {
161
- url = typeof url === 'string' ? new URL(url) : url;
162
- if (url.protocol === 'file:') {
163
- let pathname = url.pathname.replaceAll('%3A', ':').replaceAll('%3a', ':').replaceAll('%24', '$');
164
- if (!url.host) {
165
- pathname = pathname.replace(badUncLocalhostUrl, '$1:');
166
- }
167
- pathname = pathname.replace(regExpWindowsPath, (d) => d.toUpperCase());
168
- if (pathname !== url.pathname) {
169
- url = new URL(url);
170
- url.pathname = pathname;
171
- return fixUncUrl(url);
172
- }
173
- }
174
- return fixUncUrl(url);
175
- }
176
- /**
177
- * There is a bug is NodeJS that sometimes causes UNC paths converted to a URL to be prefixed with `file:////`.
178
- * @param url - URL to check.
179
- * @returns fixed URL if needed.
180
- */
181
- export function fixUncUrl(url) {
182
- if (url.href.startsWith('file:////')) {
183
- return new URL(url.href.replace(/^file:\/{4}/, 'file://'));
184
- }
185
- return url;
186
- }
187
- //# sourceMappingURL=url.mjs.map