@sindresorhus/slugify
Advanced tools
Comparing version 0.2.0 to 0.3.0
15
index.js
@@ -11,3 +11,3 @@ 'use strict'; | ||
const customReplacements = new Map([ | ||
const builtinReplacements = new Map([ | ||
['&', 'and'], | ||
@@ -18,4 +18,4 @@ ['🦄', 'unicorn'], | ||
const doCustomReplacements = string => { | ||
for (const [key, value] of customReplacements) { | ||
const doCustomReplacements = (string, replacements) => { | ||
for (const [key, value] of replacements) { | ||
string = string.replace(new RegExp(escapeStringRegexp(key), 'g'), ` ${value} `); | ||
@@ -39,10 +39,15 @@ } | ||
options = Object.assign({ | ||
separator: '-' | ||
separator: '-', | ||
customReplacements: [] | ||
}, options); | ||
const separator = escapeStringRegexp(options.separator); | ||
const customReplacements = new Map([ | ||
...builtinReplacements, | ||
...options.customReplacements | ||
]); | ||
string = deburr(string); | ||
string = decamelize(string); | ||
string = doCustomReplacements(string); | ||
string = doCustomReplacements(string, customReplacements); | ||
string = string.toLowerCase(); | ||
@@ -49,0 +54,0 @@ string = string.replace(/[^a-z\d]+/g, separator); |
{ | ||
"name": "@sindresorhus/slugify", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Slugify a string", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -31,2 +31,9 @@ # slugify [![Build Status](https://travis-ci.org/sindresorhus/slugify.svg?branch=master)](https://travis-ci.org/sindresorhus/slugify) | ||
//=> 'bar_and_baz' | ||
slugify('I ♥ 🦄 & 🐶', { | ||
customReplacements: [ | ||
['🐶', 'dog'] | ||
] | ||
}); | ||
//=> 'i-love-unicorn-and-dog' | ||
``` | ||
@@ -51,4 +58,17 @@ | ||
##### customReplacements | ||
Type: `Array`<br> | ||
Default: `[ | ||
['&', 'and'], | ||
['🦄', 'unicorn'], | ||
['♥', 'love'] | ||
]` | ||
Specifying this only replaces the default if you set an item with the same key, like `&`. | ||
## Related | ||
- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module | ||
- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename | ||
@@ -55,0 +75,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
4554
46
78