unslash 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/LICENSE +21 -0
- package/README.md +66 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/pattern.d.ts +14 -0
- package/dist/pattern.js +1 -0
- package/dist/slash.d.ts +23 -0
- package/dist/slash.js +95 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present - Gwynerva
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# `/` unslash
|
|
2
|
+
|
|
3
|
+
Stop wasting your life on slash normalization.<br />
|
|
4
|
+
Zero dependencies because why would I need any?
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install unslash
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { slash } from 'unslash';
|
|
16
|
+
|
|
17
|
+
// Need a trailing slash? Done.
|
|
18
|
+
slash('t', 'path', 'to', 'file');
|
|
19
|
+
// → 'path/to/file/'
|
|
20
|
+
|
|
21
|
+
// Don’t want it? Gone.
|
|
22
|
+
slash('!t', 'path/to/file/');
|
|
23
|
+
// → 'path/to/file'
|
|
24
|
+
|
|
25
|
+
// Want a leading slash? Here you go.
|
|
26
|
+
slash('l', 'path', 'to', 'file');
|
|
27
|
+
// → '/path/to/file'
|
|
28
|
+
|
|
29
|
+
// Need to collapse that mess? Fixed.
|
|
30
|
+
slash('c', 'path///to', 'this//file');
|
|
31
|
+
// → 'path/to/this/file'
|
|
32
|
+
|
|
33
|
+
// Want to force forward slashes? Me too.
|
|
34
|
+
slash('f', 'path\\to', 'this/file');
|
|
35
|
+
// → 'path/to/this/file'
|
|
36
|
+
|
|
37
|
+
// Need Windows paths? What is wrong with you?!
|
|
38
|
+
slash('!f', 'path', 'to', 'file');
|
|
39
|
+
// → 'path\to\file'
|
|
40
|
+
|
|
41
|
+
// Combine them. Because you can.
|
|
42
|
+
slash('tlfc', 'path\\\\to\\file');
|
|
43
|
+
// → '/path/to/file/'
|
|
44
|
+
slash('!t!l!fc', '/path///to/file/');
|
|
45
|
+
// → 'path\to\file'
|
|
46
|
+
|
|
47
|
+
// Your precious protocols are safe — don’t worry.
|
|
48
|
+
slash('tlfc', 'http://example.com', 'path', 'to', 'file');
|
|
49
|
+
// → 'http://example.com/path/to/file/'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Pattern Flags
|
|
53
|
+
|
|
54
|
+
- `t` / `!t` — Add/remove **T**railing slash
|
|
55
|
+
- `l` / `!l` — Add/remove **L**eading slash
|
|
56
|
+
- `f` / `!f` — Force **F**orward slashes / backward slashes
|
|
57
|
+
- `c` — **C**ollapse multiple slashes (yes, you’re right — `!c` doesn’t do shit)
|
|
58
|
+
|
|
59
|
+
## But Gwynerva, there are libraries for this
|
|
60
|
+
|
|
61
|
+
Yeah, I know.<br/>
|
|
62
|
+
I don’t give a shit.<br />
|
|
63
|
+
I was too lazy to look for them and made my own.<br />
|
|
64
|
+
Made with GPT help. Cry about it.
|
|
65
|
+
|
|
66
|
+
_It’d be cool if this package got popular and made me rich._
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type SlashFlag = 't' | 'l' | 'f' | 'c';
|
|
2
|
+
export declare const unslash: unique symbol;
|
|
3
|
+
type link = 'https://github.com/Gwynerva/unslash';
|
|
4
|
+
export type UnknownFlagError<C extends string> = {
|
|
5
|
+
readonly [unslash]: link;
|
|
6
|
+
SlashPatternError: `Character "${C}" is not a valid SlashFlag (allowed: t l f c, optionally prefixed with "!")`;
|
|
7
|
+
};
|
|
8
|
+
export type DuplicateFlagError<F extends string> = {
|
|
9
|
+
readonly [unslash]: link;
|
|
10
|
+
SlashPatternError: `Flag "${F}" is used more than once or both negated and non-negated`;
|
|
11
|
+
};
|
|
12
|
+
export type ValidateSlashPattern<S extends string, Pos extends string = never, Neg extends string = never> = S extends `!${infer L}${infer R}` ? L extends SlashFlag ? L extends Pos | Neg ? DuplicateFlagError<L> : ValidateSlashPattern<R, Pos, Neg | L> : UnknownFlagError<`!${L}`> : S extends `${infer L}${infer R}` ? L extends SlashFlag ? L extends Pos | Neg ? DuplicateFlagError<L> : ValidateSlashPattern<R, Pos | L, Neg> : UnknownFlagError<L> : S;
|
|
13
|
+
export type SlashPattern<S extends string> = ValidateSlashPattern<S> extends string ? S : ValidateSlashPattern<S>;
|
|
14
|
+
export {};
|
package/dist/pattern.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/slash.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SlashPattern } from './pattern.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a string formatter based on the given slash pattern.
|
|
4
|
+
* @param pattern
|
|
5
|
+
* A string of letters or their negations (prefixed with "!").
|
|
6
|
+
* Each letter represents a specific formatting rule:
|
|
7
|
+
* - `t` | `!t`: Ensure trailing slash is added | removed.
|
|
8
|
+
* - `l` | `!l`: Ensure leading slash is added | removed.
|
|
9
|
+
* - `f` | `!f`: Force forward | backward slashes.
|
|
10
|
+
* - `c` | `!c`: Collapse multiple slashes into a single slash | Does nothing.
|
|
11
|
+
*
|
|
12
|
+
* Example patterns:
|
|
13
|
+
* - `"tlfc"`: Ensure leading and trailing slashes, force forward slashes, collapse multiple slashes.
|
|
14
|
+
* - `"lf"`: Force forward slashes and ensure leading slash (trailing might be present or absent).
|
|
15
|
+
* - `tl!f`: Ensure leading and trailing slashes, force backward slashes.
|
|
16
|
+
* @param parts
|
|
17
|
+
* The string parts to be formatted according to the specified pattern.
|
|
18
|
+
* They will be joined together using forward slashes by default (or backward slashes if `!f` is specified in the pattern).
|
|
19
|
+
* @returns
|
|
20
|
+
* The formatted string with slash-concatenated parts according to the specified slash pattern.
|
|
21
|
+
* Protocols (like `http://`) are preserved and not altered by the formatting.
|
|
22
|
+
*/
|
|
23
|
+
export declare function slash<S extends string>(pattern: SlashPattern<S>, ...parts: string[]): string;
|
package/dist/slash.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a string formatter based on the given slash pattern.
|
|
3
|
+
* @param pattern
|
|
4
|
+
* A string of letters or their negations (prefixed with "!").
|
|
5
|
+
* Each letter represents a specific formatting rule:
|
|
6
|
+
* - `t` | `!t`: Ensure trailing slash is added | removed.
|
|
7
|
+
* - `l` | `!l`: Ensure leading slash is added | removed.
|
|
8
|
+
* - `f` | `!f`: Force forward | backward slashes.
|
|
9
|
+
* - `c` | `!c`: Collapse multiple slashes into a single slash | Does nothing.
|
|
10
|
+
*
|
|
11
|
+
* Example patterns:
|
|
12
|
+
* - `"tlfc"`: Ensure leading and trailing slashes, force forward slashes, collapse multiple slashes.
|
|
13
|
+
* - `"lf"`: Force forward slashes and ensure leading slash (trailing might be present or absent).
|
|
14
|
+
* - `tl!f`: Ensure leading and trailing slashes, force backward slashes.
|
|
15
|
+
* @param parts
|
|
16
|
+
* The string parts to be formatted according to the specified pattern.
|
|
17
|
+
* They will be joined together using forward slashes by default (or backward slashes if `!f` is specified in the pattern).
|
|
18
|
+
* @returns
|
|
19
|
+
* The formatted string with slash-concatenated parts according to the specified slash pattern.
|
|
20
|
+
* Protocols (like `http://`) are preserved and not altered by the formatting.
|
|
21
|
+
*/
|
|
22
|
+
export function slash(pattern, ...parts) {
|
|
23
|
+
const flags = {};
|
|
24
|
+
// @ts-expect-error Pattern is validated at compile-time
|
|
25
|
+
pattern = pattern;
|
|
26
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
27
|
+
const char = pattern[i];
|
|
28
|
+
const negated = char === '!';
|
|
29
|
+
const flag = negated ? pattern[i + 1] : char;
|
|
30
|
+
flags[flag] = !negated;
|
|
31
|
+
if (negated) {
|
|
32
|
+
i++;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const slashChar = flags['f'] === false ? '\\' : '/';
|
|
36
|
+
// ------------------------------------------------------------
|
|
37
|
+
// Protocol-safe join
|
|
38
|
+
// ------------------------------------------------------------
|
|
39
|
+
let joined = parts.join(slashChar);
|
|
40
|
+
// Matches: scheme: or scheme://
|
|
41
|
+
// Examples: http://, file://, mailto:
|
|
42
|
+
const PROTOCOL_RE = /^([a-zA-Z][a-zA-Z\d+\-.]*:)(\/\/)?/;
|
|
43
|
+
let protocol = '';
|
|
44
|
+
const match = joined.match(PROTOCOL_RE);
|
|
45
|
+
if (match) {
|
|
46
|
+
protocol = match[0];
|
|
47
|
+
joined = joined.slice(protocol.length);
|
|
48
|
+
}
|
|
49
|
+
// ------------------------------------------------------------
|
|
50
|
+
// Force slash type (convert all slashes)
|
|
51
|
+
// ------------------------------------------------------------
|
|
52
|
+
if (flags['f'] === false) {
|
|
53
|
+
// Force backward slashes: convert all / to \
|
|
54
|
+
joined = joined.replace(/\//g, '\\');
|
|
55
|
+
}
|
|
56
|
+
else if (flags['f'] === true) {
|
|
57
|
+
// Force forward slashes: convert all \ to /
|
|
58
|
+
joined = joined.replace(/\\/g, '/');
|
|
59
|
+
}
|
|
60
|
+
// ------------------------------------------------------------
|
|
61
|
+
// Collapse multiple slashes
|
|
62
|
+
// ------------------------------------------------------------
|
|
63
|
+
if (flags['c']) {
|
|
64
|
+
const slashRegex = flags['f'] === false ? /\\+/g : /\/+/g;
|
|
65
|
+
const replacement = flags['f'] === false ? '\\' : '/';
|
|
66
|
+
joined = joined.replace(slashRegex, replacement);
|
|
67
|
+
}
|
|
68
|
+
// ------------------------------------------------------------
|
|
69
|
+
// Leading slash handling
|
|
70
|
+
// ------------------------------------------------------------
|
|
71
|
+
if (flags['l']) {
|
|
72
|
+
if (!joined.startsWith(slashChar)) {
|
|
73
|
+
joined = slashChar + joined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
while (joined.startsWith('\\') || joined.startsWith('/')) {
|
|
78
|
+
joined = joined.slice(1);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// ------------------------------------------------------------
|
|
82
|
+
// Trailing slash handling
|
|
83
|
+
// ------------------------------------------------------------
|
|
84
|
+
if (flags['t']) {
|
|
85
|
+
if (!joined.endsWith(slashChar)) {
|
|
86
|
+
joined = joined + slashChar;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
while (joined.endsWith('\\') || joined.endsWith('/')) {
|
|
91
|
+
joined = joined.slice(0, -1);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return protocol + joined;
|
|
95
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "unslash",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "🤬 No more pain with slashes!",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"slash",
|
|
8
|
+
"path",
|
|
9
|
+
"path-normalization",
|
|
10
|
+
"normalize",
|
|
11
|
+
"url",
|
|
12
|
+
"trailing-slash",
|
|
13
|
+
"leading-slash",
|
|
14
|
+
"router",
|
|
15
|
+
"base-path",
|
|
16
|
+
"cross-platform",
|
|
17
|
+
"windows",
|
|
18
|
+
"utilities"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Gwynerva/unslash.git"
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "rm -rf dist && tsc --project ./tsconfig.src.json",
|
|
38
|
+
"prepack": "bun run build",
|
|
39
|
+
"format": "bun prettier --write .",
|
|
40
|
+
"test": "bun vitest run"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"prettier": "^3.8.1",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"vitest": "^4.0.18"
|
|
46
|
+
}
|
|
47
|
+
}
|