slugkit 0.1.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/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/index.cjs +157 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +61 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +127 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tung Tran
|
|
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,106 @@
|
|
|
1
|
+
# slugkit
|
|
2
|
+
|
|
3
|
+
> Tiny, type-safe **slugify** — Unicode accent folding, symbol words, and a **unique-slug** generator. **Zero dependencies**.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/trananhtung/slugkit/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/slugkit)
|
|
7
|
+
[](https://bundlephobia.com/package/slugkit)
|
|
8
|
+
[](https://www.npmjs.com/package/slugkit)
|
|
9
|
+
[](./LICENSE)
|
|
10
|
+
|
|
11
|
+
Turning a title into a clean URL sounds trivial until the title is `Crème brûlée
|
|
12
|
+
& coffee` or `Đặng Văn A`, or until two headings slugify to the same anchor.
|
|
13
|
+
`slugkit` folds accents to ASCII, turns symbols into words, and — when you need
|
|
14
|
+
it — guarantees every slug in a document is unique. **Zero dependencies**, Node
|
|
15
|
+
and browser.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { slug } from "slugkit";
|
|
19
|
+
|
|
20
|
+
slug("Hello, World!"); // "hello-world"
|
|
21
|
+
slug("Crème brûlée & coffee"); // "creme-brulee-and-coffee"
|
|
22
|
+
slug("Đặng Văn A"); // "dang-van-a"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Why slugkit?
|
|
26
|
+
|
|
27
|
+
- **Real Unicode folding.** Accents are normalized away (`café` → `cafe`), and the
|
|
28
|
+
letters normalization misses (`đ`, `ø`, `ß`, `æ`, `ł`, …) are mapped explicitly.
|
|
29
|
+
- **Symbol words.** `&` → `and`, `%` → `percent`, `@` → `at` — opt out with
|
|
30
|
+
`symbols: false`.
|
|
31
|
+
- **Unique slugs.** The `Slugger` appends `-2`, `-3`, … so a page full of repeated
|
|
32
|
+
headings still gets collision-free anchors.
|
|
33
|
+
- **Configurable.** Custom separator, preserve case, `maxLength` that truncates on
|
|
34
|
+
a **word boundary**, and your own character overrides.
|
|
35
|
+
- **Typed & tiny.** Full types, ESM + CJS, zero dependencies.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install slugkit
|
|
41
|
+
# or: pnpm add slugkit / yarn add slugkit / bun add slugkit
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## `slug(input, options?)`
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { slug } from "slugkit";
|
|
48
|
+
|
|
49
|
+
slug("Top 10 Tips"); // "top-10-tips"
|
|
50
|
+
slug("Müller Straße"); // "muller-strasse"
|
|
51
|
+
slug("Hello World", { separator: "_" }); // "hello_world"
|
|
52
|
+
slug("Hello World", { lower: false }); // "Hello-World"
|
|
53
|
+
slug("Bread & Butter", { symbols: false }); // "bread-butter"
|
|
54
|
+
slug("the quick brown fox", { maxLength: 13 }); // "the-quick" (no half-words)
|
|
55
|
+
slug("I ♥ JS", { charMap: { "♥": "love" } }); // "i-love-js"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
interface SlugOptions {
|
|
60
|
+
separator?: string; // default "-"
|
|
61
|
+
lower?: boolean; // default true
|
|
62
|
+
symbols?: boolean; // default true (& → and, % → percent, …)
|
|
63
|
+
maxLength?: number; // truncate on a word boundary
|
|
64
|
+
charMap?: Record<string, string>; // extra replacements
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Unique slugs with `Slugger`
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { Slugger } from "slugkit";
|
|
72
|
+
|
|
73
|
+
const slugger = new Slugger();
|
|
74
|
+
slugger.slug("Introduction"); // "introduction"
|
|
75
|
+
slugger.slug("Introduction"); // "introduction-2"
|
|
76
|
+
slugger.slug("Introduction"); // "introduction-3"
|
|
77
|
+
|
|
78
|
+
slugger.has("introduction-2"); // true
|
|
79
|
+
slugger.reset(); // start over
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`new Slugger(options)` applies those `SlugOptions` to every call (overridable
|
|
83
|
+
per call). `createSlugger(options?)` is a factory shorthand. Perfect for building
|
|
84
|
+
a table of contents:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const slugger = new Slugger();
|
|
88
|
+
const anchors = headings.map((h) => ({ text: h, id: slugger.slug(h) }));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Notes
|
|
92
|
+
|
|
93
|
+
- Iterates by code point, so multi-byte characters and emoji are handled safely.
|
|
94
|
+
- Anything that isn't a letter or number becomes a separator boundary; runs are
|
|
95
|
+
collapsed and the ends are trimmed.
|
|
96
|
+
|
|
97
|
+
## Pairs well with
|
|
98
|
+
|
|
99
|
+
| Need | Use |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| Generate a unique id (not derived from text) | [`idkit`](https://www.npmjs.com/package/idkit) |
|
|
102
|
+
| Redact secrets/PII from text | [`scrubtext`](https://www.npmjs.com/package/scrubtext) |
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
[MIT](./LICENSE) © Tung Tran
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
CHAR_MAP: () => CHAR_MAP,
|
|
24
|
+
Slugger: () => Slugger,
|
|
25
|
+
createSlugger: () => createSlugger,
|
|
26
|
+
slug: () => slug
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/charmap.ts
|
|
31
|
+
var CHAR_MAP = {
|
|
32
|
+
// Latin letters without a clean NFKD decomposition.
|
|
33
|
+
\u0111: "d",
|
|
34
|
+
\u0110: "D",
|
|
35
|
+
\u00F0: "d",
|
|
36
|
+
\u00D0: "D",
|
|
37
|
+
\u00F8: "o",
|
|
38
|
+
\u00D8: "O",
|
|
39
|
+
\u0142: "l",
|
|
40
|
+
\u0141: "L",
|
|
41
|
+
\u00DF: "ss",
|
|
42
|
+
"\u1E9E": "SS",
|
|
43
|
+
\u00E6: "ae",
|
|
44
|
+
\u00C6: "AE",
|
|
45
|
+
\u0153: "oe",
|
|
46
|
+
\u0152: "OE",
|
|
47
|
+
\u00FE: "th",
|
|
48
|
+
\u00DE: "TH",
|
|
49
|
+
\u0131: "i",
|
|
50
|
+
\u0149: "n",
|
|
51
|
+
// Common symbols people expect to read as words.
|
|
52
|
+
"&": "and",
|
|
53
|
+
"@": "at",
|
|
54
|
+
"%": "percent",
|
|
55
|
+
"\u20AB": "dong",
|
|
56
|
+
"\u20AC": "euro",
|
|
57
|
+
$: "dollar",
|
|
58
|
+
"\xA3": "pound",
|
|
59
|
+
"\xA9": "c",
|
|
60
|
+
"\xAE": "r",
|
|
61
|
+
"\u2122": "tm"
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/slug.ts
|
|
65
|
+
var COMBINING_MARKS = /[̀-ͯ]/g;
|
|
66
|
+
function slug(input, options = {}) {
|
|
67
|
+
const separator = options.separator ?? "-";
|
|
68
|
+
const lower = options.lower ?? true;
|
|
69
|
+
const symbols = options.symbols ?? true;
|
|
70
|
+
const map = options.charMap ? { ...CHAR_MAP, ...options.charMap } : CHAR_MAP;
|
|
71
|
+
let result = "";
|
|
72
|
+
for (const char of input.normalize("NFKC")) {
|
|
73
|
+
if (!Object.prototype.hasOwnProperty.call(map, char)) {
|
|
74
|
+
result += char;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const replacement = map[char];
|
|
78
|
+
if (new RegExp("^\\p{L}", "u").test(char)) {
|
|
79
|
+
result += replacement;
|
|
80
|
+
} else if (symbols) {
|
|
81
|
+
result += ` ${replacement} `;
|
|
82
|
+
} else {
|
|
83
|
+
result += " ";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
result = result.normalize("NFKD").replace(COMBINING_MARKS, "");
|
|
87
|
+
if (lower) result = result.toLowerCase();
|
|
88
|
+
result = result.replace(/[^\p{L}\p{N}]+/gu, separator).replace(new RegExp(`\\${separator}{2,}`, "g"), separator);
|
|
89
|
+
result = trimSeparator(result, separator);
|
|
90
|
+
if (options.maxLength !== void 0 && result.length > options.maxLength) {
|
|
91
|
+
let cut = result.slice(0, options.maxLength);
|
|
92
|
+
const nextChar = result.slice(options.maxLength, options.maxLength + separator.length);
|
|
93
|
+
if (separator !== "" && nextChar !== separator) {
|
|
94
|
+
const lastSep = cut.lastIndexOf(separator);
|
|
95
|
+
if (lastSep > 0) cut = cut.slice(0, lastSep);
|
|
96
|
+
}
|
|
97
|
+
result = trimSeparator(cut, separator);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
function trimSeparator(value, separator) {
|
|
102
|
+
if (separator === "") return value;
|
|
103
|
+
let start = 0;
|
|
104
|
+
let end = value.length;
|
|
105
|
+
while (value.startsWith(separator, start)) start += separator.length;
|
|
106
|
+
while (end >= start + separator.length && value.endsWith(separator, end)) {
|
|
107
|
+
end -= separator.length;
|
|
108
|
+
}
|
|
109
|
+
return value.slice(start, end);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/slugger.ts
|
|
113
|
+
var Slugger = class {
|
|
114
|
+
#seen = /* @__PURE__ */ new Map();
|
|
115
|
+
#options;
|
|
116
|
+
constructor(options = {}) {
|
|
117
|
+
this.#options = options;
|
|
118
|
+
}
|
|
119
|
+
/** Slugify `input`, appending a counter when the base slug was already used. */
|
|
120
|
+
slug(input, options) {
|
|
121
|
+
const separator = options?.separator ?? this.#options.separator ?? "-";
|
|
122
|
+
const base = slug(input, { ...this.#options, ...options });
|
|
123
|
+
const count = this.#seen.get(base);
|
|
124
|
+
if (count === void 0) {
|
|
125
|
+
this.#seen.set(base, 1);
|
|
126
|
+
return base;
|
|
127
|
+
}
|
|
128
|
+
let n = count + 1;
|
|
129
|
+
let candidate = `${base}${separator}${n}`;
|
|
130
|
+
while (this.#seen.has(candidate)) {
|
|
131
|
+
n += 1;
|
|
132
|
+
candidate = `${base}${separator}${n}`;
|
|
133
|
+
}
|
|
134
|
+
this.#seen.set(base, n);
|
|
135
|
+
this.#seen.set(candidate, 1);
|
|
136
|
+
return candidate;
|
|
137
|
+
}
|
|
138
|
+
/** Whether a fully-resolved slug has been produced already. */
|
|
139
|
+
has(value) {
|
|
140
|
+
return this.#seen.has(value);
|
|
141
|
+
}
|
|
142
|
+
/** Forget all previously seen slugs. */
|
|
143
|
+
reset() {
|
|
144
|
+
this.#seen.clear();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
function createSlugger(options) {
|
|
148
|
+
return new Slugger(options);
|
|
149
|
+
}
|
|
150
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
151
|
+
0 && (module.exports = {
|
|
152
|
+
CHAR_MAP,
|
|
153
|
+
Slugger,
|
|
154
|
+
createSlugger,
|
|
155
|
+
slug
|
|
156
|
+
});
|
|
157
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/charmap.ts","../src/slug.ts","../src/slugger.ts"],"sourcesContent":["export { slug, type SlugOptions } from \"./slug.js\";\nexport { Slugger, createSlugger } from \"./slugger.js\";\nexport { CHAR_MAP } from \"./charmap.js\";\n","/**\n * Characters that Unicode NFKD normalization does **not** decompose into an\n * ASCII base letter plus combining marks, so we map them explicitly. Most\n * accented letters (é, ñ, ü, …) are handled by normalization; this covers the\n * stubborn ones (đ, ø, ß, æ, ł, …) and a few useful symbol words.\n */\nexport const CHAR_MAP: Readonly<Record<string, string>> = {\n // Latin letters without a clean NFKD decomposition.\n đ: \"d\",\n Đ: \"D\",\n ð: \"d\",\n Ð: \"D\",\n ø: \"o\",\n Ø: \"O\",\n ł: \"l\",\n Ł: \"L\",\n ß: \"ss\",\n ẞ: \"SS\",\n æ: \"ae\",\n Æ: \"AE\",\n œ: \"oe\",\n Œ: \"OE\",\n þ: \"th\",\n Þ: \"TH\",\n ı: \"i\",\n ʼn: \"n\",\n // Common symbols people expect to read as words.\n \"&\": \"and\",\n \"@\": \"at\",\n \"%\": \"percent\",\n \"₫\": \"dong\",\n \"€\": \"euro\",\n $: \"dollar\",\n \"£\": \"pound\",\n \"©\": \"c\",\n \"®\": \"r\",\n \"™\": \"tm\",\n};\n","import { CHAR_MAP } from \"./charmap.js\";\n\nexport interface SlugOptions {\n /** Word separator. Default `\"-\"`. */\n separator?: string;\n /** Lowercase the result. Default `true`. */\n lower?: boolean;\n /** Expand symbols like `&` → `and`, `%` → `percent`. Default `true`. */\n symbols?: boolean;\n /** Truncate to at most this many characters (never ends on a separator). */\n maxLength?: number;\n /** Extra replacements applied before slugifying (merged over the defaults). */\n charMap?: Record<string, string>;\n}\n\nconst COMBINING_MARKS = /[̀-ͯ]/g;\n\n/**\n * Convert arbitrary text into a clean, URL-safe slug.\n *\n * Folds Unicode accents (`café` → `cafe`, `Việt Nam` → `viet-nam`), maps\n * stubborn letters and symbols (`đ` → `d`, `&` → `and`), lowercases, and joins\n * words with a separator. Zero dependencies.\n *\n * ```ts\n * slug(\"Hello, World!\"); // \"hello-world\"\n * slug(\"Crème brûlée & coffee\"); // \"creme-brulee-and-coffee\"\n * slug(\"Đặng Văn A\"); // \"dang-van-a\"\n * ```\n */\nexport function slug(input: string, options: SlugOptions = {}): string {\n const separator = options.separator ?? \"-\";\n const lower = options.lower ?? true;\n const symbols = options.symbols ?? true;\n const map = options.charMap ? { ...CHAR_MAP, ...options.charMap } : CHAR_MAP;\n\n let result = \"\";\n // Iterate by code point so multi-byte characters map correctly.\n for (const char of input.normalize(\"NFKC\")) {\n if (!Object.prototype.hasOwnProperty.call(map, char)) {\n result += char;\n continue;\n }\n const replacement = map[char] as string;\n if (/^\\p{L}/u.test(char)) {\n // Letter substitution (đ → d): glue it to its neighbours.\n result += replacement;\n } else if (symbols) {\n // Symbol word (& → and): pad so it becomes its own slug word.\n result += ` ${replacement} `;\n } else {\n result += \" \";\n }\n }\n\n // Decompose remaining accents and strip the combining marks.\n result = result.normalize(\"NFKD\").replace(COMBINING_MARKS, \"\");\n\n if (lower) result = result.toLowerCase();\n\n // Everything that is not a letter or number becomes a separator boundary.\n result = result\n .replace(/[^\\p{L}\\p{N}]+/gu, separator)\n .replace(new RegExp(`\\\\${separator}{2,}`, \"g\"), separator);\n\n // Trim separators from both ends.\n result = trimSeparator(result, separator);\n\n if (options.maxLength !== undefined && result.length > options.maxLength) {\n let cut = result.slice(0, options.maxLength);\n // If the cut landed mid-word, fall back to the last whole word.\n const nextChar = result.slice(options.maxLength, options.maxLength + separator.length);\n if (separator !== \"\" && nextChar !== separator) {\n const lastSep = cut.lastIndexOf(separator);\n if (lastSep > 0) cut = cut.slice(0, lastSep);\n }\n result = trimSeparator(cut, separator);\n }\n return result;\n}\n\nfunction trimSeparator(value: string, separator: string): string {\n if (separator === \"\") return value;\n let start = 0;\n let end = value.length;\n while (value.startsWith(separator, start)) start += separator.length;\n while (end >= start + separator.length && value.endsWith(separator, end)) {\n end -= separator.length;\n }\n return value.slice(start, end);\n}\n","import { slug, type SlugOptions } from \"./slug.js\";\n\n/**\n * A stateful slug generator that guarantees **unique** output. Repeated inputs\n * get a numeric suffix (`-2`, `-3`, …) — ideal for headings, anchors, or any\n * set of slugs that must not collide.\n *\n * ```ts\n * const slugger = new Slugger();\n * slugger.slug(\"Hello\"); // \"hello\"\n * slugger.slug(\"Hello\"); // \"hello-2\"\n * slugger.slug(\"Hello\"); // \"hello-3\"\n * ```\n */\nexport class Slugger {\n #seen = new Map<string, number>();\n #options: SlugOptions;\n\n constructor(options: SlugOptions = {}) {\n this.#options = options;\n }\n\n /** Slugify `input`, appending a counter when the base slug was already used. */\n slug(input: string, options?: SlugOptions): string {\n const separator = options?.separator ?? this.#options.separator ?? \"-\";\n const base = slug(input, { ...this.#options, ...options });\n\n const count = this.#seen.get(base);\n if (count === undefined) {\n this.#seen.set(base, 1);\n return base;\n }\n\n // Find the next free `${base}${separator}${n}`.\n let n = count + 1;\n let candidate = `${base}${separator}${n}`;\n while (this.#seen.has(candidate)) {\n n += 1;\n candidate = `${base}${separator}${n}`;\n }\n this.#seen.set(base, n);\n this.#seen.set(candidate, 1);\n return candidate;\n }\n\n /** Whether a fully-resolved slug has been produced already. */\n has(value: string): boolean {\n return this.#seen.has(value);\n }\n\n /** Forget all previously seen slugs. */\n reset(): void {\n this.#seen.clear();\n }\n}\n\n/** Convenience factory for {@link Slugger}. */\nexport function createSlugger(options?: SlugOptions): Slugger {\n return new Slugger(options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,WAA6C;AAAA;AAAA,EAExD,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,UAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,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ACT;AAAA;AAAA,EAGA,IAAI,OAAwB;AAC1B,WAAO,KAAK,MAAM,IAAI,KAAK;AAAA,EAC7B;AAAA;AAAA,EAGA,QAAc;AACZ,SAAK,MAAM,MAAM;AAAA,EACnB;AACF;AAGO,SAAS,cAAc,SAAgC;AAC5D,SAAO,IAAI,QAAQ,OAAO;AAC5B;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
interface SlugOptions {
|
|
2
|
+
/** Word separator. Default `"-"`. */
|
|
3
|
+
separator?: string;
|
|
4
|
+
/** Lowercase the result. Default `true`. */
|
|
5
|
+
lower?: boolean;
|
|
6
|
+
/** Expand symbols like `&` → `and`, `%` → `percent`. Default `true`. */
|
|
7
|
+
symbols?: boolean;
|
|
8
|
+
/** Truncate to at most this many characters (never ends on a separator). */
|
|
9
|
+
maxLength?: number;
|
|
10
|
+
/** Extra replacements applied before slugifying (merged over the defaults). */
|
|
11
|
+
charMap?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Convert arbitrary text into a clean, URL-safe slug.
|
|
15
|
+
*
|
|
16
|
+
* Folds Unicode accents (`café` → `cafe`, `Việt Nam` → `viet-nam`), maps
|
|
17
|
+
* stubborn letters and symbols (`đ` → `d`, `&` → `and`), lowercases, and joins
|
|
18
|
+
* words with a separator. Zero dependencies.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* slug("Hello, World!"); // "hello-world"
|
|
22
|
+
* slug("Crème brûlée & coffee"); // "creme-brulee-and-coffee"
|
|
23
|
+
* slug("Đặng Văn A"); // "dang-van-a"
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare function slug(input: string, options?: SlugOptions): string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A stateful slug generator that guarantees **unique** output. Repeated inputs
|
|
30
|
+
* get a numeric suffix (`-2`, `-3`, …) — ideal for headings, anchors, or any
|
|
31
|
+
* set of slugs that must not collide.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const slugger = new Slugger();
|
|
35
|
+
* slugger.slug("Hello"); // "hello"
|
|
36
|
+
* slugger.slug("Hello"); // "hello-2"
|
|
37
|
+
* slugger.slug("Hello"); // "hello-3"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare class Slugger {
|
|
41
|
+
#private;
|
|
42
|
+
constructor(options?: SlugOptions);
|
|
43
|
+
/** Slugify `input`, appending a counter when the base slug was already used. */
|
|
44
|
+
slug(input: string, options?: SlugOptions): string;
|
|
45
|
+
/** Whether a fully-resolved slug has been produced already. */
|
|
46
|
+
has(value: string): boolean;
|
|
47
|
+
/** Forget all previously seen slugs. */
|
|
48
|
+
reset(): void;
|
|
49
|
+
}
|
|
50
|
+
/** Convenience factory for {@link Slugger}. */
|
|
51
|
+
declare function createSlugger(options?: SlugOptions): Slugger;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Characters that Unicode NFKD normalization does **not** decompose into an
|
|
55
|
+
* ASCII base letter plus combining marks, so we map them explicitly. Most
|
|
56
|
+
* accented letters (é, ñ, ü, …) are handled by normalization; this covers the
|
|
57
|
+
* stubborn ones (đ, ø, ß, æ, ł, …) and a few useful symbol words.
|
|
58
|
+
*/
|
|
59
|
+
declare const CHAR_MAP: Readonly<Record<string, string>>;
|
|
60
|
+
|
|
61
|
+
export { CHAR_MAP, type SlugOptions, Slugger, createSlugger, slug };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
interface SlugOptions {
|
|
2
|
+
/** Word separator. Default `"-"`. */
|
|
3
|
+
separator?: string;
|
|
4
|
+
/** Lowercase the result. Default `true`. */
|
|
5
|
+
lower?: boolean;
|
|
6
|
+
/** Expand symbols like `&` → `and`, `%` → `percent`. Default `true`. */
|
|
7
|
+
symbols?: boolean;
|
|
8
|
+
/** Truncate to at most this many characters (never ends on a separator). */
|
|
9
|
+
maxLength?: number;
|
|
10
|
+
/** Extra replacements applied before slugifying (merged over the defaults). */
|
|
11
|
+
charMap?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Convert arbitrary text into a clean, URL-safe slug.
|
|
15
|
+
*
|
|
16
|
+
* Folds Unicode accents (`café` → `cafe`, `Việt Nam` → `viet-nam`), maps
|
|
17
|
+
* stubborn letters and symbols (`đ` → `d`, `&` → `and`), lowercases, and joins
|
|
18
|
+
* words with a separator. Zero dependencies.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* slug("Hello, World!"); // "hello-world"
|
|
22
|
+
* slug("Crème brûlée & coffee"); // "creme-brulee-and-coffee"
|
|
23
|
+
* slug("Đặng Văn A"); // "dang-van-a"
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare function slug(input: string, options?: SlugOptions): string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A stateful slug generator that guarantees **unique** output. Repeated inputs
|
|
30
|
+
* get a numeric suffix (`-2`, `-3`, …) — ideal for headings, anchors, or any
|
|
31
|
+
* set of slugs that must not collide.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const slugger = new Slugger();
|
|
35
|
+
* slugger.slug("Hello"); // "hello"
|
|
36
|
+
* slugger.slug("Hello"); // "hello-2"
|
|
37
|
+
* slugger.slug("Hello"); // "hello-3"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare class Slugger {
|
|
41
|
+
#private;
|
|
42
|
+
constructor(options?: SlugOptions);
|
|
43
|
+
/** Slugify `input`, appending a counter when the base slug was already used. */
|
|
44
|
+
slug(input: string, options?: SlugOptions): string;
|
|
45
|
+
/** Whether a fully-resolved slug has been produced already. */
|
|
46
|
+
has(value: string): boolean;
|
|
47
|
+
/** Forget all previously seen slugs. */
|
|
48
|
+
reset(): void;
|
|
49
|
+
}
|
|
50
|
+
/** Convenience factory for {@link Slugger}. */
|
|
51
|
+
declare function createSlugger(options?: SlugOptions): Slugger;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Characters that Unicode NFKD normalization does **not** decompose into an
|
|
55
|
+
* ASCII base letter plus combining marks, so we map them explicitly. Most
|
|
56
|
+
* accented letters (é, ñ, ü, …) are handled by normalization; this covers the
|
|
57
|
+
* stubborn ones (đ, ø, ß, æ, ł, …) and a few useful symbol words.
|
|
58
|
+
*/
|
|
59
|
+
declare const CHAR_MAP: Readonly<Record<string, string>>;
|
|
60
|
+
|
|
61
|
+
export { CHAR_MAP, type SlugOptions, Slugger, createSlugger, slug };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// src/charmap.ts
|
|
2
|
+
var CHAR_MAP = {
|
|
3
|
+
// Latin letters without a clean NFKD decomposition.
|
|
4
|
+
\u0111: "d",
|
|
5
|
+
\u0110: "D",
|
|
6
|
+
\u00F0: "d",
|
|
7
|
+
\u00D0: "D",
|
|
8
|
+
\u00F8: "o",
|
|
9
|
+
\u00D8: "O",
|
|
10
|
+
\u0142: "l",
|
|
11
|
+
\u0141: "L",
|
|
12
|
+
\u00DF: "ss",
|
|
13
|
+
"\u1E9E": "SS",
|
|
14
|
+
\u00E6: "ae",
|
|
15
|
+
\u00C6: "AE",
|
|
16
|
+
\u0153: "oe",
|
|
17
|
+
\u0152: "OE",
|
|
18
|
+
\u00FE: "th",
|
|
19
|
+
\u00DE: "TH",
|
|
20
|
+
\u0131: "i",
|
|
21
|
+
\u0149: "n",
|
|
22
|
+
// Common symbols people expect to read as words.
|
|
23
|
+
"&": "and",
|
|
24
|
+
"@": "at",
|
|
25
|
+
"%": "percent",
|
|
26
|
+
"\u20AB": "dong",
|
|
27
|
+
"\u20AC": "euro",
|
|
28
|
+
$: "dollar",
|
|
29
|
+
"\xA3": "pound",
|
|
30
|
+
"\xA9": "c",
|
|
31
|
+
"\xAE": "r",
|
|
32
|
+
"\u2122": "tm"
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/slug.ts
|
|
36
|
+
var COMBINING_MARKS = /[̀-ͯ]/g;
|
|
37
|
+
function slug(input, options = {}) {
|
|
38
|
+
const separator = options.separator ?? "-";
|
|
39
|
+
const lower = options.lower ?? true;
|
|
40
|
+
const symbols = options.symbols ?? true;
|
|
41
|
+
const map = options.charMap ? { ...CHAR_MAP, ...options.charMap } : CHAR_MAP;
|
|
42
|
+
let result = "";
|
|
43
|
+
for (const char of input.normalize("NFKC")) {
|
|
44
|
+
if (!Object.prototype.hasOwnProperty.call(map, char)) {
|
|
45
|
+
result += char;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const replacement = map[char];
|
|
49
|
+
if (new RegExp("^\\p{L}", "u").test(char)) {
|
|
50
|
+
result += replacement;
|
|
51
|
+
} else if (symbols) {
|
|
52
|
+
result += ` ${replacement} `;
|
|
53
|
+
} else {
|
|
54
|
+
result += " ";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
result = result.normalize("NFKD").replace(COMBINING_MARKS, "");
|
|
58
|
+
if (lower) result = result.toLowerCase();
|
|
59
|
+
result = result.replace(/[^\p{L}\p{N}]+/gu, separator).replace(new RegExp(`\\${separator}{2,}`, "g"), separator);
|
|
60
|
+
result = trimSeparator(result, separator);
|
|
61
|
+
if (options.maxLength !== void 0 && result.length > options.maxLength) {
|
|
62
|
+
let cut = result.slice(0, options.maxLength);
|
|
63
|
+
const nextChar = result.slice(options.maxLength, options.maxLength + separator.length);
|
|
64
|
+
if (separator !== "" && nextChar !== separator) {
|
|
65
|
+
const lastSep = cut.lastIndexOf(separator);
|
|
66
|
+
if (lastSep > 0) cut = cut.slice(0, lastSep);
|
|
67
|
+
}
|
|
68
|
+
result = trimSeparator(cut, separator);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
function trimSeparator(value, separator) {
|
|
73
|
+
if (separator === "") return value;
|
|
74
|
+
let start = 0;
|
|
75
|
+
let end = value.length;
|
|
76
|
+
while (value.startsWith(separator, start)) start += separator.length;
|
|
77
|
+
while (end >= start + separator.length && value.endsWith(separator, end)) {
|
|
78
|
+
end -= separator.length;
|
|
79
|
+
}
|
|
80
|
+
return value.slice(start, end);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/slugger.ts
|
|
84
|
+
var Slugger = class {
|
|
85
|
+
#seen = /* @__PURE__ */ new Map();
|
|
86
|
+
#options;
|
|
87
|
+
constructor(options = {}) {
|
|
88
|
+
this.#options = options;
|
|
89
|
+
}
|
|
90
|
+
/** Slugify `input`, appending a counter when the base slug was already used. */
|
|
91
|
+
slug(input, options) {
|
|
92
|
+
const separator = options?.separator ?? this.#options.separator ?? "-";
|
|
93
|
+
const base = slug(input, { ...this.#options, ...options });
|
|
94
|
+
const count = this.#seen.get(base);
|
|
95
|
+
if (count === void 0) {
|
|
96
|
+
this.#seen.set(base, 1);
|
|
97
|
+
return base;
|
|
98
|
+
}
|
|
99
|
+
let n = count + 1;
|
|
100
|
+
let candidate = `${base}${separator}${n}`;
|
|
101
|
+
while (this.#seen.has(candidate)) {
|
|
102
|
+
n += 1;
|
|
103
|
+
candidate = `${base}${separator}${n}`;
|
|
104
|
+
}
|
|
105
|
+
this.#seen.set(base, n);
|
|
106
|
+
this.#seen.set(candidate, 1);
|
|
107
|
+
return candidate;
|
|
108
|
+
}
|
|
109
|
+
/** Whether a fully-resolved slug has been produced already. */
|
|
110
|
+
has(value) {
|
|
111
|
+
return this.#seen.has(value);
|
|
112
|
+
}
|
|
113
|
+
/** Forget all previously seen slugs. */
|
|
114
|
+
reset() {
|
|
115
|
+
this.#seen.clear();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
function createSlugger(options) {
|
|
119
|
+
return new Slugger(options);
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
CHAR_MAP,
|
|
123
|
+
Slugger,
|
|
124
|
+
createSlugger,
|
|
125
|
+
slug
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/charmap.ts","../src/slug.ts","../src/slugger.ts"],"sourcesContent":["/**\n * Characters that Unicode NFKD normalization does **not** decompose into an\n * ASCII base letter plus combining marks, so we map them explicitly. Most\n * accented letters (é, ñ, ü, …) are handled by normalization; this covers the\n * stubborn ones (đ, ø, ß, æ, ł, …) and a few useful symbol words.\n */\nexport const CHAR_MAP: Readonly<Record<string, string>> = {\n // Latin letters without a clean NFKD decomposition.\n đ: \"d\",\n Đ: \"D\",\n ð: \"d\",\n Ð: \"D\",\n ø: \"o\",\n Ø: \"O\",\n ł: \"l\",\n Ł: \"L\",\n ß: \"ss\",\n ẞ: \"SS\",\n æ: \"ae\",\n Æ: \"AE\",\n œ: \"oe\",\n Œ: \"OE\",\n þ: \"th\",\n Þ: \"TH\",\n ı: \"i\",\n ʼn: \"n\",\n // Common symbols people expect to read as words.\n \"&\": \"and\",\n \"@\": \"at\",\n \"%\": \"percent\",\n \"₫\": \"dong\",\n \"€\": \"euro\",\n $: \"dollar\",\n \"£\": \"pound\",\n \"©\": \"c\",\n \"®\": \"r\",\n \"™\": \"tm\",\n};\n","import { CHAR_MAP } from \"./charmap.js\";\n\nexport interface SlugOptions {\n /** Word separator. Default `\"-\"`. */\n separator?: string;\n /** Lowercase the result. Default `true`. */\n lower?: boolean;\n /** Expand symbols like `&` → `and`, `%` → `percent`. Default `true`. */\n symbols?: boolean;\n /** Truncate to at most this many characters (never ends on a separator). */\n maxLength?: number;\n /** Extra replacements applied before slugifying (merged over the defaults). */\n charMap?: Record<string, string>;\n}\n\nconst COMBINING_MARKS = /[̀-ͯ]/g;\n\n/**\n * Convert arbitrary text into a clean, URL-safe slug.\n *\n * Folds Unicode accents (`café` → `cafe`, `Việt Nam` → `viet-nam`), maps\n * stubborn letters and symbols (`đ` → `d`, `&` → `and`), lowercases, and joins\n * words with a separator. Zero dependencies.\n *\n * ```ts\n * slug(\"Hello, World!\"); // \"hello-world\"\n * slug(\"Crème brûlée & coffee\"); // \"creme-brulee-and-coffee\"\n * slug(\"Đặng Văn A\"); // \"dang-van-a\"\n * ```\n */\nexport function slug(input: string, options: SlugOptions = {}): string {\n const separator = options.separator ?? \"-\";\n const lower = options.lower ?? true;\n const symbols = options.symbols ?? true;\n const map = options.charMap ? { ...CHAR_MAP, ...options.charMap } : CHAR_MAP;\n\n let result = \"\";\n // Iterate by code point so multi-byte characters map correctly.\n for (const char of input.normalize(\"NFKC\")) {\n if (!Object.prototype.hasOwnProperty.call(map, char)) {\n result += char;\n continue;\n }\n const replacement = map[char] as string;\n if (/^\\p{L}/u.test(char)) {\n // Letter substitution (đ → d): glue it to its neighbours.\n result += replacement;\n } else if (symbols) {\n // Symbol word (& → and): pad so it becomes its own slug word.\n result += ` ${replacement} `;\n } else {\n result += \" \";\n }\n }\n\n // Decompose remaining accents and strip the combining marks.\n result = result.normalize(\"NFKD\").replace(COMBINING_MARKS, \"\");\n\n if (lower) result = result.toLowerCase();\n\n // Everything that is not a letter or number becomes a separator boundary.\n result = result\n .replace(/[^\\p{L}\\p{N}]+/gu, separator)\n .replace(new RegExp(`\\\\${separator}{2,}`, \"g\"), separator);\n\n // Trim separators from both ends.\n result = trimSeparator(result, separator);\n\n if (options.maxLength !== undefined && result.length > options.maxLength) {\n let cut = result.slice(0, options.maxLength);\n // If the cut landed mid-word, fall back to the last whole word.\n const nextChar = result.slice(options.maxLength, options.maxLength + separator.length);\n if (separator !== \"\" && nextChar !== separator) {\n const lastSep = cut.lastIndexOf(separator);\n if (lastSep > 0) cut = cut.slice(0, lastSep);\n }\n result = trimSeparator(cut, separator);\n }\n return result;\n}\n\nfunction trimSeparator(value: string, separator: string): string {\n if (separator === \"\") return value;\n let start = 0;\n let end = value.length;\n while (value.startsWith(separator, start)) start += separator.length;\n while (end >= start + separator.length && value.endsWith(separator, end)) {\n end -= separator.length;\n }\n return value.slice(start, end);\n}\n","import { slug, type SlugOptions } from \"./slug.js\";\n\n/**\n * A stateful slug generator that guarantees **unique** output. Repeated inputs\n * get a numeric suffix (`-2`, `-3`, …) — ideal for headings, anchors, or any\n * set of slugs that must not collide.\n *\n * ```ts\n * const slugger = new Slugger();\n * slugger.slug(\"Hello\"); // \"hello\"\n * slugger.slug(\"Hello\"); // \"hello-2\"\n * slugger.slug(\"Hello\"); // \"hello-3\"\n * ```\n */\nexport class Slugger {\n #seen = new Map<string, number>();\n #options: SlugOptions;\n\n constructor(options: SlugOptions = {}) {\n this.#options = options;\n }\n\n /** Slugify `input`, appending a counter when the base slug was already used. */\n slug(input: string, options?: SlugOptions): string {\n const separator = options?.separator ?? this.#options.separator ?? \"-\";\n const base = slug(input, { ...this.#options, ...options });\n\n const count = this.#seen.get(base);\n if (count === undefined) {\n this.#seen.set(base, 1);\n return base;\n }\n\n // Find the next free `${base}${separator}${n}`.\n let n = count + 1;\n let candidate = `${base}${separator}${n}`;\n while (this.#seen.has(candidate)) {\n n += 1;\n candidate = `${base}${separator}${n}`;\n }\n this.#seen.set(base, n);\n this.#seen.set(candidate, 1);\n return candidate;\n }\n\n /** Whether a fully-resolved slug has been produced already. */\n has(value: string): boolean {\n return this.#seen.has(value);\n }\n\n /** Forget all previously seen slugs. */\n reset(): void {\n this.#seen.clear();\n }\n}\n\n/** Convenience factory for {@link Slugger}. */\nexport function createSlugger(options?: SlugOptions): Slugger {\n return new Slugger(options);\n}\n"],"mappings":";AAMO,IAAM,WAA6C;AAAA;AAAA,EAExD,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,UAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA;AAAA,EAEH,K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AAA,EAGA,QAAc;AACZ,SAAK,MAAM,MAAM;AAAA,EACnB;AACF;AAGO,SAAS,cAAc,SAAgC;AAC5D,SAAO,IAAI,QAAQ,OAAO;AAC5B;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "slugkit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tiny, type-safe slugify — Unicode accent folding, symbol words, and a unique-slug generator. Zero dependencies.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"slug",
|
|
7
|
+
"slugify",
|
|
8
|
+
"url-slug",
|
|
9
|
+
"permalink",
|
|
10
|
+
"unicode",
|
|
11
|
+
"diacritics",
|
|
12
|
+
"transliterate",
|
|
13
|
+
"unique",
|
|
14
|
+
"anchor",
|
|
15
|
+
"typescript",
|
|
16
|
+
"zero-dependency"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Tung Tran (https://github.com/trananhtung)",
|
|
20
|
+
"homepage": "https://github.com/trananhtung/slugkit#readme",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/trananhtung/slugkit.git"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/trananhtung/slugkit/issues"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"require": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"dev": "tsup --watch",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"prepublishOnly": "npm run build"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^20.17.10",
|
|
57
|
+
"tsup": "^8.3.5",
|
|
58
|
+
"typescript": "^5.7.2",
|
|
59
|
+
"vitest": "^2.1.8"
|
|
60
|
+
},
|
|
61
|
+
"sideEffects": false,
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
}
|
|
65
|
+
}
|