parent
ea48bfa630
commit
06d2c2de91
@ -0,0 +1,43 @@ |
|||||||
|
import t from 'tap'; |
||||||
|
|
||||||
|
import { getRegexStringForThesaurusPath } from './viteThesaurusRegexPlugin.js'; |
||||||
|
|
||||||
|
void t.test('extractWordsFromFile', async (t) => { |
||||||
|
const regexString = await getRegexStringForThesaurusPath( |
||||||
|
new URL('../../build-resources/th-en-x-basic.dat', import.meta.url), |
||||||
|
); |
||||||
|
const regex = new RegExp(regexString, 'gi'); |
||||||
|
|
||||||
|
const getMatches = (str: string) => |
||||||
|
[...str.matchAll(regex)].map((match) => match[0]); |
||||||
|
|
||||||
|
t.strictSame( |
||||||
|
getMatches( |
||||||
|
'afterthought airplane another anybody anyhow anyone anything anywhere', |
||||||
|
), |
||||||
|
[], |
||||||
|
); |
||||||
|
|
||||||
|
t.strictSame( |
||||||
|
getMatches( |
||||||
|
'afterthought airplane another xxx anybody anyhow anyone anything anywhere', |
||||||
|
), |
||||||
|
['xxx'], |
||||||
|
); |
||||||
|
|
||||||
|
t.strictSame( |
||||||
|
getMatches( |
||||||
|
'xx afterthought airplane another yy anybody anyhow anyone anything anywhere zzzz', |
||||||
|
), |
||||||
|
['xx', 'yy', 'zzzz'], |
||||||
|
); |
||||||
|
|
||||||
|
t.strictSame(getMatches('xx yy zzzz'), ['xx', 'yy', 'zzzz']); |
||||||
|
|
||||||
|
t.strictSame( |
||||||
|
getMatches( |
||||||
|
"Thing to help people escape really fast if there's a problem and everything is on fire so they decide not to go to space", |
||||||
|
), |
||||||
|
[], |
||||||
|
); |
||||||
|
}); |
@ -0,0 +1,25 @@ |
|||||||
|
import type { PathLike } from 'node:fs'; |
||||||
|
import { extractWordsFromFile } from './oooThesaurusParser.js'; |
||||||
|
|
||||||
|
/*type ViteThesaurusRegexPluginOptions = { |
||||||
|
inputOooThesaurusPath: PathLike; |
||||||
|
outputRegexPath: string; |
||||||
|
};*/ |
||||||
|
|
||||||
|
const WORD_CHARACTER_REGEX_FRAGMENT = "[a-zA-Z']"; |
||||||
|
|
||||||
|
export const getRegexStringForThesaurusPath = async ( |
||||||
|
inputOooThesaurusPath: PathLike, |
||||||
|
) => { |
||||||
|
const words = await extractWordsFromFile(inputOooThesaurusPath); |
||||||
|
return `(?<!${WORD_CHARACTER_REGEX_FRAGMENT})${WORD_CHARACTER_REGEX_FRAGMENT}+(?!${WORD_CHARACTER_REGEX_FRAGMENT})(?<!(?<!${WORD_CHARACTER_REGEX_FRAGMENT})(${words.join( |
||||||
|
'|', |
||||||
|
)}))`;
|
||||||
|
}; |
||||||
|
|
||||||
|
/*export const viteThesaurusRegexPlugin = ({ |
||||||
|
inputOooThesaurusPath, |
||||||
|
outputRegexPath, |
||||||
|
}: ViteThesaurusRegexPluginOptions) => { |
||||||
|
}; |
||||||
|
*/ |
Loading…
Reference in new issue