Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sindresorhus/slugify

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sindresorhus/slugify - npm Package Compare versions

Comparing version 2.1.1 to 2.2.0

19

index.d.ts

@@ -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);

2

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc