@stryke/path 0.16.0 → 0.18.0

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,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.correctPath = correctPath;
7
7
  exports.normalizeString = normalizeString;
8
8
  exports.normalizeWindowsPath = normalizeWindowsPath;
9
+ exports.stripStars = stripStars;
9
10
  exports.toAbsolutePath = toAbsolutePath;
10
11
  exports.toRelativePath = toRelativePath;
11
12
  var _cwd = require("./cwd.cjs");
@@ -14,49 +15,55 @@ var _joinPaths = require("./join-paths.cjs");
14
15
  var _regex = require("./regex.cjs");
15
16
  var _slash = require("./slash.cjs");
16
17
  function normalizeWindowsPath(e = "") {
17
- return e && (0, _slash.slash)(e).replace(_regex.DRIVE_LETTER_START_REGEX, n => n.toUpperCase());
18
+ return e && (0, _slash.slash)(e).replace(_regex.DRIVE_LETTER_START_REGEX, t => t.toUpperCase());
18
19
  }
19
20
  function correctPath(e) {
20
21
  if (!e || e.length === 0) return ".";
21
22
  e = normalizeWindowsPath(e);
22
- const n = e.match(_regex.UNC_REGEX),
23
+ const t = e.match(_regex.UNC_REGEX),
23
24
  r = (0, _isType.isAbsolutePath)(e),
24
- i = e.endsWith("/");
25
- 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);
25
+ n = e.endsWith("/");
26
+ return e = normalizeString(e, !r), e.length === 0 ? r ? "/" : n ? "./" : "." : (n && (e += "/"), _regex.DRIVE_LETTER_REGEX.test(e) && (e += "/"), t ? r ? `//${e}` : `//./${e}` : !e.startsWith("/") && r && !_regex.DRIVE_LETTER_REGEX.test(e) ? `/${e}` : e);
26
27
  }
27
- function normalizeString(e, n) {
28
+ function stripStars(e) {
29
+ if (!e || e.length === 0) return ".";
30
+ e = correctPath(e);
31
+ let t = !1;
32
+ return e.split("/").reduce((r, n) => n?.trim() ? t || n.includes("*") ? (t = !0, r) : r + (r.length > 0 ? `/${n}` : n) : r, "");
33
+ }
34
+ function normalizeString(e, t) {
28
35
  let r = "",
29
- i = 0,
36
+ n = 0,
30
37
  l = -1,
31
38
  s = 0,
32
39
  o = null;
33
- for (let t = 0; t <= e.length; ++t) {
34
- if (t < e.length) o = e[t];else {
40
+ for (let i = 0; i <= e.length; ++i) {
41
+ if (i < e.length) o = e[i];else {
35
42
  if (o === "/") break;
36
43
  o = "/";
37
44
  }
38
45
  if (o === "/") {
39
- if (!(l === t - 1 || s === 1)) if (s === 2) {
40
- if (r.length < 2 || i !== 2 || r[r.length - 1] !== "." || r[r.length - 2] !== ".") {
46
+ if (!(l === i - 1 || s === 1)) if (s === 2) {
47
+ if (r.length < 2 || n !== 2 || r[r.length - 1] !== "." || r[r.length - 2] !== ".") {
41
48
  if (r.length > 2) {
42
- const g = r.lastIndexOf("/");
43
- g === -1 ? (r = "", i = 0) : (r = r.slice(0, g), i = r.length - 1 - r.lastIndexOf("/")), l = t, s = 0;
49
+ const c = r.lastIndexOf("/");
50
+ c === -1 ? (r = "", n = 0) : (r = r.slice(0, c), n = r.length - 1 - r.lastIndexOf("/")), l = i, s = 0;
44
51
  continue;
45
52
  } else if (r.length > 0) {
46
- r = "", i = 0, l = t, s = 0;
53
+ r = "", n = 0, l = i, s = 0;
47
54
  continue;
48
55
  }
49
56
  }
50
- n && (r += r.length > 0 ? "/.." : "..", i = 2);
51
- } else r.length > 0 ? r += `/${e.slice(l + 1, t)}` : r = e.slice(l + 1, t), i = t - l - 1;
52
- l = t, s = 0;
57
+ t && (r += r.length > 0 ? "/.." : "..", n = 2);
58
+ } else r.length > 0 ? r += `/${e.slice(l + 1, i)}` : r = e.slice(l + 1, i), n = i - l - 1;
59
+ l = i, s = 0;
53
60
  } else o === "." && s !== -1 ? ++s : s = -1;
54
61
  }
55
62
  return r;
56
63
  }
57
- function toAbsolutePath(e, n) {
58
- return (0, _isType.isAbsolutePath)(e) ? e : (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(n || process.cwd(), e), !0));
64
+ function toAbsolutePath(e, t) {
65
+ return (0, _isType.isAbsolutePath)(e) ? e : (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(t || process.cwd(), e), !0));
59
66
  }
60
- function toRelativePath(e, n = (0, _cwd.cwd)()) {
61
- return !e || e.length === 0 ? "." : ((0, _isType.isAbsolutePath)(e) ? e = (0, _slash.slash)(normalizeString(e, !0)) : e = (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(n, e), !0)), e.startsWith("./") ? e.slice(2) : e);
67
+ function toRelativePath(e, t = (0, _cwd.cwd)()) {
68
+ return !e || e.length === 0 ? "." : ((0, _isType.isAbsolutePath)(e) ? e = (0, _slash.slash)(normalizeString(e, !0)) : e = (0, _slash.slash)(normalizeString((0, _joinPaths.joinPaths)(t, e), !0)), e.startsWith("./") ? e.slice(2) : e);
62
69
  }
@@ -6,6 +6,22 @@ export declare function normalizeWindowsPath(input?: string): string;
6
6
  * @returns The corrected path.
7
7
  */
8
8
  export declare function correctPath(path?: string): string;
9
+ /**
10
+ * Remove any star tokens (*) from the end of the file path
11
+ *
12
+ * @example
13
+ * stripStars("src/**") // returns "src"
14
+ * stripStars("src/*") // returns "src"
15
+ * stripStars("src/**\/*") // returns "src"
16
+ * stripStars("src/**\/*.txt") // returns "src"
17
+ * stripStars("src/**\/file.txt") // returns "src"
18
+ * stripStars("src/file.txt") // returns "src/file.txt"
19
+ * stripStars("") // returns "."
20
+ *
21
+ * @param path - The path to correct.
22
+ * @returns The corrected path.
23
+ */
24
+ export declare function stripStars(path?: string): string;
9
25
  /**
10
26
  * Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.
11
27
  *
@@ -1 +1 @@
1
- import{cwd as E}from"./cwd";import{isAbsolutePath as u}from"./is-type";import{joinPaths as c}from"./join-paths";import{DRIVE_LETTER_REGEX as m,DRIVE_LETTER_START_REGEX as d,UNC_REGEX as x}from"./regex";import{slash as f}from"./slash";export function normalizeWindowsPath(e=""){return e&&f(e).replace(d,n=>n.toUpperCase())}export function correctPath(e){if(!e||e.length===0)return".";e=normalizeWindowsPath(e);const n=e.match(x),r=u(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 g=r.lastIndexOf("/");g===-1?(r="",i=0):(r=r.slice(0,g),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 u(e)?e:f(normalizeString(c(n||process.cwd(),e),!0))}export function toRelativePath(e,n=E()){return!e||e.length===0?".":(u(e)?e=f(normalizeString(e,!0)):e=f(normalizeString(c(n,e),!0)),e.startsWith("./")?e.slice(2):e)}
1
+ import{cwd as m}from"./cwd";import{isAbsolutePath as u}from"./is-type";import{joinPaths as g}from"./join-paths";import{DRIVE_LETTER_REGEX as d,DRIVE_LETTER_START_REGEX as E,UNC_REGEX as x}from"./regex";import{slash as f}from"./slash";export function normalizeWindowsPath(e=""){return e&&f(e).replace(E,t=>t.toUpperCase())}export function correctPath(e){if(!e||e.length===0)return".";e=normalizeWindowsPath(e);const t=e.match(x),r=u(e),n=e.endsWith("/");return e=normalizeString(e,!r),e.length===0?r?"/":n?"./":".":(n&&(e+="/"),d.test(e)&&(e+="/"),t?r?`//${e}`:`//./${e}`:!e.startsWith("/")&&r&&!d.test(e)?`/${e}`:e)}export function stripStars(e){if(!e||e.length===0)return".";e=correctPath(e);let t=!1;return e.split("/").reduce((r,n)=>n?.trim()?t||n.includes("*")?(t=!0,r):r+(r.length>0?`/${n}`:n):r,"")}export function normalizeString(e,t){let r="",n=0,l=-1,s=0,o=null;for(let i=0;i<=e.length;++i){if(i<e.length)o=e[i];else{if(o==="/")break;o="/"}if(o==="/"){if(!(l===i-1||s===1))if(s===2){if(r.length<2||n!==2||r[r.length-1]!=="."||r[r.length-2]!=="."){if(r.length>2){const c=r.lastIndexOf("/");c===-1?(r="",n=0):(r=r.slice(0,c),n=r.length-1-r.lastIndexOf("/")),l=i,s=0;continue}else if(r.length>0){r="",n=0,l=i,s=0;continue}}t&&(r+=r.length>0?"/..":"..",n=2)}else r.length>0?r+=`/${e.slice(l+1,i)}`:r=e.slice(l+1,i),n=i-l-1;l=i,s=0}else o==="."&&s!==-1?++s:s=-1}return r}export function toAbsolutePath(e,t){return u(e)?e:f(normalizeString(g(t||process.cwd(),e),!0))}export function toRelativePath(e,t=m()){return!e||e.length===0?".":(u(e)?e=f(normalizeString(e,!0)):e=f(normalizeString(g(t,e),!0)),e.startsWith("./")?e.slice(2):e)}
package/dist/replace.cjs CHANGED
@@ -3,10 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.replaceExtension = replaceExtension;
6
7
  exports.replacePath = replacePath;
8
+ var _correctPath = require("./correct-path.cjs");
7
9
  var _cwd = require("./cwd.cjs");
10
+ var _filePathFns = require("./file-path-fns.cjs");
8
11
  var _isParentPath = require("./is-parent-path.cjs");
9
12
  var _slash = require("./slash.cjs");
10
- function replacePath(r, e = (0, _cwd.cwd)()) {
11
- return (0, _isParentPath.isParentPath)(r, e) ? (0, _slash.slash)(r).replace((0, _slash.slash)(e), "").replace(/^\//, "") : r;
13
+ function replacePath(r, i = (0, _cwd.cwd)()) {
14
+ return (0, _correctPath.correctPath)((0, _isParentPath.isParentPath)(r, i) ? (0, _slash.slash)(r).replace((0, _slash.slash)(i), "").replace(/^\//, "") : r);
15
+ }
16
+ function replaceExtension(r, i = "") {
17
+ return (0, _correctPath.correctPath)(r.replace(!i || i.includes(".") ? (0, _filePathFns.findFileDotExtensionSafe)(r) : (0, _filePathFns.findFileExtensionSafe)(r), i));
12
18
  }
package/dist/replace.d.ts CHANGED
@@ -12,3 +12,19 @@
12
12
  * @returns The {@link childPath} with the {@link parentPath} path removed
13
13
  */
14
14
  export declare function replacePath(childPath: string, parentPath?: string): string;
15
+ /**
16
+ * Replace the extension of a given path with the provided value.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * replaceExtension("/home/user/project/src/index.ts", ".js");
21
+ * // returns "/home/user/project/src/index.js"
22
+ * replaceExtension("/home/user/project/src/index.ts");
23
+ * // returns "/home/user/project/src/index"
24
+ * ```
25
+ *
26
+ * @param path - The path that will have its current extension replaced
27
+ * @param replacement - The value (or an empty string) to replace the current extension with
28
+ * @returns The path with the replaced extension
29
+ */
30
+ export declare function replaceExtension(path: string, replacement?: string): string;
package/dist/replace.mjs CHANGED
@@ -1 +1 @@
1
- import{cwd as t}from"./cwd";import{isParentPath as i}from"./is-parent-path";import{slash as o}from"./slash";export function replacePath(r,e=t()){return i(r,e)?o(r).replace(o(e),"").replace(/^\//,""):r}
1
+ import{correctPath as o}from"./correct-path";import{cwd as e}from"./cwd";import{findFileDotExtensionSafe as t,findFileExtensionSafe as s}from"./file-path-fns";import{isParentPath as f}from"./is-parent-path";import{slash as n}from"./slash";export function replacePath(r,i=e()){return o(f(r,i)?n(r).replace(n(i),"").replace(/^\//,""):r)}export function replaceExtension(r,i=""){return o(r.replace(!i||i.includes(".")?t(r):s(r),i))}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
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": {
@@ -279,5 +279,5 @@
279
279
  "main": "./dist/index.cjs",
280
280
  "module": "./dist/index.mjs",
281
281
  "types": "./dist/index.d.ts",
282
- "gitHead": "c60091277e3643e8619c90a06a9e9aed326359c1"
282
+ "gitHead": "04107da5bbf439fe7e705783009e18fc5080a4fe"
283
283
  }