talha-asad / mongoose-url-slugs

Create URL compatiable slugs on mongoose models, ensuring uniqueness.
MIT License
40 stars 22 forks source link

It does not work with Persian or Arabic characters #46

Open Mobasher-NetLinks opened 5 years ago

Mobasher-NetLinks commented 5 years ago

Suppose I have a field in collection that may contain Persian characters, now slug does not work with these type of characters, and only add -digits. What I have used in some where else in my migration script is: str = str.toLowerCase().replace(/([^a-z0-9\-\_\u0621-\u0628\u062A-\u063A\u0641-\u0642\u0644-\u0648\u064E-\u0651\u0655\u067E\u0686\u0698\u06A9\u06AF\u06BE\u06CC]+)/g, slugSeparator).replace(new RegExp(slugSeparator + '{2,}', 'g'), slugSeparator); to support Persian charcters

Mobasher-NetLinks commented 5 years ago

I've used from generator option, and solved. Thanks.

generator: function (slugText, separator) {
  slugText = slugText.toLowerCase().replace(/([^a-z0-9\-\_\u0600-\u06FF]+)/g, separator).replace(new RegExp(separator + '{2,}', 'g'), separator);
  if (slugText.substr(-1) === separator) {
    slugText = slugText.substr(0, slugText.length - 1);
  }
  return slugText;
  }