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 0.11.0 to 1.0.0

109

index.d.ts

@@ -116,29 +116,98 @@ declare namespace slugify {

/**
Slugify a string.
declare const slugify: {
/**
Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurences of the same string.
@param string - String to slugify.
@param string - String to slugify.
@example
```
import slugify = require('@sindresorhus/slugify');
@example
```
import slugify = require('@sindresorhus/slugify');
slugify('I ♥ Dogs');
//=> 'i-love-dogs'
const countableSlugify = slugify.counter();
countableSlugify('foo bar');
//=> 'foo-bar'
slugify(' Déjà Vu! ');
//=> 'deja-vu'
countableSlugify('foo bar');
//=> 'foo-bar-2'
slugify('fooBar 123 $#%');
//=> 'foo-bar-123'
countableSlugify.reset();
slugify('я люблю единорогов');
//=> 'ya-lyublyu-edinorogov'
```
*/
declare function slugify(
string: string,
options?: slugify.Options
): string;
countableSlugify('foo bar');
//=> 'foo-bar'
```
__Use case example of counter__
If, for example, you have a document with multiple sections where each subsection has an example.
```
## Section 1
### Example
## Section 2
### Example
```
You can then use `slugify.counter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
*/
counter: () => {
/**
Reset the counter.
@example
```
import slugify = require('@sindresorhus/slugify');
const countableSlugify = slugify.counter();
countableSlugify('foo bar');
//=> 'foo-bar'
countableSlugify('foo bar');
//=> 'foo-bar-2'
countableSlugify.reset();
countableSlugify('foo bar');
//=> 'foo-bar'
```
*/
reset: () => void;
(
string: string,
options?: slugify.Options
): string;
};
/**
Slugify a string.
@param string - String to slugify.
@example
```
import slugify = require('@sindresorhus/slugify');
slugify('I ♥ Dogs');
//=> 'i-love-dogs'
slugify(' Déjà Vu! ');
//=> 'deja-vu'
slugify('fooBar 123 $#%');
//=> 'foo-bar-123'
slugify('я люблю единорогов');
//=> 'ya-lyublyu-edinorogov'
```
*/
(
string: string,
options?: slugify.Options
): string;
};
export = slugify;

@@ -9,3 +9,3 @@ 'use strict';

// Separate capitalized words.
.replace(/([A-Z]{2,})([a-z\d]+)/g, '$1 $2')
.replace(/([A-Z]{2,})(\d+)/g, '$1 $2')
.replace(/([a-z\d]+)([A-Z]{2,})/g, '$1 $2')

@@ -25,3 +25,3 @@

module.exports = (string, options) => {
const slugify = (string, options) => {
if (typeof string !== 'string') {

@@ -70,1 +70,33 @@ throw new TypeError(`Expected a string, got \`${typeof string}\``);

};
const counter = () => {
const occurrences = new Map();
const countable = (string, options) => {
string = slugify(string, options);
if (!string) {
return '';
}
const stringLower = string.toLowerCase();
const numberless = occurrences.get(stringLower.replace(/(?:-\d+?)+?$/, '')) || 0;
const counter = occurrences.get(stringLower);
occurrences.set(stringLower, typeof counter === 'number' ? counter + 1 : 1);
const newCounter = occurrences.get(stringLower) || 2;
if (newCounter >= 2 || numberless > 2) {
string = `${string}-${newCounter}`;
}
return string;
};
countable.reset = () => {
occurrences.clear();
};
return countable;
};
module.exports = slugify;
module.exports.counter = counter;

6

package.json
{
"name": "@sindresorhus/slugify",
"version": "0.11.0",
"version": "1.0.0",
"description": "Slugify a string",

@@ -45,3 +45,3 @@ "license": "MIT",

"@sindresorhus/transliterate": "^0.1.0",
"escape-string-regexp": "^2.0.0"
"escape-string-regexp": "^4.0.0"
},

@@ -51,4 +51,4 @@ "devDependencies": {

"tsd": "^0.11.0",
"xo": "^0.26.1"
"xo": "^0.30.0"
}
}

@@ -167,2 +167,64 @@ # slugify [![Build Status](https://travis-ci.org/sindresorhus/slugify.svg?branch=master)](https://travis-ci.org/sindresorhus/slugify)

### slugify.counter()
Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurences of the same string.
#### Example
```js
const slugify = require('@sindresorhus/slugify');
const countableSlugify = slugify.counter();
countableSlugify('foo bar');
//=> 'foo-bar'
countableSlugify('foo bar');
//=> 'foo-bar-2'
countableSlugify.reset();
countableSlugify('foo bar');
//=> 'foo-bar'
```
#### Use-case example of counter
If, for example, you have a document with multiple sections where each subsection has an example.
```md
## Section 1
### Example
## Section 2
### Example
```
You can then use `slugify.counter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
### slugify.reset()
Reset the counter
#### Example
```js
const slugify = require('@sindresorhus/slugify');
const countableSlugify = slugify.counter();
countableSlugify('foo bar');
//=> 'foo-bar'
countableSlugify('foo bar');
//=> 'foo-bar-2'
countableSlugify.reset();
countableSlugify('foo bar');
//=> 'foo-bar'
```
## Related

@@ -169,0 +231,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