Skip to content

Commit

Permalink
Merge pull request #28 from miyaoka/fix-last-tag
Browse files Browse the repository at this point in the history
末尾にハッシュタグがある場合に文字数判定がされないのを修正
  • Loading branch information
miyaoka authored Oct 2, 2024
2 parents 48a8261 + 104ae1c commit b2a0412
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/lib/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ describe("getHashTagList", () => {
expect(getHashTagList(input)).toEqual(expected);
});

it("末尾に3文字未満のハッシュタグがある場合も無視する", () => {
const input = "『ドラゴンボールZ:KAKAROT』やるやよッ!!! #05";
expect(getHashTagList(input)).toEqual([]);
});

it("特殊文字を含むハッシュタグを正しく処理する", () => {
const input = "This is a test #hashtag1 #hash_tag2 #hash-tag3";
const expected = ["hashtag1", "hash_tag2", "hash-tag3"];
Expand Down
10 changes: 5 additions & 5 deletions src/lib/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });

// タイトルからhash部分を抜き出す
export function getHashTagList(input: string): string[] {
const result: string[] = [];
let result: string[] = [];

let buffer = "";
let afterHash = false;
Expand All @@ -62,10 +62,7 @@ export function getHashTagList(input: string): string[] {
if (!matched || isHash) {
// ハッシュタグ中ならそれまでの文字列をリストに追加
if (afterHash) {
// 3文字以上なら追加
if (buffer.length > 2) {
result.push(buffer);
}
result.push(buffer);
// reset
buffer = "";
afterHash = false;
Expand All @@ -87,6 +84,9 @@ export function getHashTagList(input: string): string[] {
result.push(buffer);
}

// 3文字未満のハッシュタグは含めない
result = result.filter((tag) => tag.length >= 3);

// ハッシュタグの重複を削除
return [...new Set(result)];
}

0 comments on commit b2a0412

Please sign in to comment.