@stryke/path 0.9.0 → 0.10.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.
@@ -4,11 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.findFileExtension = findFileExtension;
7
+ exports.findFileExtensionSafe = findFileExtensionSafe;
7
8
  exports.findFileName = findFileName;
8
9
  exports.findFilePath = findFilePath;
9
10
  exports.findFolderName = findFolderName;
11
+ exports.hasFileExtension = hasFileExtension;
10
12
  exports.hasFileName = hasFileName;
11
13
  exports.hasFilePath = hasFilePath;
14
+ exports.hasFolderName = hasFolderName;
12
15
  exports.parsePath = parsePath;
13
16
  exports.relativePath = relativePath;
14
17
  exports.relativeToWorkspaceRoot = relativeToWorkspaceRoot;
@@ -21,26 +24,26 @@ var _correctPath = require("./correct-path.cjs");
21
24
  var _getWorkspaceRoot = require("./get-workspace-root.cjs");
22
25
  var _isFile = require("./is-file.cjs");
23
26
  var _joinPaths = require("./join-paths.cjs");
24
- function findFileName(t, e = {}) {
27
+ function findFileName(e, t = {}) {
25
28
  const {
26
29
  requireExtension: n = !1,
27
30
  withExtension: r = !0
28
- } = e,
29
- i = (0, _correctPath.normalizeWindowsPath)(t)?.split(t?.includes("\\") ? "\\" : "/")?.pop() ?? "";
31
+ } = t,
32
+ i = (0, _correctPath.normalizeWindowsPath)(e)?.split(e?.includes("\\") ? "\\" : "/")?.pop() ?? "";
30
33
  return n === !0 && !i.includes(".") ? _base.EMPTY_STRING : r === !1 && i.includes(".") ? i.substring(0, i.lastIndexOf(".")) || _base.EMPTY_STRING : i;
31
34
  }
32
- function findFilePath(t) {
33
- const e = (0, _correctPath.normalizeWindowsPath)(t),
34
- n = e.replace(findFileName(e, {
35
+ function findFilePath(e) {
36
+ const t = (0, _correctPath.normalizeWindowsPath)(e),
37
+ n = t.replace(findFileName(t, {
35
38
  requireExtension: !0
36
39
  }), "");
37
40
  return n === "/" ? n : n.replace(/\/$/, "");
38
41
  }
39
- function findFolderName(t) {
40
- const e = findFilePath(t).split("/");
42
+ function findFolderName(e) {
43
+ const t = findFilePath(e).split("/");
41
44
  let n = "";
42
- for (let r = e.length - 1; r >= 0; r--) {
43
- const i = e[r];
45
+ for (let r = t.length - 1; r >= 0; r--) {
46
+ const i = t[r];
44
47
  if (i) {
45
48
  n = i;
46
49
  break;
@@ -48,53 +51,62 @@ function findFolderName(t) {
48
51
  }
49
52
  return n ?? _base.EMPTY_STRING;
50
53
  }
51
- function findFileExtension(t) {
52
- if (t === "..") return "";
53
- const e = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(t));
54
- return e && e[1] || _base.EMPTY_STRING;
54
+ function findFileExtension(e) {
55
+ if (e.endsWith(".") || e.endsWith("/")) return;
56
+ const t = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(e));
57
+ return t && t[1] || void 0;
55
58
  }
56
- function hasFileName(t) {
57
- return !!findFileName(t);
59
+ function findFileExtensionSafe(e) {
60
+ return findFileExtension(e) ?? _base.EMPTY_STRING;
58
61
  }
59
- function hasFilePath(t) {
60
- return !!findFilePath(t);
62
+ function hasFileName(e) {
63
+ return !!findFileName(e);
61
64
  }
62
- function resolvePath(t, e = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
63
- const n = (0, _correctPath.normalizeWindowsPath)(t).split("/");
65
+ function hasFilePath(e) {
66
+ return !!findFilePath(e);
67
+ }
68
+ function hasFolderName(e) {
69
+ return !!findFolderName(e);
70
+ }
71
+ function hasFileExtension(e) {
72
+ return !!findFileExtension(e);
73
+ }
74
+ function resolvePath(e, t = (0, _getWorkspaceRoot.getWorkspaceRoot)()) {
75
+ const n = (0, _correctPath.normalizeWindowsPath)(e).split("/");
64
76
  let r = "",
65
77
  i = !1;
66
78
  for (let s = n.length - 1; s >= -1 && !i; s--) {
67
- const o = s >= 0 ? n[s] : e;
79
+ const o = s >= 0 ? n[s] : t;
68
80
  !o || o.length === 0 || (r = (0, _joinPaths.joinPaths)(o, r), i = (0, _isFile.isAbsolutePath)(o));
69
81
  }
70
82
  return r = (0, _correctPath.normalizeString)(r, !i), i && !(0, _isFile.isAbsolutePath)(r) ? `/${r}` : r.length > 0 ? r : ".";
71
83
  }
72
- function resolvePaths(...t) {
73
- return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e))));
84
+ function resolvePaths(...e) {
85
+ return resolvePath((0, _joinPaths.joinPaths)(...e.map(t => (0, _correctPath.normalizeWindowsPath)(t))));
74
86
  }
75
- function relativePath(t, e, n = !1) {
76
- return (0, _nodePath.relative)(n !== !0 ? t.replace(/\/$/, "") : t, n !== !0 ? e.replace(/\/$/, "") : e);
87
+ function relativePath(e, t, n = !1) {
88
+ return (0, _nodePath.relative)(n !== !0 ? e.replace(/\/$/, "") : e, n !== !0 ? t.replace(/\/$/, "") : t);
77
89
  }
78
- function relativeToWorkspaceRoot(t) {
79
- return relativePath(t, (0, _getWorkspaceRoot.getWorkspaceRoot)());
90
+ function relativeToWorkspaceRoot(e) {
91
+ return relativePath(e, (0, _getWorkspaceRoot.getWorkspaceRoot)());
80
92
  }
81
- function parsePath(t) {
82
- const e = /^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g, "/") || "",
83
- n = (0, _correctPath.normalizeWindowsPath)(t),
93
+ function parsePath(e) {
94
+ const t = /^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g, "/") || "",
95
+ n = (0, _correctPath.normalizeWindowsPath)(e),
84
96
  r = n.replace(/\/$/, "").split("/").slice(0, -1);
85
97
  r.length === 1 && /^[A-Z]:$/i.test(r[0]) && (r[0] += "/");
86
98
  const i = findFolderName(n),
87
- s = r.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."),
88
- o = findFileExtension(t);
99
+ s = r.join("/") || ((0, _isFile.isAbsolutePath)(e) ? "/" : "."),
100
+ o = findFileExtensionSafe(e);
89
101
  return {
90
- root: e,
102
+ root: t,
91
103
  dir: s,
92
104
  base: i,
93
105
  ext: o,
94
106
  name: i.slice(0, i.length - o.length)
95
107
  };
96
108
  }
97
- function renameFile(t, e) {
98
- const n = parsePath(t);
99
- return (0, _joinPaths.joinPaths)(n.dir, e.includes(".") ? e : e + n.ext);
109
+ function renameFile(e, t) {
110
+ const n = parsePath(e);
111
+ return (0, _joinPaths.joinPaths)(n.dir, t.includes(".") ? t : t + n.ext);
100
112
  }
@@ -58,10 +58,27 @@ export declare function findFolderName(filePath: string): string;
58
58
  /**
59
59
  * Find the file extension from a file path.
60
60
  *
61
+ * @remarks
62
+ * The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
63
+ *
64
+ * The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
65
+ *
66
+ * @param filePath - The file path to process
67
+ * @returns The file extension or undefined if no extension is found
68
+ */
69
+ export declare function findFileExtension(filePath: string): string | undefined;
70
+ /**
71
+ * Find the file extension from a file path or an empty string.
72
+ *
73
+ * @remarks
74
+ * The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
75
+ *
76
+ * The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
77
+ *
61
78
  * @param filePath - The file path to process
62
- * @returns The file extension
79
+ * @returns The file extension or an empty string if no extension is found
63
80
  */
64
- export declare function findFileExtension(filePath: string): string;
81
+ export declare function findFileExtensionSafe(filePath: string): string;
65
82
  /**
66
83
  * Check if a file path has a file name.
67
84
  *
@@ -76,6 +93,20 @@ export declare function hasFileName(filePath: string): boolean;
76
93
  * @returns An indicator specifying if the file path has a file path
77
94
  */
78
95
  export declare function hasFilePath(filePath: string): boolean;
96
+ /**
97
+ * Check if a file path has a folder name.
98
+ *
99
+ * @param filePath - The file path to process
100
+ * @returns An indicator specifying if the file path has a folder name
101
+ */
102
+ export declare function hasFolderName(filePath: string): boolean;
103
+ /**
104
+ * Check if a file path has a file extension.
105
+ *
106
+ * @param filePath - The file path to process
107
+ * @returns An indicator specifying if the file path has a file extension
108
+ */
109
+ export declare function hasFileExtension(filePath: string): boolean;
79
110
  /**
80
111
  * Resolve the file path to an absolute path.
81
112
  *
@@ -1 +1 @@
1
- import{EMPTY_STRING as a}from"@stryke/types/base";import{relative as f}from"node:path";import{normalizeString as g,normalizeWindowsPath as l}from"./correct-path";import{getWorkspaceRoot as p}from"./get-workspace-root";import{isAbsolutePath as c}from"./is-file";import{joinPaths as u}from"./join-paths";export function findFileName(t,e={}){const{requireExtension:n=!1,withExtension:r=!0}=e,i=l(t)?.split(t?.includes("\\")?"\\":"/")?.pop()??"";return n===!0&&!i.includes(".")?a:r===!1&&i.includes(".")?i.substring(0,i.lastIndexOf("."))||a:i}export function findFilePath(t){const e=l(t),n=e.replace(findFileName(e,{requireExtension:!0}),"");return n==="/"?n:n.replace(/\/$/,"")}export function findFolderName(t){const e=findFilePath(t).split("/");let n="";for(let r=e.length-1;r>=0;r--){const i=e[r];if(i){n=i;break}}return n??a}export function findFileExtension(t){if(t==="..")return"";const e=/.(\.[^./]+|\.)$/.exec(l(t));return e&&e[1]||a}export function hasFileName(t){return!!findFileName(t)}export function hasFilePath(t){return!!findFilePath(t)}export function resolvePath(t,e=p()){const n=l(t).split("/");let r="",i=!1;for(let s=n.length-1;s>=-1&&!i;s--){const o=s>=0?n[s]:e;!o||o.length===0||(r=u(o,r),i=c(o))}return r=g(r,!i),i&&!c(r)?`/${r}`:r.length>0?r:"."}export function resolvePaths(...t){return resolvePath(u(...t.map(e=>l(e))))}export function relativePath(t,e,n=!1){return f(n!==!0?t.replace(/\/$/,""):t,n!==!0?e.replace(/\/$/,""):e)}export function relativeToWorkspaceRoot(t){return relativePath(t,p())}export function parsePath(t){const e=/^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g,"/")||"",n=l(t),r=n.replace(/\/$/,"").split("/").slice(0,-1);r.length===1&&/^[A-Z]:$/i.test(r[0])&&(r[0]+="/");const i=findFolderName(n),s=r.join("/")||(c(t)?"/":"."),o=findFileExtension(t);return{root:e,dir:s,base:i,ext:o,name:i.slice(0,i.length-o.length)}}export function renameFile(t,e){const n=parsePath(t);return u(n.dir,e.includes(".")?e:e+n.ext)}
1
+ import{EMPTY_STRING as a}from"@stryke/types/base";import{relative as p}from"node:path";import{normalizeString as g,normalizeWindowsPath as l}from"./correct-path";import{getWorkspaceRoot as f}from"./get-workspace-root";import{isAbsolutePath as u}from"./is-file";import{joinPaths as c}from"./join-paths";export function findFileName(e,t={}){const{requireExtension:n=!1,withExtension:r=!0}=t,i=l(e)?.split(e?.includes("\\")?"\\":"/")?.pop()??"";return n===!0&&!i.includes(".")?a:r===!1&&i.includes(".")?i.substring(0,i.lastIndexOf("."))||a:i}export function findFilePath(e){const t=l(e),n=t.replace(findFileName(t,{requireExtension:!0}),"");return n==="/"?n:n.replace(/\/$/,"")}export function findFolderName(e){const t=findFilePath(e).split("/");let n="";for(let r=t.length-1;r>=0;r--){const i=t[r];if(i){n=i;break}}return n??a}export function findFileExtension(e){if(e.endsWith(".")||e.endsWith("/"))return;const t=/.(\.[^./]+|\.)$/.exec(l(e));return t&&t[1]||void 0}export function findFileExtensionSafe(e){return findFileExtension(e)??a}export function hasFileName(e){return!!findFileName(e)}export function hasFilePath(e){return!!findFilePath(e)}export function hasFolderName(e){return!!findFolderName(e)}export function hasFileExtension(e){return!!findFileExtension(e)}export function resolvePath(e,t=f()){const n=l(e).split("/");let r="",i=!1;for(let s=n.length-1;s>=-1&&!i;s--){const o=s>=0?n[s]:t;!o||o.length===0||(r=c(o,r),i=u(o))}return r=g(r,!i),i&&!u(r)?`/${r}`:r.length>0?r:"."}export function resolvePaths(...e){return resolvePath(c(...e.map(t=>l(t))))}export function relativePath(e,t,n=!1){return p(n!==!0?e.replace(/\/$/,""):e,n!==!0?t.replace(/\/$/,""):t)}export function relativeToWorkspaceRoot(e){return relativePath(e,f())}export function parsePath(e){const t=/^[/\\]|^[a-z]:[/\\]/i.exec(e)?.[0]?.replace(/\\/g,"/")||"",n=l(e),r=n.replace(/\/$/,"").split("/").slice(0,-1);r.length===1&&/^[A-Z]:$/i.test(r[0])&&(r[0]+="/");const i=findFolderName(n),s=r.join("/")||(u(e)?"/":"."),o=findFileExtensionSafe(e);return{root:t,dir:s,base:i,ext:o,name:i.slice(0,i.length-o.length)}}export function renameFile(e,t){const n=parsePath(e);return c(n.dir,t.includes(".")?t:t+n.ext)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/path",
3
- "version": "0.9.0",
3
+ "version": "0.10.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": {
@@ -260,5 +260,5 @@
260
260
  "main": "./dist/index.cjs",
261
261
  "module": "./dist/index.mjs",
262
262
  "types": "./dist/index.d.ts",
263
- "gitHead": "4052d0819f6c27c02af4ccd7460b5cd28c36f5c4"
263
+ "gitHead": "108abe84f9ba3d55f19cc68b8cc5fe52d90b5183"
264
264
  }