pashto-grammar/src/lib/game-utils.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-09-03 12:05:05 +00:00
import {
removeAccents,
hasAccents,
Types as T,
standardizePashto,
standardizePhonetics,
flattenLengths,
2022-10-10 10:06:32 +00:00
} from "@lingdocs/ps-react";
import { removeAShort } from "./misc-helpers";
2021-09-03 12:05:05 +00:00
export function getPercentageDone(current: number, total: number): number {
2021-08-31 16:33:44 +00:00
return Math.round(
(current / (total + 1)) * 100
2021-08-31 16:33:44 +00:00
);
}
2021-09-03 12:05:05 +00:00
/**
* Says if an input written in phonetics by the user is correct/the same as a given answer
*
* The user is allowed to leave out the accents, but if they include them they must be the same as the answer
*
* @param input - the answer given by the user in phonetics
* @param answer - the correct answer in phonetics
*/
export function compareF(input: string, answer: string): boolean {
const inp = removeAShort(input);
const ans = removeAShort(answer);
return inp === (hasAccents(inp) ? ans : removeAccents(ans));
}
2023-01-13 11:09:11 +00:00
export function comparePs(inputRaw: string, answer: T.SingleOrLengthOpts<T.PsString | T.PsString[]>): boolean {
2023-01-14 13:56:02 +00:00
function cleanSpaces(s: string): string {
return s.replace(/\s+/g, " ");
}
const input = cleanSpaces(inputRaw);
if ("long" in answer) {
return comparePs(input, flattenLengths(answer));
}
if (Array.isArray(answer)) {
return answer.some(a => comparePs(input, a));
}
2022-08-30 15:08:17 +00:00
const stand = standardizePhonetics(
standardizePashto(input)
).trim();
2023-01-14 13:56:02 +00:00
return stand === cleanSpaces(answer.p) || compareF(stand, cleanSpaces(answer.f));
2021-08-31 16:33:44 +00:00
}