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.10.0
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.1
Choose a head ref
  • 2 commits
  • 6 files changed
  • 2 contributors

Commits on Feb 11, 2020

  1. Add leading underscore option (#43)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    dgaussr and sindresorhus authored Feb 11, 2020
    Copy the full SHA
    9a78d8f View commit details
  2. 0.10.1

    sindresorhus committed Feb 11, 2020
    Copy the full SHA
    948a828 View commit details
Showing with 54 additions and 1 deletion.
  1. +19 −0 index.d.ts
  2. +7 −0 index.js
  3. +1 −0 index.test-d.ts
  4. +1 −1 package.json
  5. +19 −0 readme.md
  6. +7 −0 test.js
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -80,6 +80,25 @@ declare namespace slugify {
```
*/
readonly customReplacements?: ReadonlyArray<[string, string]>;

/**
If your string starts with an underscore, it will be preserved in the slugified string.
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
@default false
@example
```
import slugify = require('@sindresorhus/slugify');
slugify('_foo_bar');
//=> 'foo-bar'
slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
```
*/
readonly preserveLeadingUnderscore?: boolean;
}
}

7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -38,9 +38,12 @@ const slugify = (string, options) => {
lowercase: true,
decamelize: true,
customReplacements: [],
preserveLeadingUnderscore: false,
...options
};

const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_');

const separator = escapeStringRegexp(options.separator);

const customReplacements = new Map([
@@ -68,6 +71,10 @@ const slugify = (string, options) => {
string = string.replace(/\\/g, '');
string = removeMootSeparators(string, separator);

if (shouldPrependUnderscore) {
string = `_${string}`;
}

return string;
};

1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -8,3 +8,4 @@ expectType<string>(slugify('fooBar', {decamelize: false}));
expectType<string>(
slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]})
);
expectType<string>(slugify('_foo_bar', {preserveLeadingUnderscore: true}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sindresorhus/slugify",
"version": "0.10.0",
"version": "0.10.1",
"description": "Slugify a string",
"license": "MIT",
"repository": "sindresorhus/slugify",
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -132,6 +132,25 @@ slugify('foo@unicorn', {
//=> 'foo-at-unicorn'
```

##### preserveLeadingUnderscore

Type: `boolean`\
Default: `false`

If your string starts with an underscore, it will be preserved in the slugified string.

Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.

```js
const slugify = require('@sindresorhus/slugify');

slugify('_foo_bar');
//=> 'foo-bar'

slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
```

## Related

- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -124,3 +124,10 @@ test('supports Turkish', t => {
test('supports Armenian', t => {
t.is(slugify('Ե ր ե ւ ա ն', {lowercase: false, separator: ' '}), 're ye v a n');
});

test('leading underscore', t => {
t.is(slugify('_foo bar', {preserveLeadingUnderscore: true}), '_foo-bar');
t.is(slugify('_foo_bar', {preserveLeadingUnderscore: true}), '_foo-bar');
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
});