Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/slugify
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.9.1
Choose a base ref
...
head repository: sindresorhus/slugify
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.10.0
Choose a head ref
  • 6 commits
  • 8 files changed
  • 3 contributors

Commits on May 21, 2019

  1. Copy the full SHA
    4835efa View commit details

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019
    Copy the full SHA
    e95f1f7 View commit details

Commits on Jan 21, 2020

  1. Add Armenian characters (#29)

    Sil van Diepen authored and sindresorhus committed Jan 21, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5220d93 View commit details
  2. Separate capitalized words (#36)

    TiagoDanin authored and sindresorhus committed Jan 21, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9122e57 View commit details
  3. Require Node.js 10

    sindresorhus committed Jan 21, 2020
    Copy the full SHA
    a67fb07 View commit details
  4. 0.10.0

    sindresorhus committed Jan 21, 2020
    Copy the full SHA
    8a67581 View commit details
Showing with 112 additions and 64 deletions.
  1. +3 −0 .github/funding.yml
  2. +1 −1 .travis.yml
  3. +30 −37 index.d.ts
  4. +5 −2 index.js
  5. +12 −6 package.json
  6. +11 −17 readme.md
  7. +43 −1 replacements.js
  8. +7 −0 test.js
3 changes: 3 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
67 changes: 30 additions & 37 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -83,42 +83,35 @@ declare namespace slugify {
}
}

declare const slugify: {
/**
Slugify a string.
@param input - The 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('I ♥ 🦄 & 🐶', {
customReplacements: [
['🐶', 'dog']
]
});
//=> 'i-love-unicorn-and-dog'
```
*/
(input: string, options?: slugify.Options): string;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function slugify(
// input: string,
// options?: slugify.Options
// ): string;
// export = slugify;
default: typeof slugify;
};
/**
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('I ♥ 🦄 & 🐶', {
customReplacements: [
['🐶', 'dog']
]
});
//=> 'i-love-unicorn-and-dog'
```
*/
declare function slugify(
string: string,
options?: slugify.Options
): string;

export = slugify;
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,10 @@ const builtinOverridableReplacements = require('./overridable-replacements');

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

.replace(/([a-z\d])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1 $2');
};
@@ -38,6 +42,7 @@ const slugify = (string, options) => {
};

const separator = escapeStringRegexp(options.separator);

const customReplacements = new Map([
...builtinOverridableReplacements,
...options.customReplacements,
@@ -67,5 +72,3 @@ const slugify = (string, options) => {
};

module.exports = slugify;
// TODO: Remove this for the next major release
module.exports.default = slugify;
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "@sindresorhus/slugify",
"version": "0.9.1",
"version": "0.10.0",
"description": "Slugify a string",
"license": "MIT",
"repository": "sindresorhus/slugify",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -41,12 +42,17 @@
"id"
],
"dependencies": {
"escape-string-regexp": "^1.0.5",
"escape-string-regexp": "^2.0.0",
"lodash.deburr": "^4.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^2.4.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
},
"xo": {
"rules": {
"prefer-named-capture-group": "off"
}
}
}
28 changes: 11 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
@@ -4,16 +4,14 @@
Useful for URLs, filenames, and IDs.

It correctly handles [German umlauts](https://en.wikipedia.org/wiki/Germanic_umlaut), Vietnamese, Arabic, Russian, Romanian, Turkish and more.

It correctly handles [German umlauts](https://en.wikipedia.org/wiki/Germanic_umlaut), Vietnamese, Arabic, Russian, Romanian, Turkish, and more.

## Install

```
$ npm install @sindresorhus/slugify
```


## Usage

```js
@@ -38,20 +36,22 @@ slugify('I ♥ 🦄 & 🐶', {

## API

### slugify(input, [options])
### slugify(string, options?)

#### input
#### string

Type: `string`

String to slugify.

#### options

Type: `Object`
Type: `object`

##### separator

Type: `string`<br>
Default: `-`
Type: `string`\
Default: `'-'`

```js
const slugify = require('@sindresorhus/slugify');
@@ -65,7 +65,7 @@ slugify('BAR and baz', {separator: '_'});

##### lowercase

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Make the slug lowercase.
@@ -82,7 +82,7 @@ slugify('Déjà Vu!', {lowercase: false});

##### decamelize

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Convert camelcase to separate words. Internally it does `fooBar``foo bar`.
@@ -99,7 +99,7 @@ slugify('fooBar', {decamelize: false});

##### customReplacements

Type: `Array<string[]>`<br>
Type: `Array<string[]>`\
Default: `[
['&', ' and '],
['🦄', ' unicorn '],
@@ -132,13 +132,7 @@ slugify('foo@unicorn', {
//=> 'foo-at-unicorn'
```


## Related

- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
44 changes: 43 additions & 1 deletion replacements.js
Original file line number Diff line number Diff line change
@@ -324,5 +324,47 @@ module.exports = [
['ğ', 'g'],
['Ğ', 'g'],
['ı', 'i'],
['İ', 'i']
['İ', 'i'],

// Armenian
['ա', 'a'],
['բ', 'b'],
['գ', 'ɡ'],
['դ', 'd'],
['ե', 'ye'],
['զ', 'z'],
['է', 'e'],
['ը', 'u'],
['թ', 't'],
['ժ', 'zh'],
['ի', 'i'],
['լ', 'l'],
['խ', 'kh'],
['ծ', 'ts'],
['կ', 'k'],
['հ', 'h'],
['ձ', 'dz'],
['ղ', 'r'],
['ճ', 'j'],
['մ', 'm'],
['յ', 'j'],
['ն', 'n'],
['շ', 'sh'],
['ո', 'vo'],
['չ', 'ch'],
['պ', 'p'],
['ջ', 'j'],
['ռ', 'r'],
['ս', 's'],
['վ', 'v'],
['տ', 't'],
['ր', 're'],
['ց', 'ts'],
['ու', 'u'],
['ւ', 'v'],
['փ', 'p'],
['ք', 'q'],
['օ', 'o'],
['ֆ', 'f'],
['և', 'yev']
];
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ test('main', t => {
t.is(slugify('foo🦄'), 'foo-unicorn');
t.is(slugify('🦄🦄🦄'), 'unicorn-unicorn-unicorn');
t.is(slugify('foo&bar'), 'foo-and-bar');
t.is(slugify('foo360BAR'), 'foo360-bar');
t.is(slugify('FOO360'), 'foo-360');
t.is(slugify('FOObar'), 'foo-bar');
});

test('custom separator', t => {
@@ -117,3 +120,7 @@ test('supports Romanian', t => {
test('supports Turkish', t => {
t.is(slugify('İ ı Ş ş Ç ç Ğ ğ', {lowercase: false, separator: ' '}), 'i i s s c c g g');
});

test('supports Armenian', t => {
t.is(slugify('Ե ր ե ւ ա ն', {lowercase: false, separator: ' '}), 're ye v a n');
});