Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
IMP - Refactor textual searching utils
Browse files Browse the repository at this point in the history
Signed-off-by: RaenonX <[email protected]>
  • Loading branch information
RaenonX committed Oct 3, 2021
1 parent 721e914 commit dc67754
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/components/elements/gameData/unit/filter/utils.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand All @@ -23,8 +22,6 @@ export const getFilteredUnitInfo = <S extends string>(
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<UnitInfoData> = [];

const isUnitElementMatch = (unit: UnitInfoDataBase) => (
Expand All @@ -40,14 +37,18 @@ export const getFilteredUnitInfo = <S extends string>(
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;
};
Expand Down
34 changes: 34 additions & 0 deletions src/utils/text.ts
Original file line number Diff line number Diff line change
@@ -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<TransformOptions>): 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;
};

0 comments on commit dc67754

Please sign in to comment.