@stryke/path 0.5.1 → 0.6.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.
@@ -6,25 +6,28 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.correctPath = correctPath;
7
7
  exports.normalizeString = normalizeString;
8
8
  exports.normalizeWindowsPath = normalizeWindowsPath;
9
+ exports.toAbsolutePath = toAbsolutePath;
10
+ exports.toRelativePath = toRelativePath;
9
11
  var _isFile = require("./is-file.cjs");
12
+ var _joinPaths = require("./join-paths.cjs");
10
13
  var _regex = require("./regex.cjs");
11
14
  var _slash = require("./slash.cjs");
12
15
  function normalizeWindowsPath(e = "") {
13
- return e && (0, _slash.slash)(e).replace(_regex.DRIVE_LETTER_START_REGEX, s => s.toUpperCase());
16
+ return e && (0, _slash.slash)(e).replace(_regex.DRIVE_LETTER_START_REGEX, n => n.toUpperCase());
14
17
  }
15
18
  function correctPath(e) {
16
19
  if (!e || e.length === 0) return ".";
17
20
  e = normalizeWindowsPath(e);
18
- const s = e.match(_regex.UNC_REGEX),
19
- n = (0, _isFile.isAbsolutePath)(e),
20
- l = e.endsWith("/");
21
- return e = normalizeString(e, !n), e.length === 0 ? n ? "/" : l ? "./" : "." : (l && (e += "/"), _regex.DRIVE_LETTER_REGEX.test(e) && (e += "/"), s ? n ? `//${e}` : `//./${e}` : !e.startsWith("/") && n && !_regex.DRIVE_LETTER_REGEX.test(e) ? `/${e}` : e);
21
+ const n = e.match(_regex.UNC_REGEX),
22
+ r = (0, _isFile.isAbsolutePath)(e),
23
+ i = e.endsWith("/");
24
+ return e = normalizeString(e, !r), e.length === 0 ? r ? "/" : i ? "./" : "." : (i && (e += "/"), _regex.DRIVE_LETTER_REGEX.test(e) && (e += "/"), n ? r ? `//${e}` : `//./${e}` : !e.startsWith("/") && r && !_regex.DRIVE_LETTER_REGEX.test(e) ? `/${e}` : e);
22
25
  }
23
- function normalizeString(e, s) {
24
- let n = "",
25
- l = 0,
26
- i = -1,
27
- r = 0,
26
+ function normalizeString(e, n) {
27
+ let r = "",
28
+ i = 0,
29
+ l = -1,
30
+ s = 0,
28
31
  o = null;
29
32
  for (let t = 0; t <= e.length; ++t) {
30
33
  if (t < e.length) o = e[t];else {
@@ -32,21 +35,27 @@ function normalizeString(e, s) {
32
35
  o = "/";
33
36
  }
34
37
  if (o === "/") {
35
- if (!(i === t - 1 || r === 1)) if (r === 2) {
36
- if (n.length < 2 || l !== 2 || n[n.length - 1] !== "." || n[n.length - 2] !== ".") {
37
- if (n.length > 2) {
38
- const f = n.lastIndexOf("/");
39
- f === -1 ? (n = "", l = 0) : (n = n.slice(0, f), l = n.length - 1 - n.lastIndexOf("/")), i = t, r = 0;
38
+ if (!(l === t - 1 || s === 1)) if (s === 2) {
39
+ if (r.length < 2 || i !== 2 || r[r.length - 1] !== "." || r[r.length - 2] !== ".") {
40
+ if (r.length > 2) {
41
+ const u = r.lastIndexOf("/");
42
+ u === -1 ? (r = "", i = 0) : (r = r.slice(0, u), i = r.length - 1 - r.lastIndexOf("/")), l = t, s = 0;
40
43
  continue;
41
- } else if (n.length > 0) {
42
- n = "", l = 0, i = t, r = 0;
44
+ } else if (r.length > 0) {
45
+ r = "", i = 0, l = t, s = 0;
43
46
  continue;
44
47
  }
45
48
  }
46
- s && (n += n.length > 0 ? "/.." : "..", l = 2);
47
- } else n.length > 0 ? n += `/${e.slice(i + 1, t)}` : n = e.slice(i + 1, t), l = t - i - 1;
48
- i = t, r = 0;
49
- } else o === "." && r !== -1 ? ++r : r = -1;
49
+ n && (r += r.length > 0 ? "/.." : "..", i = 2);
50
+ } else r.length > 0 ? r += `/${e.slice(l + 1, t)}` : r = e.slice(l + 1, t), i = t - l - 1;
51
+ l = t, s = 0;
52
+ } else o === "." && s !== -1 ? ++s : s = -1;
50
53
  }
51
- return n;
54
+ return r;
55
+ }
56
+ function toAbsolutePath(e, n) {
57
+ return (0, _isFile.isAbsolutePath)(e) ? e : (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(n || process.cwd(), e), !0));
58
+ }
59
+ function toRelativePath(e, n) {
60
+ return !e || e.length === 0 ? "." : ((0, _isFile.isAbsolutePath)(e) ? e = (0, _slash.slash)(normalizeString(e, !0)) : e = (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(n || process.cwd(), e), !0)), e.startsWith("./") ? e.slice(2) : e);
52
61
  }
@@ -14,3 +14,19 @@ export declare function correctPath(path?: string): string;
14
14
  * @returns the normalize path string.
15
15
  */
16
16
  export declare function normalizeString(path: string, allowAboveRoot: boolean): string;
17
+ /**
18
+ * Converts a given path to an absolute path based on the current working directory.
19
+ *
20
+ * @param path - The path to convert to an absolute path.
21
+ * @param cwd - The current working directory to use as the base path if the path is not absolute.
22
+ * @returns The absolute path.
23
+ */
24
+ export declare function toAbsolutePath(path: string, cwd?: string): string;
25
+ /**
26
+ * Converts a given path to a relative path based on the current working directory.
27
+ *
28
+ * @param path - The path to convert to a relative path.
29
+ * @param cwd - The current working directory to use as the base path if the path is not absolute.
30
+ * @returns The relative path.
31
+ */
32
+ export declare function toRelativePath(path: string, cwd?: string): string;
@@ -1 +1 @@
1
- import{isAbsolutePath as c}from"./is-file";import{DRIVE_LETTER_REGEX as g,DRIVE_LETTER_START_REGEX as u,UNC_REGEX as E}from"./regex";import{slash as m}from"./slash";export function normalizeWindowsPath(e=""){return e&&m(e).replace(u,s=>s.toUpperCase())}export function correctPath(e){if(!e||e.length===0)return".";e=normalizeWindowsPath(e);const s=e.match(E),n=c(e),l=e.endsWith("/");return e=normalizeString(e,!n),e.length===0?n?"/":l?"./":".":(l&&(e+="/"),g.test(e)&&(e+="/"),s?n?`//${e}`:`//./${e}`:!e.startsWith("/")&&n&&!g.test(e)?`/${e}`:e)}export function normalizeString(e,s){let n="",l=0,i=-1,r=0,o=null;for(let t=0;t<=e.length;++t){if(t<e.length)o=e[t];else{if(o==="/")break;o="/"}if(o==="/"){if(!(i===t-1||r===1))if(r===2){if(n.length<2||l!==2||n[n.length-1]!=="."||n[n.length-2]!=="."){if(n.length>2){const f=n.lastIndexOf("/");f===-1?(n="",l=0):(n=n.slice(0,f),l=n.length-1-n.lastIndexOf("/")),i=t,r=0;continue}else if(n.length>0){n="",l=0,i=t,r=0;continue}}s&&(n+=n.length>0?"/..":"..",l=2)}else n.length>0?n+=`/${e.slice(i+1,t)}`:n=e.slice(i+1,t),l=t-i-1;i=t,r=0}else o==="."&&r!==-1?++r:r=-1}return n}
1
+ import{isAbsolutePath as g}from"./is-file";import{joinPaths as c}from"./join-paths";import{DRIVE_LETTER_REGEX as m,DRIVE_LETTER_START_REGEX as E,UNC_REGEX as d}from"./regex";import{slash as f}from"./slash";export function normalizeWindowsPath(e=""){return e&&f(e).replace(E,n=>n.toUpperCase())}export function correctPath(e){if(!e||e.length===0)return".";e=normalizeWindowsPath(e);const n=e.match(d),r=g(e),i=e.endsWith("/");return e=normalizeString(e,!r),e.length===0?r?"/":i?"./":".":(i&&(e+="/"),m.test(e)&&(e+="/"),n?r?`//${e}`:`//./${e}`:!e.startsWith("/")&&r&&!m.test(e)?`/${e}`:e)}export function normalizeString(e,n){let r="",i=0,l=-1,s=0,o=null;for(let t=0;t<=e.length;++t){if(t<e.length)o=e[t];else{if(o==="/")break;o="/"}if(o==="/"){if(!(l===t-1||s===1))if(s===2){if(r.length<2||i!==2||r[r.length-1]!=="."||r[r.length-2]!=="."){if(r.length>2){const u=r.lastIndexOf("/");u===-1?(r="",i=0):(r=r.slice(0,u),i=r.length-1-r.lastIndexOf("/")),l=t,s=0;continue}else if(r.length>0){r="",i=0,l=t,s=0;continue}}n&&(r+=r.length>0?"/..":"..",i=2)}else r.length>0?r+=`/${e.slice(l+1,t)}`:r=e.slice(l+1,t),i=t-l-1;l=t,s=0}else o==="."&&s!==-1?++s:s=-1}return r}export function toAbsolutePath(e,n){return g(e)?e:f(normalizeString(c(n||process.cwd(),e),!0))}export function toRelativePath(e,n){return!e||e.length===0?".":(g(e)?e=f(normalizeString(e,!0)):e=f(normalizeString(c(n||process.cwd(),e),!0)),e.startsWith("./")?e.slice(2):e)}
package/dist/slash.cjs CHANGED
@@ -5,5 +5,5 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.slash = slash;
7
7
  function slash(r) {
8
- return r.replace(/\\/g, "/");
8
+ return r.startsWith("\\\\?\\") ? r : r.replace(/\\/g, "/");
9
9
  }
package/dist/slash.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Replace backslash to slash
3
3
  *
4
- * @param str - The string to replace
4
+ * @param path - The string to replace
5
5
  * @returns The string with replaced backslashes
6
6
  */
7
- export declare function slash(str: string): string;
7
+ export declare function slash(path: string): string;
package/dist/slash.mjs CHANGED
@@ -1 +1 @@
1
- export function slash(r){return r.replace(/\\/g,"/")}
1
+ export function slash(r){return r.startsWith("\\\\?\\")?r:r.replace(/\\/g,"/")}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "description": "A package containing various utilities that expand the functionality of NodeJs's built-in `path` module",
6
6
  "repository": {