Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Commit

Permalink
made possible to remove special chars from string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Suzan-Dev committed Apr 1, 2021
1 parent 73dcf47 commit 9900638
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

`Slugifi` is small package for creating a slug from a string.

 

## Release Notes (v1.0.7)

> You can now remove special characters from string by enabling specialChars property from the options.
 

## Installation

Npm :
Expand All @@ -26,6 +34,8 @@ Pnpm :
$ pnpm add slugifi
```

 

## Usage

ES6 :
Expand All @@ -44,15 +54,20 @@ const slugifi = require('slugifi');
slugifi('Random text'); // Outputs: random-text
```

 

## Options

```js
slugifi('some text', {
separator: '_', // defaults to '-'
capitalize: true, // defaults to false
specialChars: false, // defaults to true
});
```

 

## License

[MIT](http://opensource.org/licenses/MIT)
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const slugifi = (text, options) => {
const slug = text;
let slug = text;
let separator = '-';

if (!text) throw new Error('Please provide a string!');
if (options?.separator) separator = options.separator;

if (options?.capitalize) {
const capitalizedSlug = slug
let capitalizedSlug = slug
.split(' ')
.map((el) => {
let capitalizeEl = el.toLowerCase();
Expand All @@ -15,10 +15,18 @@ const slugifi = (text, options) => {
})
.join(separator);

if (options?.specialChars === false) {
capitalizedSlug = capitalizedSlug.replace(new RegExp('[^a-zA-Z0-9.' + separator + ']', 'g'), '');
}

return capitalizedSlug;
}

return slug.replace(/ /g, separator).toLowerCase();
slug = slug.replace(/ /g, separator).toLowerCase();
if (options?.specialChars === false) {
return slug.replace(new RegExp('[^a-zA-Z0-9.' + separator + ']', 'g'), '');
}
return slug;
};

module.exports = slugifi;

0 comments on commit 9900638

Please sign in to comment.