@sindresorhus/slugify
Advanced tools
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; |
36
index.js
@@ -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; |
{ | ||
"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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12831
238
1
234
0
+ Addedescape-string-regexp@4.0.0(transitive)
Updatedescape-string-regexp@^4.0.0