Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion title.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { encodeTitleURI, revertTitleLc, toTitleLc } from "./title.ts";
import {
encodeTitleURI,
revertTitleLc,
toReadableTitleURI,
toTitleLc,
} from "./title.ts";
import { assertStrictEquals } from "./deps/testing.ts";

Deno.test("toTitleLc()", async (t) => {
Expand Down Expand Up @@ -37,3 +42,37 @@ Deno.test("encodeTitleURI()", async (t) => {
assertStrictEquals<string>(encodeTitleURI(":title:"), ":title%3A");
});
});

Deno.test("toReadableTitleURI()", async (t) => {
await t.step("only \\w", () => {
assertStrictEquals<string>(
toReadableTitleURI("Normal_TitleAAA"),
"Normal_TitleAAA",
);
});

await t.step("with sparce", () => {
assertStrictEquals<string>(
toReadableTitleURI("Title with Spaces"),
"Title_with_Spaces",
);
});

await t.step("with multibyte characters", () => {
assertStrictEquals<string>(
toReadableTitleURI("日本語_(絵文字✨つき) タイトル"),
"日本語_(絵文字✨つき) タイトル",
);
});

await t.step("encoding //", () => {
assertStrictEquals<string>(
toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"),
"スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です",
);
assertStrictEquals<string>(
toReadableTitleURI("%2Fなども/と同様percent encodingされる"),
"%252Fなども%2Fと同様percent_encodingされる",
);
});
});
10 changes: 10 additions & 0 deletions title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ export const encodeTitleURI = (title: string): string => {

const noEncodeChars = '@$&+=:;",';
const noTailChars = ':;",';

/** titleをできるだけpercent encodingせずにURIで使える形式にする
*
* @param title 変換するtitle
* @return 変換後の文字列
*/
export const toReadableTitleURI = (title: string): string => {
return title.replaceAll(" ", "_")
.replace(/[/?#\{}^|<>%]/g, (char) => encodeURIComponent(char));
};