Skip to content

Commit a9f83e4

Browse files
committed
chore(core): added utils specs
1 parent 91978aa commit a9f83e4

18 files changed

+234
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { camelToTitleCase } from './camel-to-title-case';
2+
3+
describe('camelToTitleCase', () => {
4+
it('should convert camel case to title case correctly', () => {
5+
const input = 'camelCaseText';
6+
const expectedOutput = 'Camel Case Text';
7+
const result = camelToTitleCase(input);
8+
expect(result).toEqual(expectedOutput);
9+
});
10+
11+
it('should handle single word correctly', () => {
12+
const input = 'word';
13+
const expectedOutput = 'Word';
14+
const result = camelToTitleCase(input);
15+
expect(result).toEqual(expectedOutput);
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { escapeChars } from './escape-chars';
2+
3+
describe('escapeChars', () => {
4+
it('should escape special characters correctly', () => {
5+
const input = 'This is a string with >, <, {, }, _, `, |, [, ], and *';
6+
const expectedOutput =
7+
'This is a string with \\>, \\<, \\{, \\}, \\_, \\`, \\|, \\[, \\], and \\*';
8+
const result = escapeChars(input);
9+
expect(result).toEqual(expectedOutput);
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { formatMarkdown } from './format-markdown';
2+
3+
describe('formatMarkdown', () => {
4+
it('should trim content and restrict new lines', () => {
5+
const input = `
6+
7+
# headline
8+
9+
10+
Paragraph
11+
12+
13+
14+
## headline
15+
16+
`;
17+
const expectedOutput = `# headline
18+
19+
Paragraph
20+
21+
## headline
22+
`;
23+
const result = formatMarkdown(input);
24+
expect(result).toEqual(expectedOutput);
25+
});
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { formatTableColumn } from './format-table-column';
2+
3+
describe('formatTableColumn', () => {
4+
it('should format table column correctly', () => {
5+
const input = `This is a string with
6+
a newline, | a pipe, and a code block:
7+
\`\`\`ts
8+
const x = 10;
9+
\`\`\``;
10+
const expectedOutput =
11+
'This is a string with<br />a newline, \\| a pipe, and a code block:<br />`const x = 10;`';
12+
const result = formatTableColumn(input);
13+
expect(result).toEqual(expectedOutput);
14+
});
15+
16+
it('should remove trailing <br /> tags', () => {
17+
const input = 'This is a string with a trailing <br /> tag<br /> ';
18+
const expectedOutput = 'This is a string with a trailing <br /> tag';
19+
const result = formatTableColumn(input);
20+
expect(result).toEqual(expectedOutput);
21+
});
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import path from 'path';
2+
import { getFileNameWithExtension } from './get-file-name-with-extension';
3+
4+
describe('getFileNameWithExtension', () => {
5+
it('should return filename with extension', () => {
6+
const fileName = 'testFile';
7+
const fileExtension = '.txt';
8+
const expectedOutput = 'testFile.txt';
9+
10+
const result = getFileNameWithExtension(fileName, fileExtension);
11+
12+
expect(result).toEqual(expectedOutput);
13+
});
14+
15+
it('should handle fileExtension without dot', () => {
16+
const fileName = 'testFile';
17+
const fileExtension = 'txt';
18+
const expectedOutput = 'testFile.txt';
19+
20+
const result = getFileNameWithExtension(fileName, fileExtension);
21+
22+
expect(result).toEqual(expectedOutput);
23+
});
24+
25+
it('should handle fileName with directories', () => {
26+
const fileName = path.join('dir1', 'dir2', 'testFile');
27+
const fileExtension = 'txt';
28+
const expectedOutput = path.join('dir1', 'dir2', 'testFile.txt');
29+
30+
const result = getFileNameWithExtension(fileName, fileExtension);
31+
32+
expect(result).toEqual(expectedOutput);
33+
});
34+
});

packages/typedoc-plugin-markdown/src/libs/utils/get-first-paragraph.ts

-3
This file was deleted.

packages/typedoc-plugin-markdown/src/libs/utils/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ export { escapeChars } from './escape-chars';
77
export { formatMarkdown } from './format-markdown';
88
export { formatTableColumn } from './format-table-column';
99
export { getFileNameWithExtension } from './get-file-name-with-extension';
10-
export { getFirstParagrph } from './get-first-paragraph';
1110
export { isQuoted } from './is-quoted';
1211
export { removeFirstScopedDirectory } from './remove-first-scoped-directory';
1312
export { removeLineBreaks } from './remove-line-breaks';
1413
export { sanitizeComments } from './sanitize-comments';
15-
export { slugifyUrl } from './slugify-url';
16-
export { stripComments } from './strip-comments';
14+
export { slugify } from './slugify';
1715
export { unEscapeChars } from './un-escape-chars';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { isQuoted } from './is-quoted';
2+
3+
describe('isQuoted', () => {
4+
it('should return true for quoted strings', () => {
5+
const input = '"This is a quoted string"';
6+
const expectedOutput = true;
7+
8+
const result = isQuoted(input);
9+
10+
expect(result).toEqual(expectedOutput);
11+
});
12+
13+
it('should return false for unquoted strings', () => {
14+
const input = 'This is an unquoted string';
15+
const expectedOutput = false;
16+
17+
const result = isQuoted(input);
18+
19+
expect(result).toEqual(expectedOutput);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from 'path';
2+
import { removeFirstScopedDirectory } from './remove-first-scoped-directory';
3+
4+
describe('removeFirstScopedDirectory', () => {
5+
it('should remove first scoped directory', () => {
6+
const input = '@scoped/dir1/dir2/file.txt';
7+
const expectedOutput = `dir1${path.sep}dir2${path.sep}file.txt`;
8+
9+
const result = removeFirstScopedDirectory(input);
10+
11+
expect(result).toEqual(expectedOutput);
12+
});
13+
14+
it('should handle string without scoped directory', () => {
15+
const input = 'dir1/dir2/file.txt';
16+
const expectedOutput = `dir1${path.sep}dir2${path.sep}file.txt`;
17+
18+
const result = removeFirstScopedDirectory(input);
19+
20+
expect(result).toEqual(expectedOutput);
21+
});
22+
23+
it('should handle string with multiple scoped directories', () => {
24+
const input = '@scoped/dir1/@scoped/dir2/file.txt';
25+
const expectedOutput = `dir1${path.sep}@scoped${path.sep}dir2${path.sep}file.txt`;
26+
27+
const result = removeFirstScopedDirectory(input);
28+
29+
expect(result).toEqual(expectedOutput);
30+
});
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { removeLineBreaks } from './remove-line-breaks';
2+
3+
describe('removeLineBreaks', () => {
4+
it('should remove line breaks and replace with a single space', () => {
5+
const input = 'This is a string\nwith multiple\n\nline breaks';
6+
const expectedOutput = 'This is a string with multiple line breaks';
7+
8+
const result = removeLineBreaks(input);
9+
10+
expect(result).toEqual(expectedOutput);
11+
});
12+
13+
it('should replace multiple spaces with a single space', () => {
14+
const input = 'This is a string with multiple spaces';
15+
const expectedOutput = 'This is a string with multiple spaces';
16+
17+
const result = removeLineBreaks(input);
18+
19+
expect(result).toEqual(expectedOutput);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { slugify } from './slugify';
2+
3+
describe('slugifyUrl', () => {
4+
it('should convert to slug correctly', () => {
5+
const input = ' Type Alias ';
6+
const expectedOutput = 'type-alias';
7+
8+
const result = slugify(input);
9+
10+
expect(result).toEqual(expectedOutput);
11+
});
12+
13+
it('should handle URL with special characters', () => {
14+
const input = 'Reflection!';
15+
const expectedOutput = 'reflection';
16+
17+
const result = slugify(input);
18+
19+
expect(result).toEqual(expectedOutput);
20+
});
21+
});

packages/typedoc-plugin-markdown/src/libs/utils/slugify-url.ts renamed to packages/typedoc-plugin-markdown/src/libs/utils/slugify.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function slugifyUrl(url: string) {
1+
export function slugify(url: string) {
22
return url
33
.toLowerCase()
44
.trim()

packages/typedoc-plugin-markdown/src/libs/utils/strip-comments.ts

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { unEscapeChars } from './un-escape-chars';
2+
3+
describe('unEscapeChars', () => {
4+
it('should unescape characters correctly', () => {
5+
const input = '\\*\\<\\>\\_\\{\\}\\`\\*\\|\\]\\[';
6+
const expectedOutput = '*<>_{}`*|][';
7+
8+
const result = unEscapeChars(input);
9+
10+
expect(result).toEqual(expectedOutput);
11+
});
12+
13+
it('should handle string without escaped characters', () => {
14+
const input = 'This is a string without escaped characters';
15+
const expectedOutput = 'This is a string without escaped characters';
16+
17+
const result = unEscapeChars(input);
18+
19+
expect(result).toEqual(expectedOutput);
20+
});
21+
});

packages/typedoc-plugin-markdown/src/theme/core/url-builder.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
getFileNameWithExtension,
44
isQuoted,
55
removeFirstScopedDirectory,
6-
slugifyUrl,
6+
slugify,
77
} from '@plugin/libs/utils';
88
import { OutputFileStrategy } from '@plugin/options/option-maps';
99
import { MarkdownTheme } from '@plugin/theme/markdown-theme';
@@ -331,9 +331,7 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
331331

332332
return urlOption.directory
333333
? urlOption.directory
334-
: `${slugifyUrl(
335-
ReflectionKind.singularString(reflection.kind),
336-
)}.${alias}`;
334+
: `${slugify(ReflectionKind.singularString(reflection.kind))}.${alias}`;
337335
};
338336

339337
const filename = () => {

packages/typedoc-plugin-markdown/src/theme/resources/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export const partials = (context: MarkdownThemeContext) => {
213213
},
214214
) => declaration.apply(context, [model, options]) as string,
215215
/**
216-
* Remders a declaration title.
216+
*
217217
*
218218
* @category Member Partials
219219
*/

packages/typedoc-plugin-markdown/src/theme/resources/partials/member.declarationTitle.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { backTicks, bold, codeBlock } from '@plugin/libs/markdown';
2-
import { escapeChars, stripComments } from '@plugin/libs/utils';
2+
import { escapeChars } from '@plugin/libs/utils';
33
import { MarkdownThemeContext } from '@plugin/theme';
44
import { DeclarationReflection } from 'typedoc';
55

66
/**
7-
* Remders a declaration title.
87
*
98
* @category Member Partials
109
*/
@@ -83,7 +82,7 @@ export function declarationTitle(
8382
model.defaultValue !== '...' &&
8483
model.defaultValue !== model.name
8584
) {
86-
md.push(` = \`${stripComments(model.defaultValue)}\``);
85+
md.push(` = \`${model.defaultValue}\``);
8786
}
8887

8988
if (useCodeBlocks) {

packages/typedoc-plugin-markdown/src/theme/resources/partials/member.reflectionIndex.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { heading, link, table } from '@plugin/libs/markdown';
2-
import { escapeChars, getFirstParagrph } from '@plugin/libs/utils';
2+
import { escapeChars } from '@plugin/libs/utils';
33
import { PLURAL_KIND_KEY_MAP } from '@plugin/options/text-mappings';
44
import { MarkdownThemeContext } from '@plugin/theme';
55
import { TextContentMappings } from 'public-api';
@@ -110,9 +110,7 @@ function getTable(
110110
const comment = context.helpers.getDeclarationComment(child);
111111

112112
if (comment?.summary?.length) {
113-
row.push(
114-
getFirstParagrph(context.partials.commentParts(comment.summary)),
115-
);
113+
row.push(context.partials.commentParts(comment.summary)?.split('\n')[0]);
116114
} else {
117115
row.push('-');
118116
}

0 commit comments

Comments
 (0)