-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from traPtitech/feat/make_searchList_better
検索をいい感じに
- Loading branch information
Showing
6 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,35 @@ | ||
export const searchListCaseInsensitive = < | ||
K extends string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Index signature for type 'string' is missing in type を回避 | ||
T extends Record<K, string> & Record<PropertyKey, any> | ||
>( | ||
list: T[], | ||
// https://github.com/traPtitech/traQ_S-UI/blob/983c6f89f3246509011f8ca4d76e2ba24e0beb2b/src/lib/basic/array.ts | ||
|
||
/** | ||
* 一致するキーの一覧を返す | ||
* priorityが0のとき完全一致 | ||
* 1のとき前方一致 | ||
* 2のとき部分一致 | ||
* | ||
* @param arr 検索対象のキーの配列 | ||
* @param query lowercaseになっているクエリ | ||
* @param f キーから検索対象の文字列を取得する関数 | ||
*/ | ||
export const searchListCaseInsensitive = <T>( | ||
arr: readonly T[], | ||
_query: string, | ||
key: K | ||
f: (v: T) => string | ||
): T[] => { | ||
const query = _query.toLowerCase() | ||
return list.filter(item => item[key].toLowerCase().includes(query)) | ||
const result: Array<{ value: T; priority: number }> = [] | ||
|
||
for (const val of arr) { | ||
const valLower = f(val).toLowerCase() | ||
if (valLower === query) { | ||
result.push({ value: val, priority: 0 }) | ||
} else if (valLower.startsWith(query)) { | ||
result.push({ value: val, priority: 1 }) | ||
} else if (valLower.includes(query)) { | ||
result.push({ value: val, priority: 2 }) | ||
} | ||
} | ||
|
||
return result | ||
.toSorted((a, b) => a.priority - b.priority) | ||
.map(({ value }) => value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters