UNPKG

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