unslash 1.0.1 → 2.0.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.
- package/README.md +52 -12
- package/dist/index.d.ts +38 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +7 -0
- package/package.json +4 -4
- package/dist/pattern.d.ts +0 -14
- package/dist/pattern.js +0 -1
- package/dist/slash.d.ts +0 -23
- package/dist/slash.js +0 -95
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# `/` unslash
|
|
2
2
|
|
|
3
3
|
Stop wasting your life on slash normalization.<br />
|
|
4
|
+
Works everywhere ("everywhere" means in browsers too).<br />
|
|
4
5
|
Zero dependencies because why would I need any?
|
|
5
6
|
|
|
6
7
|
## Install
|
|
@@ -12,55 +13,94 @@ npm install unslash
|
|
|
12
13
|
## Usage
|
|
13
14
|
|
|
14
15
|
```ts
|
|
15
|
-
import { slash } from 'unslash';
|
|
16
|
+
import { s, slash } from 'unslash';
|
|
17
|
+
|
|
18
|
+
// Just join some paths. No magic.
|
|
19
|
+
s('path', 'to', 'file');
|
|
20
|
+
// → 'path/to/file'
|
|
16
21
|
|
|
17
22
|
// Need a trailing slash? Done.
|
|
18
|
-
|
|
23
|
+
s(/t/, 'path', 'to', 'file');
|
|
19
24
|
// → 'path/to/file/'
|
|
20
25
|
|
|
21
26
|
// Don’t want it? Gone.
|
|
22
|
-
|
|
27
|
+
s(/!t/, 'path/to/file/');
|
|
23
28
|
// → 'path/to/file'
|
|
24
29
|
|
|
25
30
|
// Want a leading slash? Here you go.
|
|
26
|
-
|
|
31
|
+
s(/l/, 'path', 'to', 'file');
|
|
27
32
|
// → '/path/to/file'
|
|
28
33
|
|
|
29
34
|
// Need to collapse that mess? Fixed.
|
|
30
|
-
|
|
35
|
+
s(/c/, 'path///to', 'this//file');
|
|
31
36
|
// → 'path/to/this/file'
|
|
32
37
|
|
|
33
38
|
// Want to force forward slashes? Me too.
|
|
34
|
-
|
|
39
|
+
s(/f/, 'path\\to', 'this/file');
|
|
35
40
|
// → 'path/to/this/file'
|
|
36
41
|
|
|
37
42
|
// Need Windows paths? What is wrong with you?!
|
|
38
|
-
|
|
43
|
+
s(/!f/, 'path', 'to', 'file');
|
|
39
44
|
// → 'path\to\file'
|
|
40
45
|
|
|
41
46
|
// Combine them. Because you can.
|
|
42
|
-
|
|
47
|
+
s(/tlfc/, 'path\\\\to\\file');
|
|
43
48
|
// → '/path/to/file/'
|
|
44
|
-
|
|
49
|
+
s(/!t!l!fc/, '/path///to/file/');
|
|
45
50
|
// → 'path\to\file'
|
|
46
51
|
|
|
47
52
|
// Your precious protocols are safe — don’t worry.
|
|
48
|
-
|
|
53
|
+
s(/tlfc/, 'http://example.com', 'path', 'to', 'file');
|
|
49
54
|
// → 'http://example.com/path/to/file/'
|
|
50
55
|
```
|
|
51
56
|
|
|
57
|
+
There are three ways to use the library:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import unslash, { s, slash } from 'unslash';
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
For all DRY lovers like me, you can create reusable formatters:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
// Create a formatter
|
|
67
|
+
const normalize = s(/fc/); // Force forward, collapse
|
|
68
|
+
|
|
69
|
+
// Use it
|
|
70
|
+
normalize('path\\to\\file');
|
|
71
|
+
// → 'path/to/file'
|
|
72
|
+
|
|
73
|
+
// Extend it on the fly if you're feeling spicy
|
|
74
|
+
normalize(/t/, 'path\\to\\file');
|
|
75
|
+
// → 'path/to/file/'
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
For the lazy ones, `snormalize` (or `sn`) is already pre-configured with `/fc/` (Force forward + Collapse):
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { sn } from 'unslash';
|
|
82
|
+
|
|
83
|
+
sn('path\\to//file');
|
|
84
|
+
// → 'path/to/file'
|
|
85
|
+
|
|
86
|
+
sn(/t/, 'path\\to//file');
|
|
87
|
+
// → 'path/to/file/'
|
|
88
|
+
```
|
|
89
|
+
|
|
52
90
|
## Pattern Flags
|
|
53
91
|
|
|
92
|
+
Use these in your regex literal (e.g., `/tfc/`):
|
|
93
|
+
|
|
54
94
|
- `t` / `!t` — Add/remove **T**railing slash
|
|
55
95
|
- `l` / `!l` — Add/remove **L**eading slash
|
|
56
96
|
- `f` / `!f` — Force **F**orward slashes / backward slashes
|
|
57
|
-
- `c` — **C**ollapse multiple slashes
|
|
97
|
+
- `c` / `!c` — **C**ollapse multiple slashes / Don't collapse
|
|
58
98
|
|
|
59
99
|
## But Gwynerva, there are libraries for this
|
|
60
100
|
|
|
61
101
|
Yeah, I know.<br/>
|
|
62
102
|
I don’t give a shit.<br />
|
|
63
103
|
I was too lazy to look for them and made my own.<br />
|
|
64
|
-
|
|
104
|
+
Created with GPT help. Cry about it.
|
|
65
105
|
|
|
66
106
|
_It’d be cool if this package got popular and made me rich._
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare function _slash(pattern: RegExp): SlashFormatter;
|
|
2
|
+
declare function _slash(pattern: RegExp, ...parts: string[]): string;
|
|
3
|
+
declare function _slash(...parts: string[]): string;
|
|
4
|
+
type SlashFormatter = {
|
|
5
|
+
(pattern: RegExp): SlashFormatter;
|
|
6
|
+
(...parts: string[]): string;
|
|
7
|
+
(pattern: RegExp, ...parts: string[]): string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Creates a slash formatter or formats strings directly.
|
|
11
|
+
*
|
|
12
|
+
* The formatter is configured using a RegExp pattern whose source consists of
|
|
13
|
+
* single-letter flags and their negations (`!x`). Flags are evaluated
|
|
14
|
+
* left-to-right; later flags override earlier ones (e.g. `c!c` disables
|
|
15
|
+
* collapsing even if `c` appeared before).
|
|
16
|
+
*
|
|
17
|
+
* Supported flags:
|
|
18
|
+
* - `t` | `!t` — ensure trailing slash is added | removed
|
|
19
|
+
* - `l` | `!l` — ensure leading slash is added | removed
|
|
20
|
+
* - `f` | `!f` — force forward | backward slashes
|
|
21
|
+
* - `c` | `!c` — collapse multiple slashes | do not collapse
|
|
22
|
+
*
|
|
23
|
+
* Calling with a RegExp returns a reusable formatter.
|
|
24
|
+
* Calling a formatter with another RegExp appends flags to the existing pattern.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const normalize = slasher(/fc/); // Force forward slashes, collapse multiple slashes
|
|
28
|
+
* normalize(/t/, 'my\\long\\\\path'); // `fc` flags + force trailing slash and apply formatting
|
|
29
|
+
* // → 'my/long/path/'
|
|
30
|
+
*/
|
|
31
|
+
export declare const slash: typeof _slash;
|
|
32
|
+
/** Short version of `slash`. */
|
|
33
|
+
export declare const s: typeof _slash;
|
|
34
|
+
/** Convert to forward slashes + collapse multiple adjacent slashes. */
|
|
35
|
+
export declare const snormalize: SlashFormatter;
|
|
36
|
+
/** Short version of `snormalize`. */
|
|
37
|
+
export declare const sn: SlashFormatter;
|
|
38
|
+
export default slash;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
function h(...s){const[n,...e]=s;if(typeof n=="string")return s.join("/");if(n instanceof RegExp&&e.length===0){const t=o(n);return g(t)}return n instanceof RegExp?l(o(n),e):""}function g(s){return((...e)=>{const[t,...i]=e;if(t instanceof RegExp){const r=o(t),a=p(s,r);return i.length>0?l(a,i):g(a)}return l(s,e)})}function o(s){const n=s.source,e={};for(let t=0;t<n.length;t++){const i=n[t],r=i==="!",a=r?n[t+1]:i;switch(r&&t++,a){case"f":case"c":case"t":case"l":e[a]=!r;break}}return e}function p(s,n){return{...s,...n}}function l(s,n){const e=s.f===!1?"\\":"/";let t=n.join(e);const i=/^([a-zA-Z][a-zA-Z\d+\-.]*:)(\/\/)?/;let r="";const a=t.match(i);if(a&&(r=a[0],t=t.slice(r.length)),s.f===!1?t=t.replace(/\//g,"\\"):s.f===!0&&(t=t.replace(/\\/g,"/")),s.c){const c=s.f===!1?/\\+/g:/\/+/g;t=t.replace(c,e)}if(s.t)t.endsWith(e)||(t+=e);else if(s.t===!1)for(;t.endsWith("/")||t.endsWith("\\");)t=t.slice(0,-1);if(s.l)t.startsWith(e)||(t=e+t);else if(s.l===!1)for(;t.startsWith("/")||t.startsWith("\\");)t=t.slice(1);return r+t}const f=h,x=f,u=f(/fc/),m=u;var S=f;export{S as default,x as s,f as slash,m as sn,u as snormalize};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["// #region Slash\n//\n\nfunction _slash(pattern: RegExp): SlashFormatter;\nfunction _slash(pattern: RegExp, ...parts: string[]): string;\nfunction _slash(...parts: string[]): string;\nfunction _slash(\n ...args: [RegExp | string, ...string[]] | string[]\n): string | SlashFormatter {\n const [first, ...rest] = args;\n\n //\n // slash(\"a\", \"b\")\n //\n\n if (typeof first === 'string') {\n return (args as string[]).join('/');\n }\n\n //\n // slash(/pattern/)\n //\n\n if (first instanceof RegExp && rest.length === 0) {\n const config = parsePattern(first);\n return createFormatter(config);\n }\n\n //\n // slash(/pattern/, \"a\", \"b\")\n //\n\n if (first instanceof RegExp) {\n return applyConfig(parsePattern(first), rest as string[]);\n }\n\n return '';\n}\n\n//\n// #endregion\n\n// #region Helpers\n//\n\ntype SlashFormatter = {\n (pattern: RegExp): SlashFormatter;\n (...parts: string[]): string;\n (pattern: RegExp, ...parts: string[]): string;\n};\n\ntype SlashConfig = {\n f?: boolean;\n c?: boolean;\n t?: boolean;\n l?: boolean;\n};\n\nfunction createFormatter(config: SlashConfig): SlashFormatter {\n const formatter = ((\n ...inner: [RegExp | string, ...string[]] | string[]\n ): string | SlashFormatter => {\n const [head, ...tail] = inner;\n\n // Append pattern\n if (head instanceof RegExp) {\n const nextConfig = parsePattern(head);\n const combined = mergeConfigs(config, nextConfig);\n\n // pattern + strings \u2192 evaluate immediately\n if (tail.length > 0) {\n return applyConfig(combined, tail as string[]);\n }\n\n // pattern only \u2192 return new formatter\n return createFormatter(combined);\n }\n\n // formatter(\"a\", \"b\")\n return applyConfig(config, inner as string[]);\n }) as SlashFormatter;\n\n return formatter;\n}\n\nfunction parsePattern(pattern: RegExp): SlashConfig {\n const source = pattern.source;\n const config: SlashConfig = {};\n\n for (let i = 0; i < source.length; i++) {\n const char = source[i];\n const negated = char === '!';\n const flag = negated ? source[i + 1] : char;\n\n if (negated) i++;\n\n switch (flag) {\n case 'f':\n case 'c':\n case 't':\n case 'l':\n config[flag] = !negated;\n break;\n }\n }\n\n return config;\n}\n\nfunction mergeConfigs(a: SlashConfig, b: SlashConfig): SlashConfig {\n return { ...a, ...b };\n}\n\nfunction applyConfig(config: SlashConfig, parts: string[]): string {\n const slashChar = config.f === false ? '\\\\' : '/';\n //\n // Protocol-safe join\n //\n\n let joined = parts.join(slashChar);\n\n const PROTOCOL_RE = /^([a-zA-Z][a-zA-Z\\d+\\-.]*:)(\\/\\/)?/;\n\n let protocol = '';\n const match = joined.match(PROTOCOL_RE);\n\n if (match) {\n protocol = match[0];\n joined = joined.slice(protocol.length);\n }\n\n //\n // Force slash type\n //\n\n if (config.f === false) {\n joined = joined.replace(/\\//g, '\\\\');\n } else if (config.f === true) {\n joined = joined.replace(/\\\\/g, '/');\n }\n\n //\n // Collapse multiple slashes\n //\n\n if (config.c) {\n const slashRegex = config.f === false ? /\\\\+/g : /\\/+/g;\n joined = joined.replace(slashRegex, slashChar);\n }\n\n //\n // Trailing slash\n //\n\n if (config.t) {\n if (!joined.endsWith(slashChar)) {\n joined += slashChar;\n }\n } else if (config.t === false) {\n // Explicitly removed (!t)\n while (joined.endsWith('/') || joined.endsWith('\\\\')) {\n joined = joined.slice(0, -1);\n }\n }\n\n //\n // Leading slash\n //\n\n if (config.l) {\n if (!joined.startsWith(slashChar)) {\n joined = slashChar + joined;\n }\n } else if (config.l === false) {\n // Explicitly removed (!l)\n while (joined.startsWith('/') || joined.startsWith('\\\\')) {\n joined = joined.slice(1);\n }\n }\n\n return protocol + joined;\n}\n\n//\n// #endregion\n\n// #region Exports\n//\n\n/**\n * Creates a slash formatter or formats strings directly.\n *\n * The formatter is configured using a RegExp pattern whose source consists of\n * single-letter flags and their negations (`!x`). Flags are evaluated\n * left-to-right; later flags override earlier ones (e.g. `c!c` disables\n * collapsing even if `c` appeared before).\n *\n * Supported flags:\n * - `t` | `!t` \u2014 ensure trailing slash is added | removed\n * - `l` | `!l` \u2014 ensure leading slash is added | removed\n * - `f` | `!f` \u2014 force forward | backward slashes\n * - `c` | `!c` \u2014 collapse multiple slashes | do not collapse\n *\n * Calling with a RegExp returns a reusable formatter.\n * Calling a formatter with another RegExp appends flags to the existing pattern.\n *\n * @example\n * const normalize = slasher(/fc/); // Force forward slashes, collapse multiple slashes\n * normalize(/t/, 'my\\\\long\\\\\\\\path'); // `fc` flags + force trailing slash and apply formatting\n * // \u2192 'my/long/path/'\n */\nexport const slash = _slash;\n\n/** Short version of `slash`. */\nexport const s = slash;\n\n/** Convert to forward slashes + collapse multiple adjacent slashes. */\nexport const snormalize = slash(/fc/);\n\n/** Short version of `snormalize`. */\nexport const sn = snormalize;\n\nexport default slash;\n\n//\n// #endregion\n"],
|
|
5
|
+
"mappings": "AAMA,SAASA,KACJC,EACsB,CACzB,KAAM,CAACC,EAAO,GAAGC,CAAI,EAAIF,EAMzB,GAAI,OAAOC,GAAU,SACnB,OAAQD,EAAkB,KAAK,GAAG,EAOpC,GAAIC,aAAiB,QAAUC,EAAK,SAAW,EAAG,CAChD,MAAMC,EAASC,EAAaH,CAAK,EACjC,OAAOI,EAAgBF,CAAM,CAC/B,CAMA,OAAIF,aAAiB,OACZK,EAAYF,EAAaH,CAAK,EAAGC,CAAgB,EAGnD,EACT,CAqBA,SAASG,EAAgBF,EAAqC,CAwB5D,OAvBmB,IACdI,IACyB,CAC5B,KAAM,CAACC,EAAM,GAAGC,CAAI,EAAIF,EAGxB,GAAIC,aAAgB,OAAQ,CAC1B,MAAME,EAAaN,EAAaI,CAAI,EAC9BG,EAAWC,EAAaT,EAAQO,CAAU,EAGhD,OAAID,EAAK,OAAS,EACTH,EAAYK,EAAUF,CAAgB,EAIxCJ,EAAgBM,CAAQ,CACjC,CAGA,OAAOL,EAAYH,EAAQI,CAAiB,CAC9C,EAGF,CAEA,SAASH,EAAaS,EAA8B,CAClD,MAAMC,EAASD,EAAQ,OACjBV,EAAsB,CAAC,EAE7B,QAASY,EAAI,EAAGA,EAAID,EAAO,OAAQC,IAAK,CACtC,MAAMC,EAAOF,EAAOC,CAAC,EACfE,EAAUD,IAAS,IACnBE,EAAOD,EAAUH,EAAOC,EAAI,CAAC,EAAIC,EAIvC,OAFIC,GAASF,IAELG,EAAM,CACZ,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACHf,EAAOe,CAAI,EAAI,CAACD,EAChB,KACJ,CACF,CAEA,OAAOd,CACT,CAEA,SAASS,EAAaO,EAAgBC,EAA6B,CACjE,MAAO,CAAE,GAAGD,EAAG,GAAGC,CAAE,CACtB,CAEA,SAASd,EAAYH,EAAqBkB,EAAyB,CACjE,MAAMC,EAAYnB,EAAO,IAAM,GAAQ,KAAO,IAK9C,IAAIoB,EAASF,EAAM,KAAKC,CAAS,EAEjC,MAAME,EAAc,qCAEpB,IAAIC,EAAW,GACf,MAAMC,EAAQH,EAAO,MAAMC,CAAW,EAqBtC,GAnBIE,IACFD,EAAWC,EAAM,CAAC,EAClBH,EAASA,EAAO,MAAME,EAAS,MAAM,GAOnCtB,EAAO,IAAM,GACfoB,EAASA,EAAO,QAAQ,MAAO,IAAI,EAC1BpB,EAAO,IAAM,KACtBoB,EAASA,EAAO,QAAQ,MAAO,GAAG,GAOhCpB,EAAO,EAAG,CACZ,MAAMwB,EAAaxB,EAAO,IAAM,GAAQ,OAAS,OACjDoB,EAASA,EAAO,QAAQI,EAAYL,CAAS,CAC/C,CAMA,GAAInB,EAAO,EACJoB,EAAO,SAASD,CAAS,IAC5BC,GAAUD,WAEHnB,EAAO,IAAM,GAEtB,KAAOoB,EAAO,SAAS,GAAG,GAAKA,EAAO,SAAS,IAAI,GACjDA,EAASA,EAAO,MAAM,EAAG,EAAE,EAQ/B,GAAIpB,EAAO,EACJoB,EAAO,WAAWD,CAAS,IAC9BC,EAASD,EAAYC,WAEdpB,EAAO,IAAM,GAEtB,KAAOoB,EAAO,WAAW,GAAG,GAAKA,EAAO,WAAW,IAAI,GACrDA,EAASA,EAAO,MAAM,CAAC,EAI3B,OAAOE,EAAWF,CACpB,CA8BO,MAAMK,EAAQ7B,EAGR8B,EAAID,EAGJE,EAAaF,EAAM,IAAI,EAGvBG,EAAKD,EAElB,IAAOE,EAAQJ",
|
|
6
|
+
"names": ["_slash", "args", "first", "rest", "config", "parsePattern", "createFormatter", "applyConfig", "inner", "head", "tail", "nextConfig", "combined", "mergeConfigs", "pattern", "source", "i", "char", "negated", "flag", "a", "b", "parts", "slashChar", "joined", "PROTOCOL_RE", "protocol", "match", "slashRegex", "slash", "s", "snormalize", "sn", "index_default"]
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unslash",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "🤬 No more pain with slashes!",
|
|
6
6
|
"keywords": [
|
|
@@ -11,10 +11,8 @@
|
|
|
11
11
|
"url",
|
|
12
12
|
"trailing-slash",
|
|
13
13
|
"leading-slash",
|
|
14
|
-
"router",
|
|
15
14
|
"base-path",
|
|
16
15
|
"cross-platform",
|
|
17
|
-
"windows",
|
|
18
16
|
"utilities"
|
|
19
17
|
],
|
|
20
18
|
"license": "MIT",
|
|
@@ -34,12 +32,14 @@
|
|
|
34
32
|
"dist"
|
|
35
33
|
],
|
|
36
34
|
"scripts": {
|
|
37
|
-
"build": "rm -rf dist && tsc
|
|
35
|
+
"build": "rm -rf dist && tsc -p ./tsconfig.src.json && esbuild src/**/*.ts --outdir=dist --minify --sourcemap --format=esm",
|
|
38
36
|
"prepack": "bun run build",
|
|
39
37
|
"format": "bun prettier --write .",
|
|
40
38
|
"test": "bun vitest run"
|
|
41
39
|
},
|
|
42
40
|
"devDependencies": {
|
|
41
|
+
"@types/node": "^25.1.0",
|
|
42
|
+
"esbuild": "^0.27.2",
|
|
43
43
|
"prettier": "^3.8.1",
|
|
44
44
|
"typescript": "^5.9.3",
|
|
45
45
|
"vitest": "^4.0.18"
|
package/dist/pattern.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/slash.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
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
|
-
}
|