Skip to content

Commit

Permalink
fix: search-filter ignore case
Browse files Browse the repository at this point in the history
  • Loading branch information
betterRunner committed Sep 11, 2021
1 parent 3c6a028 commit 485e157
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/utils/search-filter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import pinyin, { STYLE_NORMAL } from "pinyin";
import isChinese from "is-chinese";

export function filterBySearchText(arr: any[], key = "", text = "") {
export function filterBySearchText(
arr: any[],
key = "",
text = "",
ignoreCase = true
) {
const _getChPart = (text = "") => {
return text.replace(/[A-Za-z]/g, "");
};
Expand All @@ -12,7 +17,7 @@ export function filterBySearchText(arr: any[], key = "", text = "") {
// searchText contains Chinese text
return arr.filter((e) => {
const item = key ? e[key] : e;
return item.includes(chPart);
return item?.includes(chPart);
});
} else {
return arr.filter((e) => {
Expand All @@ -21,10 +26,10 @@ export function filterBySearchText(arr: any[], key = "", text = "") {
const parts = pinyin(item, {
style: STYLE_NORMAL,
});
return parts
.map((part) => (part.length ? part[0] : ""))
.join("")
.includes(text);
let itemStr = parts.map((part) => (part.length ? part[0] : "")).join("");
itemStr = !ignoreCase ? itemStr : itemStr.toLowerCase();
text = !ignoreCase ? text : text.toLowerCase();
return itemStr.includes(text);
});
}
}

0 comments on commit 485e157

Please sign in to comment.