@bemoje/path 1.0.0 → 1.0.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 +95 -0
- package/index.mjs +11 -11
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @bemoje/path
|
|
2
|
+
|
|
3
|
+
Cross-platform path utilities extending upath with validation, manipulation, and inspection helpers.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/path
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Working Directory Helpers
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { cwd, toCwdRelative } from '@bemoje/path'
|
|
20
|
+
|
|
21
|
+
// Join paths from cwd
|
|
22
|
+
cwd('src', 'index.ts') // /home/user/project/src/index.ts
|
|
23
|
+
|
|
24
|
+
// Convert absolute to cwd-relative
|
|
25
|
+
toCwdRelative('/home/user/project/src/index.ts') // 'src/index.ts'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Filename Manipulation
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { prefixFilename, suffixFilename } from '@bemoje/path'
|
|
32
|
+
|
|
33
|
+
prefixFilename('path/to/file.ts', 'new-') // 'path/to/new-file.ts'
|
|
34
|
+
suffixFilename('path/to/file.ts', '.bak') // 'path/to/file.bak.ts'
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Path Inspection
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { hasExtname, hasBasename, hasParentDirname, isDotFile, isRelative, isUnc } from '@bemoje/path'
|
|
41
|
+
|
|
42
|
+
hasExtname('index.ts', ['ts', 'tsx']) // true
|
|
43
|
+
hasExtname.ts('index.ts') // true
|
|
44
|
+
hasExtname.json('config.json') // true
|
|
45
|
+
|
|
46
|
+
hasBasename('src/index.ts', 'index.ts') // true
|
|
47
|
+
hasParentDirname('src/lib/utils.ts', 'lib') // true
|
|
48
|
+
|
|
49
|
+
isDotFile('.gitignore') // true
|
|
50
|
+
isRelative('./src') // true
|
|
51
|
+
isUnc('\\\\server\\share') // true
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Validation
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { isValidWin32, isExtValid } from '@bemoje/path'
|
|
58
|
+
|
|
59
|
+
isValidWin32('C:\\Users\\file.txt') // true
|
|
60
|
+
isValidWin32('C:\\Users\\file<>.txt') // false
|
|
61
|
+
|
|
62
|
+
isExtValid('.ts') // true
|
|
63
|
+
isExtValid('') // false
|
|
64
|
+
isExtValid('.') // false
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Directory and Root Helpers
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { dirnameDeep, root, toWin32 } from '@bemoje/path'
|
|
71
|
+
|
|
72
|
+
dirnameDeep('a/b/c/d', 2) // 'a/b'
|
|
73
|
+
root('/home/user') // '/'
|
|
74
|
+
toWin32('path/to/file') // 'path\\to\\file'
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API Reference
|
|
78
|
+
|
|
79
|
+
| Export | Description |
|
|
80
|
+
| ------------------ | ---------------------------------------------- |
|
|
81
|
+
| `cwd` | Join paths from process.cwd() |
|
|
82
|
+
| `toCwdRelative` | Convert absolute path to cwd-relative |
|
|
83
|
+
| `prefixFilename` | Prepend string to filename |
|
|
84
|
+
| `suffixFilename` | Append string to filename (before extension) |
|
|
85
|
+
| `dirnameDeep` | Get parent directory N levels up |
|
|
86
|
+
| `hasBasename` | Check if path has a specific basename |
|
|
87
|
+
| `hasExtname` | Check if path has specific extension(s) |
|
|
88
|
+
| `hasParentDirname` | Check if path is under a parent directory name |
|
|
89
|
+
| `isDotFile` | Check if path is a dotfile |
|
|
90
|
+
| `isExtValid` | Validate a file extension |
|
|
91
|
+
| `isRelative` | Check if path is relative |
|
|
92
|
+
| `isUnc` | Check if path is a UNC path |
|
|
93
|
+
| `isValidWin32` | Validate a Windows file path |
|
|
94
|
+
| `root` | Get root directory of a path |
|
|
95
|
+
| `toWin32` | Convert forward slashes to backslashes |
|
package/index.mjs
CHANGED
|
@@ -19,8 +19,8 @@ function hasBasename(tsFilepath, basenames) {
|
|
|
19
19
|
}
|
|
20
20
|
__name(hasBasename, "hasBasename");
|
|
21
21
|
|
|
22
|
-
// ../fn/src/
|
|
23
|
-
function
|
|
22
|
+
// ../fn/src/setNameAndLength.ts
|
|
23
|
+
function setNameAndLength(source, target, lengthDelta = 0) {
|
|
24
24
|
return Object.defineProperties(target, {
|
|
25
25
|
name: {
|
|
26
26
|
configurable: true,
|
|
@@ -28,15 +28,15 @@ function preserveNameAndLength(source, target, adjustLengthBy = 0) {
|
|
|
28
28
|
},
|
|
29
29
|
length: {
|
|
30
30
|
configurable: true,
|
|
31
|
-
value: source.length +
|
|
31
|
+
value: source.length + lengthDelta
|
|
32
32
|
}
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
__name(
|
|
35
|
+
__name(setNameAndLength, "setNameAndLength");
|
|
36
36
|
|
|
37
37
|
// ../fn/src/bindArg.ts
|
|
38
38
|
function bindArg(fn, boundArgIndex, boundArgValue) {
|
|
39
|
-
return
|
|
39
|
+
return setNameAndLength(fn, (...args) => {
|
|
40
40
|
return fn(...args.slice(0, boundArgIndex), boundArgValue, ...args.slice(boundArgIndex));
|
|
41
41
|
}, -1);
|
|
42
42
|
}
|
|
@@ -81,13 +81,13 @@ __name(hasParentDirname, "hasParentDirname");
|
|
|
81
81
|
|
|
82
82
|
// src/lib/isDotFile.ts
|
|
83
83
|
function isDotFile(filepath) {
|
|
84
|
-
return /(?:^|[
|
|
84
|
+
return /(?:^|[/\\])(\.(?!\.)[^/\\]+)$/.test(filepath);
|
|
85
85
|
}
|
|
86
86
|
__name(isDotFile, "isDotFile");
|
|
87
87
|
|
|
88
88
|
// src/lib/isExtValid.ts
|
|
89
89
|
function isExtValid(ext) {
|
|
90
|
-
return !!ext && ext !== "." && !/[
|
|
90
|
+
return !!ext && ext !== "." && !/["*:<>?|]/.test(ext);
|
|
91
91
|
}
|
|
92
92
|
__name(isExtValid, "isExtValid");
|
|
93
93
|
function isRelative(p) {
|
|
@@ -97,7 +97,7 @@ __name(isRelative, "isRelative");
|
|
|
97
97
|
|
|
98
98
|
// src/lib/isUnc.ts
|
|
99
99
|
function isUnc(filepath) {
|
|
100
|
-
return /^[
|
|
100
|
+
return /^[/\\]{2,}[^/\\]+[/\\]+[^/\\]+/.test(filepath);
|
|
101
101
|
}
|
|
102
102
|
__name(isUnc, "isUnc");
|
|
103
103
|
|
|
@@ -106,7 +106,7 @@ function isValidWin32(path2, options) {
|
|
|
106
106
|
if (path2.length === 0) {
|
|
107
107
|
return false;
|
|
108
108
|
}
|
|
109
|
-
if (path2.
|
|
109
|
+
if (path2.includes("/") && path2.includes("\\")) {
|
|
110
110
|
return false;
|
|
111
111
|
}
|
|
112
112
|
const maxLength = (options && options.extendedMaxLength ? 32767 : 260) - 12;
|
|
@@ -114,7 +114,7 @@ function isValidWin32(path2, options) {
|
|
|
114
114
|
return false;
|
|
115
115
|
}
|
|
116
116
|
const noDriveLetter = /^\w:(\\|\/)/g.test(path2) ? path2.substring(2) : path2;
|
|
117
|
-
if (/[
|
|
117
|
+
if (/["*:<>?|]/g.test(noDriveLetter)) {
|
|
118
118
|
return false;
|
|
119
119
|
}
|
|
120
120
|
if (/(\\|\/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])((\\|\/)|$)/g.test(path2)) {
|
|
@@ -146,7 +146,7 @@ __name(toCwdRelative, "toCwdRelative");
|
|
|
146
146
|
|
|
147
147
|
// src/lib/toWin32.ts
|
|
148
148
|
function toWin32(p) {
|
|
149
|
-
return p.
|
|
149
|
+
return p.replaceAll("/", "\\");
|
|
150
150
|
}
|
|
151
151
|
__name(toWin32, "toWin32");
|
|
152
152
|
|