UNPKG

4.6 kBTypeScriptView Raw
1export interface Options {
2 /**
3 @default '-'
4
5 @example
6 ```
7 import slugify from '@sindresorhus/slugify';
8
9 slugify('BAR and baz');
10 //=> 'bar-and-baz'
11
12 slugify('BAR and baz', {separator: '_'});
13 //=> 'bar_and_baz'
14
15 slugify('BAR and baz', {separator: ''});
16 //=> 'barandbaz'
17 ```
18 */
19 readonly separator?: string;
20
21 /**
22 Make the slug lowercase.
23
24 @default true
25
26 @example
27 ```
28 import slugify from '@sindresorhus/slugify';
29
30 slugify('Déjà Vu!');
31 //=> 'deja-vu'
32
33 slugify('Déjà Vu!', {lowercase: false});
34 //=> 'Deja-Vu'
35 ```
36 */
37 readonly lowercase?: boolean;
38
39 /**
40 Convert camelcase to separate words. Internally it does `fooBar` → `foo bar`.
41
42 @default true
43
44 @example
45 ```
46 import slugify from '@sindresorhus/slugify';
47
48 slugify('fooBar');
49 //=> 'foo-bar'
50
51 slugify('fooBar', {decamelize: false});
52 //=> 'foobar'
53 ```
54 */
55 readonly decamelize?: boolean;
56
57 /**
58 Add your own custom replacements.
59
60 The replacements are run on the original string before any other transformations.
61
62 This only overrides a default replacement if you set an item with the same key, like `&`.
63
64 Add a leading and trailing space to the replacement to have it separated by dashes.
65
66 @default [ ['&', ' and '], ['🦄', ' unicorn '], ['♥', ' love '] ]
67
68 @example
69 ```
70 import slugify from '@sindresorhus/slugify';
71
72 slugify('Foo@unicorn', {
73 customReplacements: [
74 ['@', 'at']
75 ]
76 });
77 //=> 'fooatunicorn'
78
79 slugify('foo@unicorn', {
80 customReplacements: [
81 ['@', ' at ']
82 ]
83 });
84 //=> 'foo-at-unicorn'
85
86 slugify('I love 🐶', {
87 customReplacements: [
88 ['🐶', 'dogs']
89 ]
90 });
91 //=> 'i-love-dogs'
92 ```
93 */
94 readonly customReplacements?: ReadonlyArray<[string, string]>;
95
96 /**
97 If your string starts with an underscore, it will be preserved in the slugified string.
98
99 Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
100
101 @default false
102
103 @example
104 ```
105 import slugify from '@sindresorhus/slugify';
106
107 slugify('_foo_bar');
108 //=> 'foo-bar'
109
110 slugify('_foo_bar', {preserveLeadingUnderscore: true});
111 //=> '_foo-bar'
112 ```
113 */
114 readonly preserveLeadingUnderscore?: boolean;
115
116 /**
117 If your string ends with a dash, it will be preserved in the slugified string.
118
119 For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
120
121 @default false
122
123 @example
124 ```
125 import slugify from '@sindresorhus/slugify';
126
127 slugify('foo-bar-');
128 //=> 'foo-bar'
129
130 slugify('foo-bar-', {preserveTrailingDash: true});
131 //=> 'foo-bar-'
132 ```
133 */
134 readonly preserveTrailingDash?: boolean;
135
136 /**
137 Preserve certain characters.
138
139 It cannot contain the `separator`.
140
141 For example, if you want to slugify URLs, but preserve the HTML fragment `#` character, you could set `preserveCharacters: ['#']`.
142
143 @default []
144
145 @example
146 ```
147 import slugify from '@sindresorhus/slugify';
148
149 slugify('foo_bar#baz', {preserveCharacters: ['#']});
150 //=> 'foo-bar#baz'
151 ```
152 */
153 readonly preserveCharacters?: string[];
154}
155
156/**
157Slugify a string.
158
159@param string - String to slugify.
160
161@example
162```
163import slugify from '@sindresorhus/slugify';
164
165slugify('I ♥ Dogs');
166//=> 'i-love-dogs'
167
168slugify(' Déjà Vu! ');
169//=> 'deja-vu'
170
171slugify('fooBar 123 $#%');
172//=> 'foo-bar-123'
173
174slugify('я люблю единорогов');
175//=> 'ya-lyublyu-edinorogov'
176```
177*/
178export default function slugify(string: string, options?: Options): string;
179
180export interface CountableSlugify {
181 /**
182 Reset the counter.
183
184 @example
185 ```
186 import {slugifyWithCounter} from '@sindresorhus/slugify';
187
188 const slugify = slugifyWithCounter();
189
190 slugify('foo bar');
191 //=> 'foo-bar'
192
193 slugify('foo bar');
194 //=> 'foo-bar-2'
195
196 slugify.reset();
197
198 slugify('foo bar');
199 //=> 'foo-bar'
200 ```
201 */
202 reset: () => void;
203
204 /**
205 Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurrences of the same string.
206
207 @param string - String to slugify.
208
209 @example
210 ```
211 import {slugifyWithCounter} from '@sindresorhus/slugify';
212
213 const slugify = slugifyWithCounter();
214
215 slugify('foo bar');
216 //=> 'foo-bar'
217
218 slugify('foo bar');
219 //=> 'foo-bar-2'
220
221 slugify.reset();
222
223 slugify('foo bar');
224 //=> 'foo-bar'
225 ```
226
227 __Use case example of counter__
228
229 If, for example, you have a document with multiple sections where each subsection has an example.
230
231 ```
232 ## Section 1
233
234 ### Example
235
236 ## Section 2
237
238 ### Example
239 ```
240
241 You can then use `slugifyWithCounter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
242 */
243 (string: string, options?: Options): string;
244}
245
246export function slugifyWithCounter(): CountableSlugify;