|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { describe, it } from 'node:test'; |
| 5 | + |
| 6 | +import createNodeSlugger, { slug } from '../slugger.mjs'; |
| 7 | + |
| 8 | +const identity = str => str; |
| 9 | + |
| 10 | +describe('slug', () => { |
| 11 | + describe('node.js replacement', () => { |
| 12 | + it('replaces "node.js" with "nodejs"', () => { |
| 13 | + assert.strictEqual(slug('node.js', identity), 'nodejs'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('is case-insensitive', () => { |
| 17 | + assert.strictEqual(slug('Node.JS', identity), 'nodejs'); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('ampersand replacement', () => { |
| 22 | + it('replaces & with -and-', () => { |
| 23 | + assert.strictEqual(slug('a&b', identity), 'a-and-b'); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('special character to hyphen replacement', () => { |
| 28 | + it('replaces underscores with hyphens', () => { |
| 29 | + assert.strictEqual(slug('foo_bar', identity), 'foo-bar'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('replaces forward slashes with hyphens', () => { |
| 33 | + assert.strictEqual(slug('foo/bar', identity), 'foo-bar'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('replaces colons with hyphens', () => { |
| 37 | + assert.strictEqual(slug('foo:bar', identity), 'foo-bar'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('replaces commas with hyphens', () => { |
| 41 | + assert.strictEqual(slug('foo,bar', identity), 'foo-bar'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('replaces semicolons with hyphens', () => { |
| 45 | + assert.strictEqual(slug('foo;bar', identity), 'foo-bar'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('leading hyphen removal', () => { |
| 50 | + it('removes a single leading hyphen', () => { |
| 51 | + assert.strictEqual(slug('-foo', identity), 'foo'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('removes multiple leading hyphens', () => { |
| 55 | + assert.strictEqual(slug('--foo', identity), 'foo'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('preserves an all-hyphen string', () => { |
| 59 | + assert.strictEqual(slug('---', identity), '---'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('trailing hyphen removal', () => { |
| 64 | + it('removes a single trailing hyphen', () => { |
| 65 | + assert.strictEqual(slug('foo-', identity), 'foo'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('removes multiple trailing hyphens', () => { |
| 69 | + assert.strictEqual(slug('foo--', identity), 'foo'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('consecutive hyphen replacement', () => { |
| 74 | + it('replaces from start of string up to and including double-hyphen with a single hyphen', () => { |
| 75 | + assert.strictEqual(slug('foo--bar', identity), '-bar'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not fire on an all-hyphen string', () => { |
| 79 | + assert.strictEqual(slug('---', identity), '---'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('integration with github-slugger', () => { |
| 84 | + it('lowercases and hyphenates a plain title', () => { |
| 85 | + assert.strictEqual(slug('Hello World'), 'hello-world'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('converts underscored names to hyphenated slugs', () => { |
| 89 | + assert.strictEqual(slug('child_process'), 'child-process'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('handles titles with no special characters', () => { |
| 93 | + assert.strictEqual(slug('stability index'), 'stability-index'); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('createNodeSlugger', () => { |
| 99 | + it('deduplicates the same slug by appending a counter', () => { |
| 100 | + const slugger = createNodeSlugger(); |
| 101 | + assert.strictEqual(slugger.slug('Hello'), 'hello'); |
| 102 | + assert.strictEqual(slugger.slug('Hello'), 'hello-1'); |
| 103 | + assert.strictEqual(slugger.slug('Hello'), 'hello-2'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('does not affect different titles', () => { |
| 107 | + const slugger = createNodeSlugger(); |
| 108 | + assert.strictEqual(slugger.slug('First'), 'first'); |
| 109 | + assert.strictEqual(slugger.slug('Second'), 'second'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('each instance has independent state', () => { |
| 113 | + const slugger1 = createNodeSlugger(); |
| 114 | + const slugger2 = createNodeSlugger(); |
| 115 | + slugger1.slug('Hello'); |
| 116 | + assert.strictEqual(slugger2.slug('Hello'), 'hello'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments