UNPKG

4.19 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/**
138Slugify a string.
139
140@param string - String to slugify.
141
142@example
143```
144import slugify from '@sindresorhus/slugify';
145
146slugify('I ♥ Dogs');
147//=> 'i-love-dogs'
148
149slugify(' Déjà Vu! ');
150//=> 'deja-vu'
151
152slugify('fooBar 123 $#%');
153//=> 'foo-bar-123'
154
155slugify('я люблю единорогов');
156//=> 'ya-lyublyu-edinorogov'
157```
158*/
159export default function slugify(string: string, options?: Options): string;
160
161export interface CountableSlugify {
162 /**
163 Reset the counter.
164
165 @example
166 ```
167 import {slugifyWithCounter} from '@sindresorhus/slugify';
168
169 const slugify = slugifyWithCounter();
170
171 slugify('foo bar');
172 //=> 'foo-bar'
173
174 slugify('foo bar');
175 //=> 'foo-bar-2'
176
177 slugify.reset();
178
179 slugify('foo bar');
180 //=> 'foo-bar'
181 ```
182 */
183 reset: () => void;
184
185 /**
186 Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurrences of the same string.
187
188 @param string - String to slugify.
189
190 @example
191 ```
192 import {slugifyWithCounter} from '@sindresorhus/slugify';
193
194 const slugify = slugifyWithCounter();
195
196 slugify('foo bar');
197 //=> 'foo-bar'
198
199 slugify('foo bar');
200 //=> 'foo-bar-2'
201
202 slugify.reset();
203
204 slugify('foo bar');
205 //=> 'foo-bar'
206 ```
207
208 __Use case example of counter__
209
210 If, for example, you have a document with multiple sections where each subsection has an example.
211
212 ```
213 ## Section 1
214
215 ### Example
216
217 ## Section 2
218
219 ### Example
220 ```
221
222 You can then use `slugifyWithCounter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
223 */
224 (string: string, options?: Options): string;
225}
226
227export function slugifyWithCounter(): CountableSlugify;