better endsWith

This commit is contained in:
lingdocs 2021-10-15 22:51:51 -04:00
parent e7e773c825
commit 7a61b27b41
3 changed files with 32 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@lingdocs/pashto-inflector", "name": "@lingdocs/pashto-inflector",
"version": "1.2.0", "version": "1.2.1",
"author": "lingdocs.com", "author": "lingdocs.com",
"description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations", "description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations",
"homepage": "https://verbs.lingdocs.com", "homepage": "https://verbs.lingdocs.com",

View File

@ -1138,4 +1138,20 @@ test("endsWith", () => {
.toBe(true); .toBe(true);
expect(endsWith({ p: "چای", f: "chaay" }, [{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }])) expect(endsWith({ p: "چای", f: "chaay" }, [{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }]))
.toBe(true); .toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه"} ))
.toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ت"} ))
.toBe(false);
expect(endsWith({ p: "ویده", f: "weedú" }, { f: "u" } ))
.toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { f: "u" }, true))
.toBe(false);
expect(endsWith({ p: "چت", f: "chat" }, { f: ["d", "t"] }))
.toBe(true);
expect(endsWith({ p: "چت", f: "chat" }, { f: ["d", "D"] }))
.toBe(false);
expect(endsWith({ p: "چت", f: "chat" }, { p: ["د", "ت"] }))
.toBe(true);
expect(endsWith({ p: "چت", f: "chat" }, { p: ["ډ", "د"] }))
.toBe(false);
}); });

View File

@ -966,14 +966,26 @@ export function isPluralInflections(inf: T.PluralInflections | T.Inflections): i
* @param matchAccent - true if you want it to be accent-sensitive * @param matchAccent - true if you want it to be accent-sensitive
* @returns * @returns
*/ */
export function endsWith(ps: T.PsString, ending: T.PsString | T.PsString[], matchAccent?: boolean): boolean { export function endsWith(
ps: T.PsString,
ending: T.PsString | T.PsString[] | { p: string | string[] } | { f: string | string[] },
matchAccent?: boolean,
): boolean {
if (Array.isArray(ending)) { if (Array.isArray(ending)) {
return ending.some(e => endsWith(ps, e)); return ending.some(e => endsWith(ps, e));
} }
if ("p" in ending && Array.isArray(ending.p)) {
return ending.p.some(e => endsWith(ps, { p: e }));
}
if ("f" in ending && Array.isArray(ending.f)) {
return ending.f.some(e => endsWith(ps, { f: e }));
}
const f = removeFVarients(ps.f); const f = removeFVarients(ps.f);
return ( return (
ps.p.slice(-ending.p.length) === ending.p (("p" in ending) ? ps.p.slice(-ending.p.length) === ending.p : true)
&& &&
(matchAccent ? f.slice(-ending.f.length) : removeAccents(f.slice(-ending.f.length))) === ending.f (("f" in ending) ?
((matchAccent ? f.slice(-ending.f.length) : removeAccents(f.slice(-ending.f.length))) === ending.f)
: true)
); );
} }