@sindresorhus/slugify
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -135,2 +135,21 @@ export interface Options { | ||
readonly preserveTrailingDash?: boolean; | ||
/** | ||
Preserve certain characters. | ||
It cannot contain the `separator`. | ||
For example, if you want to slugify URLs, but preserve the HTML fragment `#` character, you could set `preserveCharacters: ['#']`. | ||
@default [] | ||
@example | ||
``` | ||
import slugify from '@sindresorhus/slugify'; | ||
slugify('foo_bar#baz', {preserveCharacters: ['#']}); | ||
//=> 'foo-bar#baz' | ||
``` | ||
*/ | ||
readonly preserveCharacters?: string[]; | ||
} | ||
@@ -137,0 +156,0 @@ |
26
index.js
@@ -25,2 +25,19 @@ import escapeStringRegexp from 'escape-string-regexp'; | ||
const buildPatternSlug = options => { | ||
let negationSetPattern = 'a-z\\d'; | ||
negationSetPattern += options.lowercase ? '' : 'A-Z'; | ||
if (options.preserveCharacters.length > 0) { | ||
for (const character of options.preserveCharacters) { | ||
if (character === options.separator) { | ||
throw new Error(`The separator character \`${options.separator}\` cannot be included in preserved characters: ${options.preserveCharacters}`); | ||
} | ||
negationSetPattern += escapeStringRegexp(character); | ||
} | ||
} | ||
return new RegExp(`[^${negationSetPattern}]+`, 'g'); | ||
}; | ||
export default function slugify(string, options) { | ||
@@ -38,2 +55,3 @@ if (typeof string !== 'string') { | ||
preserveTrailingDash: false, | ||
preserveCharacters: [], | ||
...options | ||
@@ -56,7 +74,6 @@ }; | ||
let patternSlug = /[^a-zA-Z\d]+/g; | ||
const patternSlug = buildPatternSlug(options); | ||
if (options.lowercase) { | ||
string = string.toLowerCase(); | ||
patternSlug = /[^a-z\d]+/g; | ||
} | ||
@@ -66,2 +83,7 @@ | ||
string = string.replace(/\\/g, ''); | ||
// Detect contractions/possessives by looking for any word followed by a `-t` | ||
// or `-s` in isolation and then remove it. | ||
string = string.replace(/([a-zA-Z\d]+)-([ts])(-|$)/g, '$1$2$3'); | ||
if (options.separator) { | ||
@@ -68,0 +90,0 @@ string = removeMootSeparators(string, options.separator); |
{ | ||
"name": "@sindresorhus/slugify", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Slugify a string", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -189,2 +189,20 @@ # slugify | ||
##### preserveCharacters | ||
Type: `string[]`\ | ||
Default: `[]` | ||
Preserve certain characters. | ||
It cannot contain the `separator`. | ||
For example, if you want to slugify URLs, but preserve the HTML fragment `#` character. | ||
```js | ||
import slugify from '@sindresorhus/slugify'; | ||
slugify('foo_bar#baz', {preserveCharacters: ['#']}); | ||
//=> 'foo-bar#baz' | ||
``` | ||
### slugifyWithCounter() | ||
@@ -191,0 +209,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15303
279
274