Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ slugify('some string', '_') // some_string
```js
slugify('some string', {
replacement: '-', // replace spaces with replacement character, defaults to `-`
preserveReplacement: false, // keep replacement characters already in the input, defaults to `false`
remove: undefined, // remove characters that match regex, defaults to `undefined`
lower: false, // convert to lower case, defaults to `false`
strict: false, // strip special characters except replacement, defaults to `false`
Expand All @@ -31,6 +32,11 @@ slugify('some string', {
})
```

Set `preserveReplacement: true` to keep repeated or trailing replacement
characters from the input. For example, `slugify('step -1',
{preserveReplacement: true})` returns `step--1` while `slugify('step 1')`
returns `step-1`. This option applies when `strict` is `false`.

## Remove

For example, to remove `*+~.()'"!:@` from the result slug, you can use `slugify('..', {remove: /[*+~.()'"!:@]/g})`.
Expand Down
1 change: 1 addition & 0 deletions slugify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare function slugify(
options?:
| {
replacement?: string;
preserveReplacement?: boolean;
remove?: RegExp;
lower?: boolean;
strict?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion slugify.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe('slugify', () => {
t.equal(slugify('foo bar baz', {replacement: ''}), 'foobarbaz')
})

it('options.preserveReplacement keeps existing separators', () => {
t.equal(slugify('1952--', {preserveReplacement: true}), '1952--')
t.equal(slugify('step -1', {preserveReplacement: true}), 'step--1')
t.equal(slugify('step 1', {preserveReplacement: true}), 'step-1')
t.equal(slugify('step _1', {
replacement: '_',
preserveReplacement: true
}), 'step__1')
t.equal(slugify('1952--'), '1952')
})

it('options.remove', () => {
t.equal(slugify(
'foo *+~.() bar \'"!:@ baz',
Expand Down