From dc67754a76198fbf7b57c37eb528140a1442f213 Mon Sep 17 00:00:00 2001 From: RaenonX Date: Sun, 3 Oct 2021 06:52:31 -0500 Subject: [PATCH] IMP - Refactor textual searching utils Signed-off-by: RaenonX --- .../elements/gameData/unit/filter/utils.ts | 15 ++++---- src/utils/text.ts | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/utils/text.ts diff --git a/src/components/elements/gameData/unit/filter/utils.ts b/src/components/elements/gameData/unit/filter/utils.ts index bc90986c..726a2beb 100644 --- a/src/components/elements/gameData/unit/filter/utils.ts +++ b/src/components/elements/gameData/unit/filter/utils.ts @@ -1,7 +1,6 @@ -import {OpenCC} from 'opencc'; - import {UnitNameRefData, UnitType} from '../../../../../api-def/api'; import {CharaInfo, CharaInfoData, DragonInfo, UnitInfoData, UnitInfoDataBase} from '../../../../../api-def/resources'; +import {transformForSearch} from '../../../../../utils/text'; import {UnitFilterInputData} from './types'; @@ -23,8 +22,6 @@ export const getFilteredUnitInfo = ( return []; } - // Config name doc: https://github.com/BYVoid/OpenCC#configurations-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6 - const openCC: OpenCC = new OpenCC('s2t.json'); const ret: Array = []; const isUnitElementMatch = (unit: UnitInfoDataBase) => ( @@ -40,14 +37,18 @@ export const getFilteredUnitInfo = ( return true; } - const keywordProcessed = openCC.convertSync(inputData.keyword.toLowerCase()); + const keywordProcessed = transformForSearch(inputData.keyword.toLowerCase()); const isKeywordPartialUnitName = Object .values(unit.name) - .some((name) => name.toLowerCase().indexOf(keywordProcessed) >= 0); + .some((name) => ( + transformForSearch(name, {variantInsensitive: false}).indexOf(keywordProcessed) >= 0 + )); const isKeywordPartialCustomName = Object.entries(unitNameRef) .filter(([_, referencedUnitId]) => unit.id === referencedUnitId) - .some(([name, _]) => name.toLowerCase().indexOf(keywordProcessed) >= 0); + .some(([name, _]) => ( + transformForSearch(name, {variantInsensitive: false}).indexOf(keywordProcessed) >= 0 + )); return isKeywordPartialUnitName || isKeywordPartialCustomName; }; diff --git a/src/utils/text.ts b/src/utils/text.ts new file mode 100644 index 00000000..b96cbe84 --- /dev/null +++ b/src/utils/text.ts @@ -0,0 +1,34 @@ +import {OpenCC} from 'opencc'; + +import {overrideObject} from './override'; +import {DeepPartial} from './types'; + + +// Config name doc: https://github.com/BYVoid/OpenCC#configurations-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6 +const openCC: OpenCC = new OpenCC('s2t.json'); + +type TransformOptions = { + caseInsensitive: boolean, + variantInsensitive: boolean, +} + +export const transformForSearch = (text: string, options?: DeepPartial): string => { + const transformOptions: TransformOptions = overrideObject( + { + caseInsensitive: true, + variantInsensitive: true, + }, + options, + {originalOnly: true}, + ); + + if (transformOptions.caseInsensitive) { + text = text.toLowerCase(); + } + + if (transformOptions.variantInsensitive) { + text = openCC.convertSync(text); + } + + return text; +};