diff --git a/README.md b/README.md index 385d7b0..27f3e13 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/slugify.d.ts b/slugify.d.ts index 78a9d7f..6b1f49b 100644 --- a/slugify.d.ts +++ b/slugify.d.ts @@ -14,6 +14,7 @@ declare function slugify( | { replacement?: string; remove?: RegExp; + replace?: RegExp; lower?: boolean; strict?: boolean; locale?: string; diff --git a/slugify.js b/slugify.js index dd13ebf..9161a60 100644 --- a/slugify.js +++ b/slugify.js @@ -30,6 +30,12 @@ var trim = options.trim === undefined ? true : options.trim + // Replace matches with whitespace so they are normalized with other + // separators below (including trimming and duplicate removal). + if (options.replace) { + string = string.replace(options.replace, ' ') + } + var slug = string.normalize().split('') // replace characters based on charMap .reduce(function (result, ch) { diff --git a/test/slugify.js b/test/slugify.js index 356c4b7..45c2550 100644 --- a/test/slugify.js +++ b/test/slugify.js @@ -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') })