From 710658cb61b92061eb16d05e9e6c313f46de541a Mon Sep 17 00:00:00 2001 From: flouc001 Date: Sun, 12 Jul 2020 19:38:37 +0100 Subject: [PATCH] use options param, update docs, update tests --- docs/USING_PRO.md | 12 ++++++------ src/Slugger.js | 14 ++++---------- test/unit/marked-spec.js | 10 ++++++++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 418fd1e3..fb70f46d 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -82,13 +82,13 @@ slugger.slug('foo-1') // foo-1-2 ... ``` -`slugger` also has a stateless method `slugText` which does not update the internal accumulator: +`slugger.slug` can also be called with the `dryrun` option for stateless operation: ```js -slugger.slug('foo') // foo -slugger.slugText('foo') // foo-1 -slugger.slug('foo') // foo-1 -slugger.slugText('foo') // foo-2 -slugger.slug('foo') // foo-2 +slugger.slug('foo') // foo +slugger.slug('foo', { dryrun: true }) // foo-1 +slugger.slug('foo') // foo-1 +slugger.slug('foo', { dryrun: true }) // foo-2 +slugger.slug('foo') // foo-2 ... ``` diff --git a/src/Slugger.js b/src/Slugger.js index 75e16c85..db385f54 100644 --- a/src/Slugger.js +++ b/src/Slugger.js @@ -39,17 +39,11 @@ module.exports = class Slugger { /** * Convert string to unique id + * @param {object} options + * @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator. */ - slug(value) { + slug(value, options = {}) { const slug = this.serialize(value); - return this.getNextSafeSlug(slug); - } - - /** - * Get slug text without incrementing accumulator - */ - slugText(value) { - const slug = this.serialize(value); - return this.getNextSafeSlug(slug, true); + return this.getNextSafeSlug(slug, options.dryrun); } }; diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index c80868a8..93ce922e 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -64,11 +64,17 @@ describe('Test slugger functionality', () => { expect(slugger.slug('html')).toBe('html'); }); - it('should not increment seen when just getting text', () => { + it('should not increment seen when using dryrun option', () => { const slugger = new marked.Slugger(); - slugger.slugText('

This Section

'); + slugger.slug('

This Section

', { dryrun: true }); expect(slugger.slug('

This Section

')).toBe('this-section'); }); + + it('should still return the next unique id when using dryrun', () => { + const slugger = new marked.Slugger(); + expect(slugger.slug('

This Section

')).toBe('this-section'); + expect(slugger.slug('

This Section

', { dryrun: true })).toBe('this-section-1'); + }); }); describe('Test paragraph token type', () => {