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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ slugify('some string', '_') // some_string
slugify('some string', {
replacement: '-', // replace spaces with replacement character, defaults to `-`
remove: undefined, // remove characters that match regex, defaults to `undefined`
replace: undefined,// replace characters that match regex with `replacement`
lower: false, // convert to lower case, defaults to `false`
strict: false, // strip special characters except replacement, defaults to `false`
locale: 'vi', // language code of the locale to use
Expand All @@ -44,6 +45,19 @@ For example, to remove `*+~.()'"!:@` from the result slug, you can use `slugify(
* If the value of `remove` is a string, it should be a single character.
Otherwise, the `remove` option might not work as expected.

## Replace

Use `replace` to turn matching parts of the input into the configured
replacement character. It is applied before character transliteration and
default filtering. For example:

```js
slugify('foo*+~bar.baz', {replace: /[*+~.]/g}) // foo-bar-baz
```

The same regular-expression guidance as `remove` applies: use a global
character-class regular expression, such as `/[*+~.]/g`.

## Locales

The main `charmap.json` file contains all known characters and their transliteration. All new characters should be added there first. In case you stumble upon a character already set in `charmap.json`, but not transliterated correctly according to your language, then you have to add those characters in `locales.json` to override the already existing transliteration in `charmap.json`, but for your locale only.
Expand Down
1 change: 1 addition & 0 deletions slugify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare function slugify(
| {
replacement?: string;
remove?: RegExp;
replace?: RegExp;
lower?: boolean;
strict?: boolean;
locale?: string;
Expand Down
6 changes: 6 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 @@ -64,6 +64,27 @@ describe('slugify', () => {
), 'foo-bar-bar-foo-foo-bar')
})

it('options.replace', () => {
t.equal(slugify(
'foo*+~bar.baz',
{replace: /[*+~.]/g}
), 'foo-bar-baz')
})

it('options.replace uses the configured replacement and trims it', () => {
t.equal(slugify(
'*foo*bar*',
{replace: /[*]/g, replacement: '_'}
), 'foo_bar')
})

it('options.replace runs before default character removal', () => {
t.equal(slugify(
'foo/bar',
{replace: /\//g}
), 'foo-bar')
})

it('options.lower', () => {
t.equal(slugify('Foo bAr baZ', {lower: true}), 'foo-bar-baz')
})
Expand Down