@gjsify/path 0.0.1 → 0.1.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/README.md CHANGED
@@ -1,3 +1,27 @@
1
- # @gjsify/path [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC)
1
+ # @gjsify/path
2
2
 
3
- path core module for [gjs](https://gitlab.gnome.org/GNOME/gjs) forked from [@cgjs/path](https://github.com/cgjs/cgjs/tree/master/packages/path)
3
+ GJS implementation of the Node.js `path` module. Provides full POSIX and Win32 path utilities.
4
+
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/path
11
+ # or
12
+ yarn add @gjsify/path
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { join, resolve, basename, dirname, extname } from '@gjsify/path';
19
+
20
+ console.log(join('/home', 'user', 'file.txt'));
21
+ console.log(resolve('relative/path'));
22
+ console.log(basename('/home/user/file.txt')); // 'file.txt'
23
+ ```
24
+
25
+ ## License
26
+
27
+ MIT
@@ -0,0 +1,20 @@
1
+ const CHAR_DOT = 46;
2
+ const CHAR_FORWARD_SLASH = 47;
3
+ const CHAR_BACKWARD_SLASH = 92;
4
+ const CHAR_COLON = 58;
5
+ const CHAR_QUESTION_MARK = 63;
6
+ const CHAR_LOWERCASE_A = 97;
7
+ const CHAR_LOWERCASE_Z = 122;
8
+ const CHAR_UPPERCASE_A = 65;
9
+ const CHAR_UPPERCASE_Z = 90;
10
+ export {
11
+ CHAR_BACKWARD_SLASH,
12
+ CHAR_COLON,
13
+ CHAR_DOT,
14
+ CHAR_FORWARD_SLASH,
15
+ CHAR_LOWERCASE_A,
16
+ CHAR_LOWERCASE_Z,
17
+ CHAR_QUESTION_MARK,
18
+ CHAR_UPPERCASE_A,
19
+ CHAR_UPPERCASE_Z
20
+ };
@@ -0,0 +1,52 @@
1
+ import * as posix from "./posix.js";
2
+ import * as win32 from "./win32.js";
3
+ const {
4
+ resolve,
5
+ normalize,
6
+ isAbsolute,
7
+ join,
8
+ relative,
9
+ toNamespacedPath,
10
+ dirname,
11
+ basename,
12
+ extname,
13
+ format,
14
+ parse,
15
+ sep,
16
+ delimiter
17
+ } = posix;
18
+ var index_default = {
19
+ resolve: posix.resolve,
20
+ normalize: posix.normalize,
21
+ isAbsolute: posix.isAbsolute,
22
+ join: posix.join,
23
+ relative: posix.relative,
24
+ toNamespacedPath: posix.toNamespacedPath,
25
+ dirname: posix.dirname,
26
+ basename: posix.basename,
27
+ extname: posix.extname,
28
+ format: posix.format,
29
+ parse: posix.parse,
30
+ sep: posix.sep,
31
+ delimiter: posix.delimiter,
32
+ posix,
33
+ win32
34
+ };
35
+ export {
36
+ basename,
37
+ index_default as default,
38
+ delimiter,
39
+ dirname,
40
+ extname,
41
+ format,
42
+ isAbsolute,
43
+ join,
44
+ normalize,
45
+ parse,
46
+ posix,
47
+ relative,
48
+ resolve,
49
+ sep,
50
+ toNamespacedPath,
51
+ win32
52
+ };
@@ -0,0 +1,339 @@
1
+ import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./constants.js";
2
+ import {
3
+ assertPath,
4
+ isPosixPathSeparator,
5
+ normalizeString,
6
+ _format
7
+ } from "./util.js";
8
+ const sep = "/";
9
+ const delimiter = ":";
10
+ 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 "/";
22
+ }
23
+ 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 ".";
48
+ }
49
+ 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;
65
+ }
66
+ function isAbsolute(path) {
67
+ assertPath(path);
68
+ return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
69
+ }
70
+ 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);
86
+ }
87
+ 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);
142
+ }
143
+ function toNamespacedPath(path) {
144
+ return path;
145
+ }
146
+ 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);
165
+ }
166
+ 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
+ }
218
+ }
219
+ 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);
253
+ }
254
+ 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);
261
+ }
262
+ 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;
324
+ }
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
+ };
@@ -0,0 +1,113 @@
1
+ import {
2
+ CHAR_DOT,
3
+ CHAR_FORWARD_SLASH,
4
+ CHAR_BACKWARD_SLASH
5
+ } from "./constants.js";
6
+ function assertPath(path) {
7
+ if (typeof path !== "string") {
8
+ throw new TypeError(
9
+ 'The "path" argument must be of type string. Received type ' + typeof path
10
+ );
11
+ }
12
+ }
13
+ function isPosixPathSeparator(code) {
14
+ return code === CHAR_FORWARD_SLASH;
15
+ }
16
+ function isPathSeparator(code) {
17
+ return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
18
+ }
19
+ function isWindowsDeviceRoot(code) {
20
+ return code >= 65 && code <= 90 || // A-Z
21
+ code >= 97 && code <= 122;
22
+ }
23
+ function normalizeString(path, allowAboveRoot, separator, isPathSep) {
24
+ let res = "";
25
+ let lastSegmentLength = 0;
26
+ let lastSlash = -1;
27
+ let dots = 0;
28
+ let code;
29
+ for (let i = 0; i <= path.length; ++i) {
30
+ if (i < path.length) {
31
+ code = path.charCodeAt(i);
32
+ } else if (isPathSep(code)) {
33
+ break;
34
+ } else {
35
+ code = CHAR_FORWARD_SLASH;
36
+ }
37
+ if (isPathSep(code)) {
38
+ if (lastSlash === i - 1 || dots === 1) {
39
+ } else if (lastSlash !== i - 1 && dots === 2) {
40
+ if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== CHAR_DOT || res.charCodeAt(res.length - 2) !== CHAR_DOT) {
41
+ if (res.length > 2) {
42
+ const lastSlashIndex = res.lastIndexOf(separator);
43
+ if (lastSlashIndex === -1) {
44
+ res = "";
45
+ lastSegmentLength = 0;
46
+ } else {
47
+ res = res.slice(0, lastSlashIndex);
48
+ lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
49
+ }
50
+ lastSlash = i;
51
+ dots = 0;
52
+ continue;
53
+ } else if (res.length === 2 || res.length === 1) {
54
+ res = "";
55
+ lastSegmentLength = 0;
56
+ lastSlash = i;
57
+ dots = 0;
58
+ continue;
59
+ }
60
+ }
61
+ if (allowAboveRoot) {
62
+ if (res.length > 0) {
63
+ res += `${separator}..`;
64
+ } else {
65
+ res = "..";
66
+ }
67
+ lastSegmentLength = 2;
68
+ }
69
+ } else {
70
+ if (res.length > 0) {
71
+ res += separator + path.slice(lastSlash + 1, i);
72
+ } else {
73
+ res = path.slice(lastSlash + 1, i);
74
+ }
75
+ lastSegmentLength = i - lastSlash - 1;
76
+ }
77
+ lastSlash = i;
78
+ dots = 0;
79
+ } else if (code === CHAR_DOT && dots !== -1) {
80
+ ++dots;
81
+ } else {
82
+ dots = -1;
83
+ }
84
+ }
85
+ return res;
86
+ }
87
+ function _format(sep, pathObject) {
88
+ if (pathObject === null || typeof pathObject !== "object") {
89
+ throw new TypeError(
90
+ 'The "pathObject" argument must be of type Object. Received type ' + typeof pathObject
91
+ );
92
+ }
93
+ const dir = pathObject.dir || pathObject.root;
94
+ const base = pathObject.base || (pathObject.name || "") + formatExt(pathObject.ext);
95
+ if (!dir) {
96
+ return base;
97
+ }
98
+ if (dir === pathObject.root) {
99
+ return dir + base;
100
+ }
101
+ return dir + sep + base;
102
+ }
103
+ function formatExt(ext) {
104
+ return ext ? `${ext[0] === "." ? "" : "."}${ext}` : "";
105
+ }
106
+ export {
107
+ _format,
108
+ assertPath,
109
+ isPathSeparator,
110
+ isPosixPathSeparator,
111
+ isWindowsDeviceRoot,
112
+ normalizeString
113
+ };