simov/slugify

replacement and remove (question & suggestion)

Closed this issue · 3 comments

Because the way slugify checkes for undefined, when we replace char with empty string, things might not work as expected.

for example, I want to replace ☢ with radioactive, and colon with empty space, I'll have to do it in extend and also remove. and before I dive in deep into src code, I was using
slugify.extend({ '&': '', '.': '-', '%': '', ':': '' })
and expect that it will replace the char that I specified will be gone, however, & was not replaced but removed by
.replace(options.remove || /[^\w\s$*_+~.()'"!\-:@]+/g, '')
and because of this, the colon didn't work out for me (colon should be replaced by empty string).

Am I missing something? if not, could we change from:
return result + (locale[ch] || charMap[ch] || ch)
to
return result + (locale[ch] !== undefined ? locale[ch] : charMap[ch] !== undefined ? charMap[ch] : ch)

Trott commented

Replicated with this:

'use strict';

const slugify = require('slugify');

console.log(slugify('a&b:c')) // aandb:c

slugify.extend({ '&': '', '.': '-', '%': '', ':': '' });

console.log(slugify('a&b:c')) // Expected abc but got ab:c
Trott commented

Not necessarily a great solution and probably something you already realize, but just in case: You can work around this in your own code by explicitly sending a remove option that omits the ::

slugify(myString, { remove: /[^\w\s$*_+~.()'"!\-@]+/g });

hello.How can I change the url entirely??For example: I got this: www.random.com/product?id=0 , and I want to change it fot the following: www.random.com/product-name.Is it posible??