forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslugger.test.mjs
More file actions
136 lines (108 loc) · 4.27 KB
/
slugger.test.mjs
File metadata and controls
136 lines (108 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import createNodeSlugger, { slug } from '../slugger.mjs';
const identity = str => str;
describe('slug', () => {
describe('node.js replacement', () => {
it('replaces "node.js" with "nodejs"', () => {
assert.strictEqual(slug('node.js', identity), 'nodejs');
});
it('is case-insensitive', () => {
assert.strictEqual(slug('Node.JS', identity), 'nodejs');
});
});
describe('ampersand replacement', () => {
it('replaces & with -and-', () => {
assert.strictEqual(slug('a&b', identity), 'a-and-b');
});
});
describe('special character to hyphen replacement', () => {
it('replaces underscores with hyphens (except leading)', () => {
assert.strictEqual(slug('foo_bar', identity), 'foo-bar');
});
it('replaces forward slashes with hyphens', () => {
assert.strictEqual(slug('foo/bar', identity), 'foo-bar');
});
it('replaces colons with hyphens', () => {
assert.strictEqual(slug('foo:bar', identity), 'foo-bar');
});
it('replaces commas with hyphens', () => {
assert.strictEqual(slug('foo,bar', identity), 'foo-bar');
});
it('replaces semicolons with hyphens', () => {
assert.strictEqual(slug('foo;bar', identity), 'foo-bar');
});
});
describe('underscore preservation (anchor link compatibility)', () => {
it('preserves leading underscores so __dirname slug matches #__dirname anchor', () => {
assert.strictEqual(slug('__dirname', identity), '__dirname');
});
it('preserves leading underscores so __filename slug matches #__filename anchor', () => {
assert.strictEqual(slug('__filename', identity), '__filename');
});
});
describe('leading hyphen removal', () => {
it('removes a single leading hyphen', () => {
assert.strictEqual(slug('-foo', identity), 'foo');
});
it('removes multiple leading hyphens', () => {
assert.strictEqual(slug('--foo', identity), 'foo');
});
it('preserves an all-hyphen string', () => {
assert.strictEqual(slug('---', identity), '---');
});
});
describe('trailing hyphen removal', () => {
it('removes a single trailing hyphen', () => {
assert.strictEqual(slug('foo-', identity), 'foo');
});
it('removes multiple trailing hyphens', () => {
assert.strictEqual(slug('foo--', identity), 'foo');
});
});
describe('consecutive hyphen replacement', () => {
it('replaces from start of string up to and including double-hyphen with a single hyphen', () => {
assert.strictEqual(slug('foo--bar', identity), '-bar');
});
it('does not fire on an all-hyphen string', () => {
assert.strictEqual(slug('---', identity), '---');
});
});
describe('integration with github-slugger', () => {
it('lowercases and hyphenates a plain title', () => {
assert.strictEqual(slug('Hello World'), 'hello-world');
});
it('converts internal underscored names to hyphenated slugs', () => {
assert.strictEqual(slug('child_process'), 'child-process');
});
it('handles titles with no special characters', () => {
assert.strictEqual(slug('stability index'), 'stability-index');
});
it('generates correct slug for __dirname (preserves leading underscores)', () => {
assert.strictEqual(slug('__dirname'), '__dirname');
});
it('generates correct slug for __filename (preserves leading underscores)', () => {
assert.strictEqual(slug('__filename'), '__filename');
});
});
});
describe('createNodeSlugger', () => {
it('deduplicates the same slug by appending a counter', () => {
const slugger = createNodeSlugger();
assert.strictEqual(slugger.slug('Hello'), 'hello');
assert.strictEqual(slugger.slug('Hello'), 'hello-1');
assert.strictEqual(slugger.slug('Hello'), 'hello-2');
});
it('does not affect different titles', () => {
const slugger = createNodeSlugger();
assert.strictEqual(slugger.slug('First'), 'first');
assert.strictEqual(slugger.slug('Second'), 'second');
});
it('each instance has independent state', () => {
const slugger1 = createNodeSlugger();
const slugger2 = createNodeSlugger();
slugger1.slug('Hello');
assert.strictEqual(slugger2.slug('Hello'), 'hello');
});
});