Skip to content

Commit 49b4a1d

Browse files
authored
Merge pull request #230 from takker99:doc-test
refactor(test): Move tests into JSDoc comments
2 parents 12efbd4 + a44081c commit 49b4a1d

File tree

8 files changed

+149
-112
lines changed

8 files changed

+149
-112
lines changed

text.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

text.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
// deno-lint-ignore-file no-irregular-whitespace
12
import { isString } from "@core/unknownutil/is/string";
23

34
/** Count the number of leading whitespace characters (indentation level)
5+
*
6+
* ```ts
7+
* import { assertEquals } from "@std/assert/equals";
8+
*
9+
* assertEquals(getIndentCount("sample text "), 0);
10+
* assertEquals(getIndentCount(" sample text "), 2);
11+
* assertEquals(getIndentCount("   sample text"), 3);
12+
* assertEquals(getIndentCount("\t \t  sample text"), 5);
13+
* ```
414
*
515
* @param text - The input {@linkcode string} to analyze
616
* @returns The {@linkcode number} of leading whitespace characters

title.test.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

title.ts

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
1+
// deno-lint-ignore-file no-irregular-whitespace
12
/** Convert a string to titleLc format
23
*
3-
* - Converts spaces (` `) to underscores (`_`)
4+
* -
45
* - Converts uppercase to lowercase
56
*
67
* Primarily used for comparing links for equality
78
*
9+
* @example Converts spaces (` `) to underscores (`_`)
10+
* ```ts
11+
* import { assertEquals } from "@std/assert/equals";
12+
*
13+
* assertEquals(toTitleLc("sample text"), "sample_text");
14+
* assertEquals(
15+
* toTitleLc("空白入り タイトル"),
16+
* "空白入り_タイトル",
17+
* );
18+
* assertEquals(
19+
* toTitleLc(" 前後にも 空白入り _タイトル "),
20+
* "_前後にも_空白入り__タイトル_",
21+
* );
22+
* ```
23+
*
24+
* @example Converts uppercase to lowercase
25+
* ```ts
26+
* import { assertEquals } from "@std/assert/equals";
27+
*
28+
* assertEquals(toTitleLc("Scrapbox-Gyazo"), "scrapbox-gyazo");
29+
* assertEquals(
30+
* toTitleLc("全角アルファベット「Scrapbox」も変換できる"),
31+
* "全角アルファベット「scrapbox」も変換できる",
32+
* );
33+
* assertEquals(
34+
* toTitleLc("Scrapbox is one of the products powered by Nota inc."),
35+
* "scrapbox_is_one_of_the_products_powered_by_nota_inc.",
36+
* );
37+
* ```
38+
*
839
* @param text - String to convert
940
* @returns A {@linkcode string} containing the converted text in titleLc format
1041
*/
1142
export const toTitleLc = (text: string): string =>
1243
text.replaceAll(" ", "_").toLowerCase();
1344

14-
/** Convert underscores (`_`) to single-byte spaces
45+
/** Convert underscores (`_`) to single-byte spaces (` `)
46+
*
47+
* ```ts
48+
* import { assertEquals } from "@std/assert/equals";
49+
*
50+
* assertEquals(revertTitleLc("sample_text"), "sample text");
51+
* assertEquals(
52+
* revertTitleLc("Title_with underscore"),
53+
* "Title with underscore",
54+
* );
55+
* ```
1556
*
1657
* @param text - String to convert
1758
* @returns A {@linkcode string} with underscores converted to spaces
@@ -20,6 +61,13 @@ export const revertTitleLc = (text: string): string =>
2061
text.replaceAll("_", " ");
2162

2263
/** Encode a title into a URI-safe format
64+
*
65+
* ```ts
66+
* import { assertEquals } from "@std/assert/equals";
67+
*
68+
* assertEquals(encodeTitleURI("sample text"), "sample_text");
69+
* assertEquals(encodeTitleURI(":title:"), ":title%3A");
70+
* ```
2371
*
2472
* @param title - Title to encode
2573
* @returns A {@linkcode string} containing the URI-safe encoded title
@@ -41,6 +89,60 @@ const noEncodeChars = '@$&+=:;",';
4189
const noTailChars = ':;",';
4290

4391
/** Convert a title to a URI-safe format while minimizing percent encoding
92+
*
93+
* @example Only words
94+
* ```ts
95+
* import { assertEquals } from "@std/assert/equals";
96+
*
97+
* assertEquals(
98+
* toReadableTitleURI("Normal_TitleAAA"),
99+
* "Normal_TitleAAA",
100+
* );
101+
* ```
102+
*
103+
* @example With spaces
104+
* ```ts
105+
* import { assertEquals } from "@std/assert/equals";
106+
*
107+
* assertEquals(
108+
* toReadableTitleURI("Title with Spaces"),
109+
* "Title_with_Spaces",
110+
* );
111+
* ```
112+
*
113+
* @example With special characters
114+
* ```ts
115+
* import { assertEquals } from "@std/assert/equals";
116+
*
117+
* assertEquals(
118+
* toReadableTitleURI("Title with special characters: /?{}^|<>%"),
119+
* "Title_with_special_characters:_%2F%3F%7B%7D%5E%7C%3C%3E%25",
120+
* );
121+
* ```
122+
*
123+
* @example With multibyte characters
124+
* ```ts
125+
* import { assertEquals } from "@std/assert/equals";
126+
*
127+
* assertEquals(
128+
* toReadableTitleURI("日本語_(絵文字✨つき) タイトル"),
129+
* "日本語_(絵文字✨つき) タイトル",
130+
* );
131+
* ```
132+
*
133+
* @example With percent encoding
134+
* ```ts
135+
* import { assertEquals } from "@std/assert/equals";
136+
*
137+
* assertEquals(
138+
* toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"),
139+
* "スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です",
140+
* );
141+
* assertEquals(
142+
* toReadableTitleURI("%2Fなども/と同様percent encodingされる"),
143+
* "%252Fなども%2Fと同様percent_encodingされる",
144+
* );
145+
* ```
44146
*
45147
* @param title - Title to convert
46148
* @returns A {@linkcode string} containing the URI-safe title with minimal percent encoding

websocket/isSameArray.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

websocket/isSameArray.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
/**
2+
* Compare two arrays to see if they are the same.
3+
*
4+
* This function only checks each element's reference, not the content.
5+
*
6+
* ```ts
7+
* import { assert } from "@std/assert/assert";
8+
*
9+
* assert(isSameArray([1, 2, 3], [1, 2, 3]));
10+
* assert(isSameArray([1, 2, 3], [3, 2, 1]));
11+
* assert(!isSameArray([1, 2, 3], [3, 2, 3]));
12+
* assert(!isSameArray([1, 2, 3], [1, 2]));
13+
* assert(isSameArray([], []));
14+
* ```
15+
*
16+
* @param a
17+
* @param b
18+
* @returns
19+
*/
120
export const isSameArray = <T>(a: T[], b: T[]): boolean =>
221
a.length === b.length && a.every((x) => b.includes(x));

websocket/suggestUnDupTitle.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

websocket/suggestUnDupTitle.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Suggest a new title that is already in use.
3+
*
4+
* ```ts
5+
* import { assertEquals } from "@std/assert/equals";
6+
*
7+
* assertEquals(suggestUnDupTitle("title"), "title_2");
8+
* assertEquals(suggestUnDupTitle("title_2"), "title_3");
9+
* assertEquals(suggestUnDupTitle("title_10"), "title_11");
10+
* assertEquals(suggestUnDupTitle("title_10_3"), "title_10_4");
11+
* assertEquals(suggestUnDupTitle("another_title_5"), "another_title_6");
12+
* ```
13+
*
14+
* @param title - The title to suggest a new name for
15+
* @returns
16+
*/
117
export const suggestUnDupTitle = (title: string): string => {
218
const matched = title.match(/(.+?)(?:_(\d+))?$/);
319
const title_ = matched?.[1] ?? title;

0 commit comments

Comments
 (0)