@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/util.js CHANGED
@@ -1,113 +1,106 @@
1
- import {
2
- CHAR_DOT,
3
- CHAR_FORWARD_SLASH,
4
- CHAR_BACKWARD_SLASH
5
- } from "./constants.js";
1
+ import { CHAR_BACKWARD_SLASH, CHAR_DOT, CHAR_FORWARD_SLASH } from "./constants.js";
2
+
3
+ //#region src/util.ts
6
4
  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
- }
5
+ if (typeof path !== "string") {
6
+ throw new TypeError("The \"path\" argument must be of type string. Received type " + typeof path);
7
+ }
12
8
  }
13
9
  function isPosixPathSeparator(code) {
14
- return code === CHAR_FORWARD_SLASH;
10
+ return code === 47;
15
11
  }
16
12
  function isPathSeparator(code) {
17
- return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
13
+ return code === 47 || code === 92;
18
14
  }
19
15
  function isWindowsDeviceRoot(code) {
20
- return code >= 65 && code <= 90 || // A-Z
21
- code >= 97 && code <= 122;
16
+ return code >= 65 && code <= 90 || code >= 97 && code <= 122;
22
17
  }
18
+ /**
19
+ * Resolves `.` and `..` segments in a path string.
20
+ */
23
21
  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;
22
+ let res = "";
23
+ let lastSegmentLength = 0;
24
+ let lastSlash = -1;
25
+ let dots = 0;
26
+ let code;
27
+ for (let i = 0; i <= path.length; ++i) {
28
+ if (i < path.length) {
29
+ code = path.charCodeAt(i);
30
+ } else if (isPathSep(code)) {
31
+ break;
32
+ } else {
33
+ code = 47;
34
+ }
35
+ if (isPathSep(code)) {
36
+ if (lastSlash === i - 1 || dots === 1) {} else if (lastSlash !== i - 1 && dots === 2) {
37
+ if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 || res.charCodeAt(res.length - 2) !== 46) {
38
+ if (res.length > 2) {
39
+ const lastSlashIndex = res.lastIndexOf(separator);
40
+ if (lastSlashIndex === -1) {
41
+ res = "";
42
+ lastSegmentLength = 0;
43
+ } else {
44
+ res = res.slice(0, lastSlashIndex);
45
+ lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
46
+ }
47
+ lastSlash = i;
48
+ dots = 0;
49
+ continue;
50
+ } else if (res.length === 2 || res.length === 1) {
51
+ res = "";
52
+ lastSegmentLength = 0;
53
+ lastSlash = i;
54
+ dots = 0;
55
+ continue;
56
+ }
57
+ }
58
+ if (allowAboveRoot) {
59
+ if (res.length > 0) {
60
+ res += `${separator}..`;
61
+ } else {
62
+ res = "..";
63
+ }
64
+ lastSegmentLength = 2;
65
+ }
66
+ } else {
67
+ if (res.length > 0) {
68
+ res += separator + path.slice(lastSlash + 1, i);
69
+ } else {
70
+ res = path.slice(lastSlash + 1, i);
71
+ }
72
+ lastSegmentLength = i - lastSlash - 1;
73
+ }
74
+ lastSlash = i;
75
+ dots = 0;
76
+ } else if (code === 46 && dots !== -1) {
77
+ ++dots;
78
+ } else {
79
+ dots = -1;
80
+ }
81
+ }
82
+ return res;
86
83
  }
84
+ /**
85
+ * Format a parsed path object into a path string.
86
+ */
87
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;
88
+ if (pathObject === null || typeof pathObject !== "object") {
89
+ throw new TypeError("The \"pathObject\" argument must be of type Object. Received type " + typeof pathObject);
90
+ }
91
+ const dir = pathObject.dir || pathObject.root;
92
+ const base = pathObject.base || (pathObject.name || "") + formatExt(pathObject.ext);
93
+ if (!dir) {
94
+ return base;
95
+ }
96
+ if (dir === pathObject.root) {
97
+ return dir + base;
98
+ }
99
+ return dir + sep + base;
102
100
  }
103
101
  function formatExt(ext) {
104
- return ext ? `${ext[0] === "." ? "" : "."}${ext}` : "";
102
+ return ext ? `${ext[0] === "." ? "" : "."}${ext}` : "";
105
103
  }
106
- export {
107
- _format,
108
- assertPath,
109
- isPathSeparator,
110
- isPosixPathSeparator,
111
- isWindowsDeviceRoot,
112
- normalizeString
113
- };
104
+
105
+ //#endregion
106
+ export { _format, assertPath, isPathSeparator, isPosixPathSeparator, isWindowsDeviceRoot, normalizeString };