diff --git a/text.test.ts b/text.test.ts deleted file mode 100644 index c3c544f..0000000 --- a/text.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { getIndentCount } from "./text.ts"; -import { assertStrictEquals } from "@std/assert"; - -Deno.test("getIndentCount()", () => { - assertStrictEquals(getIndentCount("sample text "), 0); - assertStrictEquals(getIndentCount(" sample text "), 2); - assertStrictEquals(getIndentCount("   sample text"), 3); - assertStrictEquals(getIndentCount("\t \t  sample text"), 5); -}); diff --git a/text.ts b/text.ts index 4364700..818b64f 100644 --- a/text.ts +++ b/text.ts @@ -1,6 +1,16 @@ +// deno-lint-ignore-file no-irregular-whitespace import { isString } from "@core/unknownutil/is/string"; /** Count the number of leading whitespace characters (indentation level) + * + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(getIndentCount("sample text "), 0); + * assertEquals(getIndentCount(" sample text "), 2); + * assertEquals(getIndentCount("   sample text"), 3); + * assertEquals(getIndentCount("\t \t  sample text"), 5); + * ``` * * @param text - The input {@linkcode string} to analyze * @returns The {@linkcode number} of leading whitespace characters diff --git a/title.test.ts b/title.test.ts deleted file mode 100644 index 135c791..0000000 --- a/title.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { - encodeTitleURI, - revertTitleLc, - toReadableTitleURI, - toTitleLc, -} from "./title.ts"; -import { assertStrictEquals } from "@std/assert"; - -Deno.test("toTitleLc()", async (t) => { - await t.step("` ` -> `_`", () => { - assertStrictEquals( - toTitleLc("空白入り タイトル"), - "空白入り_タイトル", - ); - assertStrictEquals( - toTitleLc(" 前後にも 空白入り _タイトル "), - "_前後にも_空白入り__タイトル_", - ); - }); - - await t.step("upper -> lower", () => { - assertStrictEquals(toTitleLc("Scrapbox-Gyazo"), "scrapbox-gyazo"); - assertStrictEquals( - toTitleLc("全角アルファベット「Scrapbox」も変換できる"), - "全角アルファベット「scrapbox」も変換できる", - ); - }); - await t.step("UPPER Case -> lower_case", () => { - assertStrictEquals( - toTitleLc("Scrapbox is one of the products powered by Nota inc."), - "scrapbox_is_one_of_the_products_powered_by_nota_inc.", - ); - }); -}); - -Deno.test("revertTitleLc()", () => { - assertStrictEquals( - revertTitleLc("Title_with underscore"), - "Title with underscore", - ); -}); - -Deno.test("encodeTitleURI()", async (t) => { - await t.step("tail symbol", () => { - assertStrictEquals(encodeTitleURI(":title:"), ":title%3A"); - }); -}); - -Deno.test("toReadableTitleURI()", async (t) => { - await t.step("only \\w", () => { - assertStrictEquals( - toReadableTitleURI("Normal_TitleAAA"), - "Normal_TitleAAA", - ); - }); - - await t.step("with sparce", () => { - assertStrictEquals( - toReadableTitleURI("Title with Spaces"), - "Title_with_Spaces", - ); - }); - - await t.step("with multibyte characters", () => { - assertStrictEquals( - toReadableTitleURI("日本語_(絵文字✨つき) タイトル"), - "日本語_(絵文字✨つき) タイトル", - ); - }); - - await t.step("encoding //", () => { - assertStrictEquals( - toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"), - "スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です", - ); - assertStrictEquals( - toReadableTitleURI("%2Fなども/と同様percent encodingされる"), - "%252Fなども%2Fと同様percent_encodingされる", - ); - }); -}); diff --git a/title.ts b/title.ts index 1bebb57..98b98cb 100644 --- a/title.ts +++ b/title.ts @@ -1,17 +1,58 @@ +// deno-lint-ignore-file no-irregular-whitespace /** Convert a string to titleLc format * - * - Converts spaces (` `) to underscores (`_`) + * - * - Converts uppercase to lowercase * * Primarily used for comparing links for equality * + * @example Converts spaces (` `) to underscores (`_`) + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(toTitleLc("sample text"), "sample_text"); + * assertEquals( + * toTitleLc("空白入り タイトル"), + * "空白入り_タイトル", + * ); + * assertEquals( + * toTitleLc(" 前後にも 空白入り _タイトル "), + * "_前後にも_空白入り__タイトル_", + * ); + * ``` + * + * @example Converts uppercase to lowercase + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(toTitleLc("Scrapbox-Gyazo"), "scrapbox-gyazo"); + * assertEquals( + * toTitleLc("全角アルファベット「Scrapbox」も変換できる"), + * "全角アルファベット「scrapbox」も変換できる", + * ); + * assertEquals( + * toTitleLc("Scrapbox is one of the products powered by Nota inc."), + * "scrapbox_is_one_of_the_products_powered_by_nota_inc.", + * ); + * ``` + * * @param text - String to convert * @returns A {@linkcode string} containing the converted text in titleLc format */ export const toTitleLc = (text: string): string => text.replaceAll(" ", "_").toLowerCase(); -/** Convert underscores (`_`) to single-byte spaces +/** Convert underscores (`_`) to single-byte spaces (` `) + * + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(revertTitleLc("sample_text"), "sample text"); + * assertEquals( + * revertTitleLc("Title_with underscore"), + * "Title with underscore", + * ); + * ``` * * @param text - String to convert * @returns A {@linkcode string} with underscores converted to spaces @@ -20,6 +61,13 @@ export const revertTitleLc = (text: string): string => text.replaceAll("_", " "); /** Encode a title into a URI-safe format + * + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(encodeTitleURI("sample text"), "sample_text"); + * assertEquals(encodeTitleURI(":title:"), ":title%3A"); + * ``` * * @param title - Title to encode * @returns A {@linkcode string} containing the URI-safe encoded title @@ -41,6 +89,60 @@ const noEncodeChars = '@$&+=:;",'; const noTailChars = ':;",'; /** Convert a title to a URI-safe format while minimizing percent encoding + * + * @example Only words + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals( + * toReadableTitleURI("Normal_TitleAAA"), + * "Normal_TitleAAA", + * ); + * ``` + * + * @example With spaces + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals( + * toReadableTitleURI("Title with Spaces"), + * "Title_with_Spaces", + * ); + * ``` + * + * @example With special characters + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals( + * toReadableTitleURI("Title with special characters: /?{}^|<>%"), + * "Title_with_special_characters:_%2F%3F%7B%7D%5E%7C%3C%3E%25", + * ); + * ``` + * + * @example With multibyte characters + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals( + * toReadableTitleURI("日本語_(絵文字✨つき) タイトル"), + * "日本語_(絵文字✨つき) タイトル", + * ); + * ``` + * + * @example With percent encoding + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals( + * toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"), + * "スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です", + * ); + * assertEquals( + * toReadableTitleURI("%2Fなども/と同様percent encodingされる"), + * "%252Fなども%2Fと同様percent_encodingされる", + * ); + * ``` * * @param title - Title to convert * @returns A {@linkcode string} containing the URI-safe title with minimal percent encoding diff --git a/websocket/isSameArray.test.ts b/websocket/isSameArray.test.ts deleted file mode 100644 index 1a753a0..0000000 --- a/websocket/isSameArray.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { isSameArray } from "./isSameArray.ts"; -import { assert } from "@std/assert"; - -Deno.test("isSameArray()", () => { - assert(isSameArray([1, 2, 3], [1, 2, 3])); - assert(isSameArray([1, 2, 3], [3, 2, 1])); - assert(!isSameArray([1, 2, 3], [3, 2, 3])); - assert(!isSameArray([1, 2, 3], [1, 2])); - assert(isSameArray([], [])); -}); diff --git a/websocket/isSameArray.ts b/websocket/isSameArray.ts index deaa2be..c2720c5 100644 --- a/websocket/isSameArray.ts +++ b/websocket/isSameArray.ts @@ -1,2 +1,21 @@ +/** + * Compare two arrays to see if they are the same. + * + * This function only checks each element's reference, not the content. + * + * ```ts + * import { assert } from "@std/assert/assert"; + * + * assert(isSameArray([1, 2, 3], [1, 2, 3])); + * assert(isSameArray([1, 2, 3], [3, 2, 1])); + * assert(!isSameArray([1, 2, 3], [3, 2, 3])); + * assert(!isSameArray([1, 2, 3], [1, 2])); + * assert(isSameArray([], [])); + * ``` + * + * @param a + * @param b + * @returns + */ export const isSameArray = (a: T[], b: T[]): boolean => a.length === b.length && a.every((x) => b.includes(x)); diff --git a/websocket/suggestUnDupTitle.test.ts b/websocket/suggestUnDupTitle.test.ts deleted file mode 100644 index 5835123..0000000 --- a/websocket/suggestUnDupTitle.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { suggestUnDupTitle } from "./suggestUnDupTitle.ts"; -import { assertEquals } from "@std/assert"; - -Deno.test("suggestUnDupTitle()", () => { - assertEquals(suggestUnDupTitle("title"), "title_2"); - assertEquals(suggestUnDupTitle("title_2"), "title_3"); - assertEquals(suggestUnDupTitle("title_10"), "title_11"); - assertEquals(suggestUnDupTitle("title_10_3"), "title_10_4"); - assertEquals(suggestUnDupTitle("another_title_5"), "another_title_6"); -}); diff --git a/websocket/suggestUnDupTitle.ts b/websocket/suggestUnDupTitle.ts index bfc6c19..63164fb 100644 --- a/websocket/suggestUnDupTitle.ts +++ b/websocket/suggestUnDupTitle.ts @@ -1,3 +1,19 @@ +/** + * Suggest a new title that is already in use. + * + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(suggestUnDupTitle("title"), "title_2"); + * assertEquals(suggestUnDupTitle("title_2"), "title_3"); + * assertEquals(suggestUnDupTitle("title_10"), "title_11"); + * assertEquals(suggestUnDupTitle("title_10_3"), "title_10_4"); + * assertEquals(suggestUnDupTitle("another_title_5"), "another_title_6"); + * ``` + * + * @param title - The title to suggest a new name for + * @returns + */ export const suggestUnDupTitle = (title: string): string => { const matched = title.match(/(.+?)(?:_(\d+))?$/); const title_ = matched?.[1] ?? title;