Skip to content

Add counter for multiple occurences #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,10 @@ declare function slugify(
): string;

export = slugify;

declare function counter(
string: string,
options?: slugify.Options
): string;

declare function reset(): void;
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const removeMootSeparators = (string, separator) => {
.replace(new RegExp(`^${escapedSeparator}|${escapedSeparator}$`, 'g'), '');
};

module.exports = (string, options) => {
const slugify = (string, options) => {
if (typeof string !== 'string') {
throw new TypeError(`Expected a string, got \`${typeof string}\``);
}
Expand Down Expand Up @@ -65,3 +65,30 @@ module.exports = (string, options) => {

return string;
};

const counter = () => {
const occurrences = new Map();

const countable = (string, options) => {
string = slugify(string, options);
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;
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,31 @@ test('leading underscore', t => {
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
});

test('counter', t => {
const countableSlugify = slugify.counter();
t.is(countableSlugify('foo bar'), 'foo-bar');
t.is(countableSlugify('foo bar'), 'foo-bar-2');
countableSlugify.reset();
t.is(countableSlugify('foo'), 'foo');
t.is(countableSlugify('foo'), 'foo-2');
t.is(countableSlugify('foo 1'), 'foo-1');
t.is(countableSlugify('foo-1'), 'foo-1-2');
t.is(countableSlugify('foo-1'), 'foo-1-3');
t.is(countableSlugify('foo'), 'foo-3');
t.is(countableSlugify('foo'), 'foo-4');
t.is(countableSlugify('foo-1'), 'foo-1-4');
t.is(countableSlugify('foo-2'), 'foo-2-1'); // Or foo-2-2 ??
t.is(countableSlugify('foo-2'), 'foo-2-2');
t.is(countableSlugify('foo-2-1'), 'foo-2-1-1');
t.is(countableSlugify('foo-2-1'), 'foo-2-1-2');
t.is(countableSlugify('foo-11'), 'foo-11-1');
t.is(countableSlugify('foo-111'), 'foo-111-1');
t.is(countableSlugify('foo-111-1'), 'foo-111-1-1');
t.is(countableSlugify('fooCamelCase', {lowercase: false, decamelize: false}), 'fooCamelCase');
t.is(countableSlugify('fooCamelCase', {decamelize: false}), 'foocamelcase-2');

const countableSlugify2 = slugify.counter();
t.is(countableSlugify2('foo'), 'foo');
t.is(countableSlugify2('foo'), 'foo-2');
});