From 64d6bbc39b6f716ef831369db075c389743dd701 Mon Sep 17 00:00:00 2001 From: dgaussr Date: Mon, 27 Jan 2020 19:53:44 +0000 Subject: [PATCH 1/9] Add leading underscore option --- index.js | 7 +++++++ readme.md | 17 +++++++++++++++++ test.js | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/index.js b/index.js index 5f49fa8..8c2ed1d 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,7 @@ const slugify = (string, options) => { options = { separator: '-', + leadingUnderscore: false, lowercase: true, decamelize: true, customReplacements: [], @@ -64,6 +65,12 @@ const slugify = (string, options) => { patternSlug = /[^a-z\d]+/g; } + if (options.leadingUnderscore) { + let patternSlugString = patternSlug.source; + patternSlugString = `(?!^)_?${patternSlugString}`; + patternSlug = new RegExp(patternSlugString, 'g'); + } + string = string.replace(patternSlug, separator); string = string.replace(/\\/g, ''); string = removeMootSeparators(string, separator); diff --git a/readme.md b/readme.md index 59298ac..998c701 100644 --- a/readme.md +++ b/readme.md @@ -132,6 +132,23 @@ slugify('foo@unicorn', { //=> 'foo-at-unicorn' ``` +##### customReplacements + +Type: `boolean`\ +Default: `false` + +Allow leading underscore as an escape `_foo_bar` → `_foo-bar`. + +```js +const slugify = require('@sindresorhus/slugify'); + +slugify('_foo_bar'); +//=> 'foo-bar' + +slugify('_foo_bar', {leadingUnderscore: true}); +//=> '_foo-bar' +``` + ## Related - [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module diff --git a/test.js b/test.js index 8c10156..93d8190 100644 --- a/test.js +++ b/test.js @@ -124,3 +124,8 @@ 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', {leadingUnderscore: true}), '_foo-bar'); + t.is(slugify('_foo_bar', {leadingUnderscore: true}), '_foo-bar'); +}); From 0c20cd66e45a3f3997cbf9277e993a8c511cc257 Mon Sep 17 00:00:00 2001 From: Ricardo Fernandes Date: Sat, 1 Feb 2020 00:07:39 +0000 Subject: [PATCH 2/9] sindresorhus review changes --- index.d.ts | 18 ++++++++++++++++++ index.js | 4 ++-- index.test-d.ts | 1 + readme.md | 6 +++--- test.js | 5 +++-- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 52c72df..5d028a6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -80,6 +80,24 @@ declare namespace slugify { ``` */ readonly customReplacements?: ReadonlyArray<[string, string]>; + + /** + It preserves leading underscore. + + @default false + + @example + ``` + import slugify = require('@sindresorhus/slugify'); + + slugify('_foo_bar'); + //=> 'foo-bar' + + slugify('_foo_bar', {preserveLeadingUnderscore: true}); + //=> '_foo-bar' + ``` + */ + readonly preserveLeadingUnderscore?: boolean; } } diff --git a/index.js b/index.js index 8c2ed1d..b218f33 100644 --- a/index.js +++ b/index.js @@ -35,10 +35,10 @@ const slugify = (string, options) => { options = { separator: '-', - leadingUnderscore: false, lowercase: true, decamelize: true, customReplacements: [], + preserveLeadingUnderscore: false, ...options }; @@ -65,7 +65,7 @@ const slugify = (string, options) => { patternSlug = /[^a-z\d]+/g; } - if (options.leadingUnderscore) { + if (options.preserveLeadingUnderscore) { let patternSlugString = patternSlug.source; patternSlugString = `(?!^)_?${patternSlugString}`; patternSlug = new RegExp(patternSlugString, 'g'); diff --git a/index.test-d.ts b/index.test-d.ts index 9cf8690..e6b83bf 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -8,3 +8,4 @@ expectType(slugify('fooBar', {decamelize: false})); expectType( slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]}) ); +expectType(slugify('_foo_bar', {preserveLeadingUnderscore: true})); diff --git a/readme.md b/readme.md index 998c701..210feb0 100644 --- a/readme.md +++ b/readme.md @@ -132,12 +132,12 @@ slugify('foo@unicorn', { //=> 'foo-at-unicorn' ``` -##### customReplacements +##### preserveLeadingUnderscore Type: `boolean`\ Default: `false` -Allow leading underscore as an escape `_foo_bar` → `_foo-bar`. +It preserves leading underscore: `_foo_bar` → `_foo-bar`. ```js const slugify = require('@sindresorhus/slugify'); @@ -145,7 +145,7 @@ const slugify = require('@sindresorhus/slugify'); slugify('_foo_bar'); //=> 'foo-bar' -slugify('_foo_bar', {leadingUnderscore: true}); +slugify('_foo_bar', {preserveLeadingUnderscore: true}); //=> '_foo-bar' ``` diff --git a/test.js b/test.js index 93d8190..8ce5aaf 100644 --- a/test.js +++ b/test.js @@ -126,6 +126,7 @@ test('supports Armenian', t => { }); test('leading underscore', t => { - t.is(slugify('_foo bar', {leadingUnderscore: true}), '_foo-bar'); - t.is(slugify('_foo_bar', {leadingUnderscore: 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'); }); From 9348c2972c3d626179202a62b77c9bebb6fcc60b Mon Sep 17 00:00:00 2001 From: Ricardo Fernandes Date: Mon, 3 Feb 2020 12:00:23 +0000 Subject: [PATCH 3/9] fixes in the code and in the readme file --- index.js | 10 ++++------ readme.md | 2 +- test.js | 3 ++- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index b218f33..77e7f38 100644 --- a/index.js +++ b/index.js @@ -65,15 +65,13 @@ const slugify = (string, options) => { patternSlug = /[^a-z\d]+/g; } - if (options.preserveLeadingUnderscore) { - let patternSlugString = patternSlug.source; - patternSlugString = `(?!^)_?${patternSlugString}`; - patternSlug = new RegExp(patternSlugString, 'g'); - } - + const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_'); string = string.replace(patternSlug, separator); string = string.replace(/\\/g, ''); string = removeMootSeparators(string, separator); + if (shouldPrependUnderscore) { + string = '_' + string; + } return string; }; diff --git a/readme.md b/readme.md index 210feb0..0e40739 100644 --- a/readme.md +++ b/readme.md @@ -137,7 +137,7 @@ slugify('foo@unicorn', { Type: `boolean`\ Default: `false` -It preserves leading underscore: `_foo_bar` → `_foo-bar`. +If your string starts with an underscore, it will be preserved in the slugified string. ```js const slugify = require('@sindresorhus/slugify'); diff --git a/test.js b/test.js index 8ce5aaf..4bab671 100644 --- a/test.js +++ b/test.js @@ -128,5 +128,6 @@ test('supports Armenian', t => { 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'); + t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar'); }); From ee0cf2626ae0628ae8e1e72aa4ed9de26ff45e1c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 5 Feb 2020 12:35:54 +0700 Subject: [PATCH 4/9] Update index.js --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 77e7f38..2f1ec08 100644 --- a/index.js +++ b/index.js @@ -66,11 +66,13 @@ const slugify = (string, options) => { } const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_'); + string = string.replace(patternSlug, separator); string = string.replace(/\\/g, ''); string = removeMootSeparators(string, separator); + if (shouldPrependUnderscore) { - string = '_' + string; + string = `_${string}`; } return string; From ea9be90e622e5b716467fb589e81304bdab74d9e Mon Sep 17 00:00:00 2001 From: Ricardo Fernandes Date: Wed, 5 Feb 2020 19:43:30 +0000 Subject: [PATCH 5/9] fix in the order of the condition and in readme --- index.d.ts | 5 ++++- index.js | 3 ++- readme.md | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 5d028a6..3b410c3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -82,7 +82,7 @@ declare namespace slugify { readonly customReplacements?: ReadonlyArray<[string, string]>; /** - It preserves leading underscore. + If your string starts with an underscore, it will be preserved in the slugified string. @default false @@ -95,6 +95,9 @@ declare namespace slugify { slugify('_foo_bar', {preserveLeadingUnderscore: true}); //=> '_foo-bar' + + slugify('_hidden_filename', {preserveLeadingUnderscore: true}); + //=> '_hidden-filename' ``` */ readonly preserveLeadingUnderscore?: boolean; diff --git a/index.js b/index.js index 77e7f38..11a213b 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,8 @@ const slugify = (string, options) => { ...options }; + const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_'); + const separator = escapeStringRegexp(options.separator); const customReplacements = new Map([ @@ -65,7 +67,6 @@ const slugify = (string, options) => { patternSlug = /[^a-z\d]+/g; } - const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_'); string = string.replace(patternSlug, separator); string = string.replace(/\\/g, ''); string = removeMootSeparators(string, separator); diff --git a/readme.md b/readme.md index 0e40739..0228842 100644 --- a/readme.md +++ b/readme.md @@ -147,6 +147,9 @@ slugify('_foo_bar'); slugify('_foo_bar', {preserveLeadingUnderscore: true}); //=> '_foo-bar' + +slugify('_hidden_filename', {preserveLeadingUnderscore: true}); +//=> '_hidden-filename' ``` ## Related From e0717697ab4376b3581bf8e6087fec538a5f621c Mon Sep 17 00:00:00 2001 From: Ricardo Fernandes Date: Wed, 5 Feb 2020 20:01:09 +0000 Subject: [PATCH 6/9] fix in the order of the condition and in readme --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 11a213b..582d5cb 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,7 @@ const slugify = (string, options) => { string = string.replace(/\\/g, ''); string = removeMootSeparators(string, separator); if (shouldPrependUnderscore) { - string = '_' + string; + string = `_${string}`; } return string; From ed03eebf4788c40cc503e6f1b9dbe8bbcc52e85c Mon Sep 17 00:00:00 2001 From: Ricardo Fernandes Date: Sat, 8 Feb 2020 13:59:17 +0000 Subject: [PATCH 7/9] readme update. include use-case --- index.d.ts | 3 ++- readme.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3b410c3..832c850 100644 --- a/index.d.ts +++ b/index.d.ts @@ -82,7 +82,8 @@ declare namespace slugify { readonly customReplacements?: ReadonlyArray<[string, string]>; /** - If your string starts with an underscore, it will be preserved in the slugified 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 diff --git a/readme.md b/readme.md index 0228842..248c5be 100644 --- a/readme.md +++ b/readme.md @@ -137,7 +137,7 @@ slugify('foo@unicorn', { Type: `boolean`\ Default: `false` -If your string starts with an underscore, it will be preserved in the slugified 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. ```js const slugify = require('@sindresorhus/slugify'); From 85209f8af968383506b3b029a6f3af555c226ac3 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 12 Feb 2020 04:42:00 +0700 Subject: [PATCH 8/9] Update index.d.ts --- index.d.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 832c850..d87933a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -96,9 +96,6 @@ declare namespace slugify { slugify('_foo_bar', {preserveLeadingUnderscore: true}); //=> '_foo-bar' - - slugify('_hidden_filename', {preserveLeadingUnderscore: true}); - //=> '_hidden-filename' ``` */ readonly preserveLeadingUnderscore?: boolean; From 781c37e0bc5e22ad6d9fda9708a53996ff798676 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 12 Feb 2020 04:43:20 +0700 Subject: [PATCH 9/9] Update readme.md --- readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 248c5be..4ee157d 100644 --- a/readme.md +++ b/readme.md @@ -137,7 +137,9 @@ slugify('foo@unicorn', { 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. +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'); @@ -147,9 +149,6 @@ slugify('_foo_bar'); slugify('_foo_bar', {preserveLeadingUnderscore: true}); //=> '_foo-bar' - -slugify('_hidden_filename', {preserveLeadingUnderscore: true}); -//=> '_hidden-filename' ``` ## Related