Skip to content

Commit

Permalink
Merge pull request #23 from miyaoka/rename
Browse files Browse the repository at this point in the history
refactor: rename
  • Loading branch information
miyaoka authored Oct 2, 2024
2 parents 7a05a47 + f91d729 commit 7473d34
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/components/menu/FilteringButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const searchStore = useSearchStore();
const eventListStore = useEventListStore();
function clear() {
searchStore.setSearchTerm("");
searchStore.setSearchString("");
}
</script>

Expand Down
6 changes: 3 additions & 3 deletions src/components/menu/SearchFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function onInput() {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
searchStore.setSearchTerm(searchQuery.value);
searchStore.setSearchString(searchQuery.value);
}, 500);
}
Expand Down Expand Up @@ -44,13 +44,13 @@ function onClick() {
inputEl.value?.focus();
} else {
// close
searchStore.setSearchTerm("");
searchStore.setSearchString("");
searchQuery.value = "";
}
}
watch(
() => searchStore.searchTerm,
() => searchStore.searchString,
(newVal) => {
searchQuery.value = newVal;
isInput.value = newVal !== "";
Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/keyword/KeywordListPopover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const searchStore = useSearchStore();
const filteredEventList = computed(() => {
const list = eventListStore.liverEventList;
if (!list) return [];
const searchQuery = searchStore.parsedSearchInput;
const searchQuery = searchStore.searchQuery;
return getFilteredEventList({
liverEventList: list,
filterMap: storageStore.talentFilterMap,
Expand Down
6 changes: 3 additions & 3 deletions src/components/menu/keyword/KeywordListPopoverItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function setSearchTerm(term: string) {
// 空白を含むならダブルクォーテーションで囲む
const formattedTerm = term.includes(" ") ? `"${term}"` : term;
// 同じものなら検索を解除
if (searchStore.searchTerm === formattedTerm) {
searchStore.setSearchTerm("");
if (searchStore.searchString === formattedTerm) {
searchStore.setSearchString("");
return;
}
searchStore.setSearchTerm(formattedTerm);
searchStore.setSearchString(formattedTerm);
}
</script>

Expand Down
32 changes: 16 additions & 16 deletions src/lib/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { parseInput, createSearchRegexp, searchQueryToTerms } from "./search";
import { parseSearchString, createSearchRegexp, searchQueryToSaerchString } from "./search";

describe("parseInput", () => {
it("引用符で囲まれた文字列は空白があっても区切られないこと", () => {
Expand All @@ -9,7 +9,7 @@ describe("parseInput", () => {
hashtagList: [],
options: {},
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("接頭辞がある文字列とそれ以外の文字列に分割できること", () => {
Expand All @@ -19,7 +19,7 @@ describe("parseInput", () => {
hashtagList: [],
options: { tag: ["example"], status: ["live"] },
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("同じ接頭辞が指定されていたらすべてが配列に追加されること", () => {
Expand All @@ -29,7 +29,7 @@ describe("parseInput", () => {
hashtagList: [],
options: { tag: ["example", "example2"] },
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("クォートされた接頭辞の値が正しく処理されること", () => {
Expand All @@ -39,7 +39,7 @@ describe("parseInput", () => {
hashtagList: [],
options: { talent: ["the talent"] },
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("空のとき", () => {
Expand All @@ -49,7 +49,7 @@ describe("parseInput", () => {
hashtagList: [],
options: {},
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("複数の接頭辞と単語が混在する場合", () => {
Expand All @@ -59,7 +59,7 @@ describe("parseInput", () => {
hashtagList: [],
options: { tag: ["example", "example2"], status: ["live"] },
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("hashtagがある場合", () => {
Expand All @@ -69,7 +69,7 @@ describe("parseInput", () => {
hashtagList: ["tag1", "tag2"],
options: {},
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});

it("unicode対応", () => {
Expand All @@ -79,7 +79,7 @@ describe("parseInput", () => {
hashtagList: ["日本語タグ"],
options: { tag: ["絵文🔥字", "日本語 空白入り"] },
};
expect(parseInput(input)).toEqual(expected);
expect(parseSearchString(input)).toEqual(expected);
});
});

Expand Down Expand Up @@ -134,7 +134,7 @@ describe("searchQueryToTerms", () => {
};
const expected = "example test";

expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("オプションを含むクエリを文字列に変換できること", () => {
Expand All @@ -144,7 +144,7 @@ describe("searchQueryToTerms", () => {
options: { tag: ["exampleTag"], status: ["live"] },
};
const expected = "example tag:exampleTag status:live";
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("ハッシュタグを含むクエリを文字列に変換できること", () => {
Expand All @@ -154,7 +154,7 @@ describe("searchQueryToTerms", () => {
options: {},
};
const expected = "example #tag1 #tag2";
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("単語、オプション、ハッシュタグを含むクエリを文字列に変換できること", () => {
Expand All @@ -164,7 +164,7 @@ describe("searchQueryToTerms", () => {
options: { tag: ["exampleTag"], status: ["live"] },
};
const expected = "example test tag:exampleTag status:live #tag1";
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("空のクエリを空文字列に変換できること", () => {
Expand All @@ -174,7 +174,7 @@ describe("searchQueryToTerms", () => {
options: {},
};
const expected = "";
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("オプションが空の配列の場合は無視されること", () => {
Expand All @@ -184,7 +184,7 @@ describe("searchQueryToTerms", () => {
options: { tag: [] },
};
const expected = "example";
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});

it("空白を含むオプションの値がクォートされること", () => {
Expand All @@ -194,6 +194,6 @@ describe("searchQueryToTerms", () => {
options: { tag: ["example tag"] },
};
const expected = 'example "the space" tag:"example tag"';
expect(searchQueryToTerms(searchQuery)).toEqual(expected);
expect(searchQueryToSaerchString(searchQuery)).toEqual(expected);
});
});
5 changes: 3 additions & 2 deletions src/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const regexPattern = `(?<quoted>${quoted})|(?<optionKey>${optionKey}):(?<optionV
const searchRegex = new RegExp(regexPattern, "g");

// 正規表現でハッシュタグ、単語、オプションを抽出
export function parseInput(input: string): SearchQuery {
export function parseSearchString(input: string): SearchQuery {
const wordList: string[] = [];
const hashtagList: string[] = [];
const options: Record<string, string[]> = {};
Expand Down Expand Up @@ -57,7 +57,8 @@ function quoteIfSpace(str: string): string {
return str.includes(" ") ? `"${str}"` : str;
}

export function searchQueryToTerms(searchQuery: SearchQuery): string {
// SearchQueryを検索文字列に変換
export function searchQueryToSaerchString(searchQuery: SearchQuery): string {
const { wordList, options, hashtagList } = searchQuery;
const optionStr = Object.entries(options)
.flatMap(([key, valueList]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/store/eventListStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useEventListStore = defineStore("eventListStore", () => {
return getFilteredEventList({
liverEventList: liverEventList.value,
filterMap: storageStore.talentFilterMap,
searchQuery: searchStore.parsedSearchInput,
searchQuery: searchStore.searchQuery,
});
});

Expand Down
42 changes: 21 additions & 21 deletions src/store/searchStore.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { acceptHMRUpdate, defineStore } from "pinia";
import { computed, ref } from "vue";
import { useScrollStore } from "./scrollStore";
import { parseInput, searchQueryToTerms } from "@/lib/search";
import { parseSearchString, searchQueryToSaerchString } from "@/lib/search";

export const useSearchStore = defineStore("searchStore", () => {
const scrollStore = useScrollStore();

const searchTerm = ref("");
const searchString = ref("");

const parsedSearchInput = computed(() => {
return parseInput(searchTerm.value);
const searchQuery = computed(() => {
return parseSearchString(searchString.value);
});
const hasQuery = computed(() => {
const { wordList, options, hashtagList } = parsedSearchInput.value;
const { wordList, options, hashtagList } = searchQuery.value;
return wordList.length > 0 || Object.keys(options).length > 0 || hashtagList.length > 0;
});
const searchStatusList = computed(() => {
return parsedSearchInput.value.options.status ?? [];
return searchQuery.value.options.status ?? [];
});
const searchTalentList = computed(() => {
return parsedSearchInput.value.options.talent ?? [];
return searchQuery.value.options.talent ?? [];
});
const isLiveOnly = computed(() => {
return searchStatusList.value.includes("live");
});

function setSearchTerm(term: string) {
function setSearchString(term: string) {
// 未入力状態であればスクロール位置を保存する
if (searchTerm.value === "") {
if (searchString.value === "") {
scrollStore.savePosition();
}
searchTerm.value = term;
searchString.value = term;

// 入力がクリアされたらスクロール位置をリセットする
if (term === "") {
Expand All @@ -55,15 +55,15 @@ export const useSearchStore = defineStore("searchStore", () => {
scrollStore.restorePosition();
}

const { options } = parsedSearchInput.value;
const { options } = searchQuery.value;
let newStatusList = [];
if (isLiveOnly.value) {
newStatusList = searchStatusList.value.filter((status) => status !== "live");
} else {
newStatusList = [...searchStatusList.value, "live"];
}
searchTerm.value = searchQueryToTerms({
...parsedSearchInput.value,
searchString.value = searchQueryToSaerchString({
...searchQuery.value,
options: { ...options, status: newStatusList },
});
}
Expand All @@ -74,12 +74,12 @@ export const useSearchStore = defineStore("searchStore", () => {
scrollStore.savePosition();
}

const { options } = parsedSearchInput.value;
const { options } = searchQuery.value;

// 解除
if (talent === null) {
searchTerm.value = searchQueryToTerms({
...parsedSearchInput.value,
searchString.value = searchQueryToSaerchString({
...searchQuery.value,
options: { ...options, talent: [] },
});
} else {
Expand All @@ -91,8 +91,8 @@ export const useSearchStore = defineStore("searchStore", () => {
// セット
newTalentList = [talent];
}
searchTerm.value = searchQueryToTerms({
...parsedSearchInput.value,
searchString.value = searchQueryToSaerchString({
...searchQuery.value,
options: { ...options, talent: newTalentList },
});
}
Expand All @@ -104,10 +104,10 @@ export const useSearchStore = defineStore("searchStore", () => {
}

return {
searchTerm,
setSearchTerm,
searchString,
setSearchString,
setFocusedTalent,
parsedSearchInput,
searchQuery,
isLiveOnly,
toggleLiveOnly,
hasQuery,
Expand Down

0 comments on commit 7473d34

Please sign in to comment.