Skip to content

Commit aedbf1d

Browse files
authored
Merge pull request #20 from takker99/feature-title
✨ Enable to create readable titles for URL
2 parents 5d9c2d7 + cc067ff commit aedbf1d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

title.test.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { encodeTitleURI, revertTitleLc, toTitleLc } from "./title.ts";
1+
import {
2+
encodeTitleURI,
3+
revertTitleLc,
4+
toReadableTitleURI,
5+
toTitleLc,
6+
} from "./title.ts";
27
import { assertStrictEquals } from "./deps/testing.ts";
38

49
Deno.test("toTitleLc()", async (t) => {
@@ -37,3 +42,37 @@ Deno.test("encodeTitleURI()", async (t) => {
3742
assertStrictEquals<string>(encodeTitleURI(":title:"), ":title%3A");
3843
});
3944
});
45+
46+
Deno.test("toReadableTitleURI()", async (t) => {
47+
await t.step("only \\w", () => {
48+
assertStrictEquals<string>(
49+
toReadableTitleURI("Normal_TitleAAA"),
50+
"Normal_TitleAAA",
51+
);
52+
});
53+
54+
await t.step("with sparce", () => {
55+
assertStrictEquals<string>(
56+
toReadableTitleURI("Title with Spaces"),
57+
"Title_with_Spaces",
58+
);
59+
});
60+
61+
await t.step("with multibyte characters", () => {
62+
assertStrictEquals<string>(
63+
toReadableTitleURI("日本語_(絵文字✨つき) タイトル"),
64+
"日本語_(絵文字✨つき) タイトル",
65+
);
66+
});
67+
68+
await t.step("encoding //", () => {
69+
assertStrictEquals<string>(
70+
toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"),
71+
"スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です",
72+
);
73+
assertStrictEquals<string>(
74+
toReadableTitleURI("%2Fなども/と同様percent encodingされる"),
75+
"%252Fなども%2Fと同様percent_encodingされる",
76+
);
77+
});
78+
});

title.ts

+10
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ export const encodeTitleURI = (title: string): string => {
4040

4141
const noEncodeChars = '@$&+=:;",';
4242
const noTailChars = ':;",';
43+
44+
/** titleをできるだけpercent encodingせずにURIで使える形式にする
45+
*
46+
* @param title 変換するtitle
47+
* @return 変換後の文字列
48+
*/
49+
export const toReadableTitleURI = (title: string): string => {
50+
return title.replaceAll(" ", "_")
51+
.replace(/[/?#\{}^|<>%]/g, (char) => encodeURIComponent(char));
52+
};

0 commit comments

Comments
 (0)