@gjsify/path 0.3.12 → 0.3.14

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/lib/esm/posix.js CHANGED
@@ -1,339 +1,343 @@
1
+ import { __exportAll } from "./_virtual/_rolldown/runtime.js";
1
2
  import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./constants.js";
2
- import {
3
- assertPath,
4
- isPosixPathSeparator,
5
- normalizeString,
6
- _format
7
- } from "./util.js";
3
+ import { _format, assertPath, isPosixPathSeparator, normalizeString } from "./util.js";
4
+
5
+ //#region src/posix.ts
6
+ var posix_exports = /* @__PURE__ */ __exportAll({
7
+ basename: () => basename,
8
+ delimiter: () => ":",
9
+ dirname: () => dirname,
10
+ extname: () => extname,
11
+ format: () => format,
12
+ isAbsolute: () => isAbsolute,
13
+ join: () => join,
14
+ normalize: () => normalize,
15
+ parse: () => parse,
16
+ relative: () => relative,
17
+ resolve: () => resolve,
18
+ sep: () => "/",
19
+ toNamespacedPath: () => toNamespacedPath
20
+ });
8
21
  const sep = "/";
9
22
  const delimiter = ":";
10
23
  function posixCwd() {
11
- if (typeof globalThis.process?.cwd === "function") {
12
- return globalThis.process.cwd();
13
- }
14
- try {
15
- const GLib = globalThis.imports?.gi?.GLib;
16
- if (GLib?.get_current_dir) {
17
- return GLib.get_current_dir();
18
- }
19
- } catch {
20
- }
21
- return "/";
24
+ if (typeof globalThis.process?.cwd === "function") {
25
+ return globalThis.process.cwd();
26
+ }
27
+ try {
28
+ const GLib = globalThis.imports?.gi?.GLib;
29
+ if (GLib?.get_current_dir) {
30
+ return GLib.get_current_dir();
31
+ }
32
+ } catch {}
33
+ return "/";
22
34
  }
23
35
  function resolve(...pathSegments) {
24
- let resolvedPath = "";
25
- let resolvedAbsolute = false;
26
- for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
27
- let path;
28
- if (i >= 0) {
29
- path = pathSegments[i];
30
- assertPath(path);
31
- if (path.length === 0) continue;
32
- } else {
33
- path = posixCwd();
34
- }
35
- resolvedPath = `${path}/${resolvedPath}`;
36
- resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
37
- }
38
- resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, "/", isPosixPathSeparator);
39
- if (resolvedAbsolute) {
40
- if (resolvedPath.length > 0) {
41
- return `/${resolvedPath}`;
42
- }
43
- return "/";
44
- } else if (resolvedPath.length > 0) {
45
- return resolvedPath;
46
- }
47
- return ".";
36
+ let resolvedPath = "";
37
+ let resolvedAbsolute = false;
38
+ for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
39
+ let path;
40
+ if (i >= 0) {
41
+ path = pathSegments[i];
42
+ assertPath(path);
43
+ if (path.length === 0) continue;
44
+ } else {
45
+ path = posixCwd();
46
+ }
47
+ resolvedPath = `${path}/${resolvedPath}`;
48
+ resolvedAbsolute = path.charCodeAt(0) === 47;
49
+ }
50
+ resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, "/", isPosixPathSeparator);
51
+ if (resolvedAbsolute) {
52
+ if (resolvedPath.length > 0) {
53
+ return `/${resolvedPath}`;
54
+ }
55
+ return "/";
56
+ } else if (resolvedPath.length > 0) {
57
+ return resolvedPath;
58
+ }
59
+ return ".";
48
60
  }
49
61
  function normalize(path) {
50
- assertPath(path);
51
- if (path.length === 0) return ".";
52
- const isAbsolutePath = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
53
- const trailingSeparator = path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
54
- let normalized = normalizeString(path, !isAbsolutePath, "/", isPosixPathSeparator);
55
- if (normalized.length === 0 && !isAbsolutePath) {
56
- normalized = ".";
57
- }
58
- if (normalized.length > 0 && trailingSeparator) {
59
- normalized += "/";
60
- }
61
- if (isAbsolutePath) {
62
- return `/${normalized}`;
63
- }
64
- return normalized;
62
+ assertPath(path);
63
+ if (path.length === 0) return ".";
64
+ const isAbsolutePath = path.charCodeAt(0) === 47;
65
+ const trailingSeparator = path.charCodeAt(path.length - 1) === 47;
66
+ let normalized = normalizeString(path, !isAbsolutePath, "/", isPosixPathSeparator);
67
+ if (normalized.length === 0 && !isAbsolutePath) {
68
+ normalized = ".";
69
+ }
70
+ if (normalized.length > 0 && trailingSeparator) {
71
+ normalized += "/";
72
+ }
73
+ if (isAbsolutePath) {
74
+ return `/${normalized}`;
75
+ }
76
+ return normalized;
65
77
  }
66
78
  function isAbsolute(path) {
67
- assertPath(path);
68
- return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
79
+ assertPath(path);
80
+ return path.length > 0 && path.charCodeAt(0) === 47;
69
81
  }
70
82
  function join(...paths) {
71
- if (paths.length === 0) return ".";
72
- let joined;
73
- for (let i = 0; i < paths.length; ++i) {
74
- const arg = paths[i];
75
- assertPath(arg);
76
- if (arg.length > 0) {
77
- if (joined === void 0) {
78
- joined = arg;
79
- } else {
80
- joined += `/${arg}`;
81
- }
82
- }
83
- }
84
- if (joined === void 0) return ".";
85
- return normalize(joined);
83
+ if (paths.length === 0) return ".";
84
+ let joined;
85
+ for (let i = 0; i < paths.length; ++i) {
86
+ const arg = paths[i];
87
+ assertPath(arg);
88
+ if (arg.length > 0) {
89
+ if (joined === undefined) {
90
+ joined = arg;
91
+ } else {
92
+ joined += `/${arg}`;
93
+ }
94
+ }
95
+ }
96
+ if (joined === undefined) return ".";
97
+ return normalize(joined);
86
98
  }
87
99
  function relative(from, to) {
88
- assertPath(from);
89
- assertPath(to);
90
- if (from === to) return "";
91
- from = resolve(from);
92
- to = resolve(to);
93
- if (from === to) return "";
94
- let fromStart = 1;
95
- const fromEnd = from.length;
96
- const fromLen = fromEnd - fromStart;
97
- let toStart = 1;
98
- const toLen = to.length - toStart;
99
- const length = fromLen < toLen ? fromLen : toLen;
100
- let lastCommonSep = -1;
101
- let i = 0;
102
- for (; i <= length; ++i) {
103
- if (i === length) {
104
- if (toLen > length) {
105
- if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
106
- return to.slice(toStart + i + 1);
107
- } else if (i === 0) {
108
- return to.slice(toStart + i);
109
- }
110
- } else if (fromLen > length) {
111
- if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
112
- lastCommonSep = i;
113
- } else if (i === 0) {
114
- lastCommonSep = 0;
115
- }
116
- }
117
- break;
118
- }
119
- const fromCode = from.charCodeAt(fromStart + i);
120
- const toCode = to.charCodeAt(toStart + i);
121
- if (fromCode !== toCode) break;
122
- if (fromCode === CHAR_FORWARD_SLASH) lastCommonSep = i;
123
- }
124
- let out = "";
125
- for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
126
- if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
127
- if (out.length === 0) {
128
- out += "..";
129
- } else {
130
- out += "/..";
131
- }
132
- }
133
- }
134
- if (out.length > 0) {
135
- return out + to.slice(toStart + lastCommonSep);
136
- }
137
- toStart += lastCommonSep;
138
- if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
139
- ++toStart;
140
- }
141
- return to.slice(toStart);
100
+ assertPath(from);
101
+ assertPath(to);
102
+ if (from === to) return "";
103
+ from = resolve(from);
104
+ to = resolve(to);
105
+ if (from === to) return "";
106
+ let fromStart = 1;
107
+ const fromEnd = from.length;
108
+ const fromLen = fromEnd - fromStart;
109
+ let toStart = 1;
110
+ const toLen = to.length - toStart;
111
+ const length = fromLen < toLen ? fromLen : toLen;
112
+ let lastCommonSep = -1;
113
+ let i = 0;
114
+ for (; i <= length; ++i) {
115
+ if (i === length) {
116
+ if (toLen > length) {
117
+ if (to.charCodeAt(toStart + i) === 47) {
118
+ return to.slice(toStart + i + 1);
119
+ } else if (i === 0) {
120
+ return to.slice(toStart + i);
121
+ }
122
+ } else if (fromLen > length) {
123
+ if (from.charCodeAt(fromStart + i) === 47) {
124
+ lastCommonSep = i;
125
+ } else if (i === 0) {
126
+ lastCommonSep = 0;
127
+ }
128
+ }
129
+ break;
130
+ }
131
+ const fromCode = from.charCodeAt(fromStart + i);
132
+ const toCode = to.charCodeAt(toStart + i);
133
+ if (fromCode !== toCode) break;
134
+ if (fromCode === 47) lastCommonSep = i;
135
+ }
136
+ let out = "";
137
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
138
+ if (i === fromEnd || from.charCodeAt(i) === 47) {
139
+ if (out.length === 0) {
140
+ out += "..";
141
+ } else {
142
+ out += "/..";
143
+ }
144
+ }
145
+ }
146
+ if (out.length > 0) {
147
+ return out + to.slice(toStart + lastCommonSep);
148
+ }
149
+ toStart += lastCommonSep;
150
+ if (to.charCodeAt(toStart) === 47) {
151
+ ++toStart;
152
+ }
153
+ return to.slice(toStart);
142
154
  }
143
155
  function toNamespacedPath(path) {
144
- return path;
156
+ return path;
145
157
  }
146
158
  function dirname(path) {
147
- assertPath(path);
148
- if (path.length === 0) return ".";
149
- const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
150
- let end = -1;
151
- let matchedSlash = true;
152
- for (let i = path.length - 1; i >= 1; --i) {
153
- if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
154
- if (!matchedSlash) {
155
- end = i;
156
- break;
157
- }
158
- } else {
159
- matchedSlash = false;
160
- }
161
- }
162
- if (end === -1) return hasRoot ? "/" : ".";
163
- if (hasRoot && end === 1) return "//";
164
- return path.slice(0, end);
159
+ assertPath(path);
160
+ if (path.length === 0) return ".";
161
+ const hasRoot = path.charCodeAt(0) === 47;
162
+ let end = -1;
163
+ let matchedSlash = true;
164
+ for (let i = path.length - 1; i >= 1; --i) {
165
+ if (path.charCodeAt(i) === 47) {
166
+ if (!matchedSlash) {
167
+ end = i;
168
+ break;
169
+ }
170
+ } else {
171
+ matchedSlash = false;
172
+ }
173
+ }
174
+ if (end === -1) return hasRoot ? "/" : ".";
175
+ if (hasRoot && end === 1) return "//";
176
+ return path.slice(0, end);
165
177
  }
166
178
  function basename(path, ext) {
167
- if (ext !== void 0) assertPath(ext);
168
- assertPath(path);
169
- let start = 0;
170
- let end = -1;
171
- let matchedSlash = true;
172
- if (ext !== void 0 && ext.length > 0 && ext.length <= path.length) {
173
- if (ext.length === path.length && ext === path) return "";
174
- let extIdx = ext.length - 1;
175
- let firstNonSlashEnd = -1;
176
- for (let i = path.length - 1; i >= 0; --i) {
177
- const code = path.charCodeAt(i);
178
- if (code === CHAR_FORWARD_SLASH) {
179
- if (!matchedSlash) {
180
- start = i + 1;
181
- break;
182
- }
183
- } else {
184
- if (firstNonSlashEnd === -1) {
185
- matchedSlash = false;
186
- firstNonSlashEnd = i + 1;
187
- }
188
- if (extIdx >= 0) {
189
- if (code === ext.charCodeAt(extIdx)) {
190
- if (--extIdx === -1) {
191
- end = i;
192
- }
193
- } else {
194
- extIdx = -1;
195
- end = firstNonSlashEnd;
196
- }
197
- }
198
- }
199
- }
200
- if (start === end) end = firstNonSlashEnd;
201
- else if (end === -1) end = path.length;
202
- return path.slice(start, end);
203
- } else {
204
- for (let i = path.length - 1; i >= 0; --i) {
205
- if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
206
- if (!matchedSlash) {
207
- start = i + 1;
208
- break;
209
- }
210
- } else if (end === -1) {
211
- matchedSlash = false;
212
- end = i + 1;
213
- }
214
- }
215
- if (end === -1) return "";
216
- return path.slice(start, end);
217
- }
179
+ if (ext !== undefined) assertPath(ext);
180
+ assertPath(path);
181
+ let start = 0;
182
+ let end = -1;
183
+ let matchedSlash = true;
184
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
185
+ if (ext.length === path.length && ext === path) return "";
186
+ let extIdx = ext.length - 1;
187
+ let firstNonSlashEnd = -1;
188
+ for (let i = path.length - 1; i >= 0; --i) {
189
+ const code = path.charCodeAt(i);
190
+ if (code === 47) {
191
+ if (!matchedSlash) {
192
+ start = i + 1;
193
+ break;
194
+ }
195
+ } else {
196
+ if (firstNonSlashEnd === -1) {
197
+ matchedSlash = false;
198
+ firstNonSlashEnd = i + 1;
199
+ }
200
+ if (extIdx >= 0) {
201
+ if (code === ext.charCodeAt(extIdx)) {
202
+ if (--extIdx === -1) {
203
+ end = i;
204
+ }
205
+ } else {
206
+ extIdx = -1;
207
+ end = firstNonSlashEnd;
208
+ }
209
+ }
210
+ }
211
+ }
212
+ if (start === end) end = firstNonSlashEnd;
213
+ else if (end === -1) end = path.length;
214
+ return path.slice(start, end);
215
+ } else {
216
+ for (let i = path.length - 1; i >= 0; --i) {
217
+ if (path.charCodeAt(i) === 47) {
218
+ if (!matchedSlash) {
219
+ start = i + 1;
220
+ break;
221
+ }
222
+ } else if (end === -1) {
223
+ matchedSlash = false;
224
+ end = i + 1;
225
+ }
226
+ }
227
+ if (end === -1) return "";
228
+ return path.slice(start, end);
229
+ }
218
230
  }
219
231
  function extname(path) {
220
- assertPath(path);
221
- let startDot = -1;
222
- let startPart = 0;
223
- let end = -1;
224
- let matchedSlash = true;
225
- let preDotState = 0;
226
- for (let i = path.length - 1; i >= 0; --i) {
227
- const code = path.charCodeAt(i);
228
- if (code === CHAR_FORWARD_SLASH) {
229
- if (!matchedSlash) {
230
- startPart = i + 1;
231
- break;
232
- }
233
- continue;
234
- }
235
- if (end === -1) {
236
- matchedSlash = false;
237
- end = i + 1;
238
- }
239
- if (code === CHAR_DOT) {
240
- if (startDot === -1) {
241
- startDot = i;
242
- } else if (preDotState !== 1) {
243
- preDotState = 1;
244
- }
245
- } else if (startDot !== -1) {
246
- preDotState = -1;
247
- }
248
- }
249
- if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
250
- return "";
251
- }
252
- return path.slice(startDot, end);
232
+ assertPath(path);
233
+ let startDot = -1;
234
+ let startPart = 0;
235
+ let end = -1;
236
+ let matchedSlash = true;
237
+ let preDotState = 0;
238
+ for (let i = path.length - 1; i >= 0; --i) {
239
+ const code = path.charCodeAt(i);
240
+ if (code === 47) {
241
+ if (!matchedSlash) {
242
+ startPart = i + 1;
243
+ break;
244
+ }
245
+ continue;
246
+ }
247
+ if (end === -1) {
248
+ matchedSlash = false;
249
+ end = i + 1;
250
+ }
251
+ if (code === 46) {
252
+ if (startDot === -1) {
253
+ startDot = i;
254
+ } else if (preDotState !== 1) {
255
+ preDotState = 1;
256
+ }
257
+ } else if (startDot !== -1) {
258
+ preDotState = -1;
259
+ }
260
+ }
261
+ if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
262
+ return "";
263
+ }
264
+ return path.slice(startDot, end);
253
265
  }
254
266
  function format(pathObject) {
255
- if (pathObject === null || typeof pathObject !== "object") {
256
- throw new TypeError(
257
- 'The "pathObject" argument must be of type Object. Received type ' + typeof pathObject
258
- );
259
- }
260
- return _format("/", pathObject);
267
+ if (pathObject === null || typeof pathObject !== "object") {
268
+ throw new TypeError("The \"pathObject\" argument must be of type Object. Received type " + typeof pathObject);
269
+ }
270
+ return _format("/", pathObject);
261
271
  }
262
272
  function parse(path) {
263
- assertPath(path);
264
- const ret = { root: "", dir: "", base: "", ext: "", name: "" };
265
- if (path.length === 0) return ret;
266
- const isAbsolutePath = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
267
- let start;
268
- if (isAbsolutePath) {
269
- ret.root = "/";
270
- start = 1;
271
- } else {
272
- start = 0;
273
- }
274
- let startDot = -1;
275
- let startPart = 0;
276
- let end = -1;
277
- let matchedSlash = true;
278
- let i = path.length - 1;
279
- let preDotState = 0;
280
- for (; i >= start; --i) {
281
- const code = path.charCodeAt(i);
282
- if (code === CHAR_FORWARD_SLASH) {
283
- if (!matchedSlash) {
284
- startPart = i + 1;
285
- break;
286
- }
287
- continue;
288
- }
289
- if (end === -1) {
290
- matchedSlash = false;
291
- end = i + 1;
292
- }
293
- if (code === CHAR_DOT) {
294
- if (startDot === -1) startDot = i;
295
- else if (preDotState !== 1) preDotState = 1;
296
- } else if (startDot !== -1) {
297
- preDotState = -1;
298
- }
299
- }
300
- if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
301
- if (end !== -1) {
302
- if (startPart === 0 && isAbsolutePath) {
303
- ret.base = ret.name = path.slice(1, end);
304
- } else {
305
- ret.base = ret.name = path.slice(startPart, end);
306
- }
307
- }
308
- } else {
309
- if (startPart === 0 && isAbsolutePath) {
310
- ret.name = path.slice(1, startDot);
311
- ret.base = path.slice(1, end);
312
- } else {
313
- ret.name = path.slice(startPart, startDot);
314
- ret.base = path.slice(startPart, end);
315
- }
316
- ret.ext = path.slice(startDot, end);
317
- }
318
- if (startPart > 0) {
319
- ret.dir = path.slice(0, startPart - 1);
320
- } else if (isAbsolutePath) {
321
- ret.dir = "/";
322
- }
323
- return ret;
273
+ assertPath(path);
274
+ const ret = {
275
+ root: "",
276
+ dir: "",
277
+ base: "",
278
+ ext: "",
279
+ name: ""
280
+ };
281
+ if (path.length === 0) return ret;
282
+ const isAbsolutePath = path.charCodeAt(0) === 47;
283
+ let start;
284
+ if (isAbsolutePath) {
285
+ ret.root = "/";
286
+ start = 1;
287
+ } else {
288
+ start = 0;
289
+ }
290
+ let startDot = -1;
291
+ let startPart = 0;
292
+ let end = -1;
293
+ let matchedSlash = true;
294
+ let i = path.length - 1;
295
+ let preDotState = 0;
296
+ for (; i >= start; --i) {
297
+ const code = path.charCodeAt(i);
298
+ if (code === 47) {
299
+ if (!matchedSlash) {
300
+ startPart = i + 1;
301
+ break;
302
+ }
303
+ continue;
304
+ }
305
+ if (end === -1) {
306
+ matchedSlash = false;
307
+ end = i + 1;
308
+ }
309
+ if (code === 46) {
310
+ if (startDot === -1) startDot = i;
311
+ else if (preDotState !== 1) preDotState = 1;
312
+ } else if (startDot !== -1) {
313
+ preDotState = -1;
314
+ }
315
+ }
316
+ if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
317
+ if (end !== -1) {
318
+ if (startPart === 0 && isAbsolutePath) {
319
+ ret.base = ret.name = path.slice(1, end);
320
+ } else {
321
+ ret.base = ret.name = path.slice(startPart, end);
322
+ }
323
+ }
324
+ } else {
325
+ if (startPart === 0 && isAbsolutePath) {
326
+ ret.name = path.slice(1, startDot);
327
+ ret.base = path.slice(1, end);
328
+ } else {
329
+ ret.name = path.slice(startPart, startDot);
330
+ ret.base = path.slice(startPart, end);
331
+ }
332
+ ret.ext = path.slice(startDot, end);
333
+ }
334
+ if (startPart > 0) {
335
+ ret.dir = path.slice(0, startPart - 1);
336
+ } else if (isAbsolutePath) {
337
+ ret.dir = "/";
338
+ }
339
+ return ret;
324
340
  }
325
- export {
326
- basename,
327
- delimiter,
328
- dirname,
329
- extname,
330
- format,
331
- isAbsolute,
332
- join,
333
- normalize,
334
- parse,
335
- relative,
336
- resolve,
337
- sep,
338
- toNamespacedPath
339
- };
341
+
342
+ //#endregion
343
+ export { basename, delimiter, dirname, extname, format, isAbsolute, join, normalize, parse, posix_exports, relative, resolve, sep, toNamespacedPath };