use options param, update docs, update tests

This commit is contained in:
flouc001 2020-07-12 19:38:37 +01:00
parent 2290f677c9
commit 710658cb61
3 changed files with 18 additions and 18 deletions

View File

@ -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
...
```

View File

@ -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);
}
};

View File

@ -64,11 +64,17 @@ describe('Test slugger functionality', () => {
expect(slugger.slug('<em>html</em>')).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('<h1>This Section</h1>');
slugger.slug('<h1>This Section</h1>', { dryrun: true });
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
});
it('should still return the next unique id when using dryrun', () => {
const slugger = new marked.Slugger();
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section-1');
});
});
describe('Test paragraph token type', () => {