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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ slugify('some string', {
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`
strictReplace: false, // replace special characters except replacement with replacement character, defaults to `false`
locale: 'vi', // language code of the locale to use
trim: true // trim leading and trailing replacement chars, defaults to `true`
})
```

## Strict replace

Set `strictReplace` to `true` to replace special characters with the configured
replacement character instead of removing them. Consecutive replacements are
collapsed and leading or trailing ones are removed when `trim` is `true`.
When both `strict` and `strictReplace` are `true`, `strict` takes precedence.

```js
slugify('foo_bar. -@-baz!', {strictReplace: true}) // foo-bar-baz
```

## 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 @@ -16,6 +16,7 @@ declare function slugify(
remove?: RegExp;
lower?: boolean;
strict?: boolean;
strictReplace?: boolean;
locale?: string;
trim?: boolean;
}
Expand Down
4 changes: 4 additions & 0 deletions slugify.js

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

21 changes: 21 additions & 0 deletions test/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ describe('slugify', () => {
t.equal(slugify('foo_bar. -@-baz!', {strict: true}), 'foobar-baz')
})

it('options.strictReplace', () => {
t.equal(slugify(
'foo_bar. -@-baz!',
{strictReplace: true}
), 'foo-bar-baz')
})

it('options.strictReplace uses the configured replacement and trims it', () => {
t.equal(slugify(
'_foo@bar_',
{strictReplace: true, replacement: '_'}
), 'foo_bar')
})

it('options.strict takes precedence over options.strictReplace', () => {
t.equal(slugify(
'foo@bar',
{strict: true, strictReplace: true}
), 'foobar')
})

it('options.strict - remove duplicates of the replacement character', () => {
t.equal(slugify('foo @ bar', {strict: true}), 'foo-bar')
})
Expand Down