ability to curry endsWith

This commit is contained in:
lingdocs 2021-10-16 11:23:01 -04:00
parent 7a61b27b41
commit acbe165d3f
3 changed files with 44 additions and 24 deletions

View File

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

View File

@ -1125,33 +1125,40 @@ test("splitPsByVarients", () => {
});
test("endsWith", () => {
expect(endsWith({ p: "سړی", f: "saRey" }, { p: "ی", f: "ey" }))
expect(endsWith({ p: "ی", f: "ey" }, { p: "سړی", f: "saRey" }))
.toBe(true);
// f variations should be removed in case of using DictionaryEntry
expect(endsWith({ p: "سړی", f: "saRey, saRaayyy" }, { p: "ی", f: "ey" }))
expect(endsWith({ p: "ی", f: "ey" }, { p: "سړی", f: "saRey, saRaayyy" }))
.toBe(true);
expect(endsWith({ p: "سړی", f: "saRey" }, { p: "ي", f: "ee" }))
expect(endsWith({ p: "ي", f: "ee" }, { p: "سړی", f: "saRey" }))
.toBe(false);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه", f: "u" }, true))
expect(endsWith({ p: "ه", f: "u" }, { p: "ویده", f: "weedú" }, true))
.toBe(false);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه", f: "u" }))
expect(endsWith({ p: "ه", f: "u" }, { p: "ویده", f: "weedú" }))
.toBe(true);
expect(endsWith({ p: "چای", f: "chaay" }, [{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }]))
expect(endsWith([{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }], { p: "چای", f: "chaay" }))
.toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه"} ))
expect(endsWith({ p: "ه"}, { p: "ویده", f: "weedú" }))
.toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ت"} ))
expect(endsWith({ p: "ت"}, { p: "ویده", f: "weedú" }))
.toBe(false);
expect(endsWith({ p: "ویده", f: "weedú" }, { f: "u" } ))
expect(endsWith({ f: "u" }, { p: "ویده", f: "weedú" }))
.toBe(true);
expect(endsWith({ p: "ویده", f: "weedú" }, { f: "u" }, true))
expect(endsWith({ f: "u" }, { p: "ویده", f: "weedú" }, true))
.toBe(false);
expect(endsWith({ p: "چت", f: "chat" }, { f: ["d", "t"] }))
expect(endsWith({ f: ["d", "t"] }, { p: "چت", f: "chat" }))
.toBe(true);
expect(endsWith({ p: "چت", f: "chat" }, { f: ["d", "D"] }))
expect(endsWith({ f: ["d", "D"] }, { p: "چت", f: "chat" }))
.toBe(false);
expect(endsWith({ p: "چت", f: "chat" }, { p: ["د", "ت"] }))
expect(endsWith({ p: ["د", "ت"] }, { p: "چت", f: "chat" }))
.toBe(true);
expect(endsWith({ p: "چت", f: "chat" }, { p: ["ډ", "د"] }))
expect(endsWith({ p: ["ډ", "د"] }, { p: "چت", f: "chat" }))
.toBe(false);
// ability to curry
expect(endsWith({ p: "ی", f: "ey" })({ p: "سړی", f: "saRéy" }))
.toBe(true);
expect(endsWith({ p: "ی", f: "ey" }, true)({ p: "سړی", f: "saRéy" }))
.toBe(false);
expect(endsWith({ f: ["d", "D"] })({ p: "چت", f: "chat" }))
.toBe(false);
});

View File

@ -961,24 +961,37 @@ export function isPluralInflections(inf: T.PluralInflections | T.Inflections): i
* determines if ps ends with a given ending, or one of an array of given endings
* (can be accent sensitive or not)
*
* @param ps - the PsString in question
* @param ending - an ending (or array of possible endings) to check for
* @param matchAccent - true if you want it to be accent-sensitive
* @returns
* regular - `endsWith(ending, string, matchAccent?)` returns `boolean`
* curried - `endsWith(ending, matchAccent?)` returns `(ps) => boolean`
*
*/
export function endsWith(
ps: T.PsString,
ending: T.PsString | T.PsString[] | { p: string | string[] } | { f: string | string[] },
ps?: boolean,
): (ps: T.PsString) => boolean;
export function endsWith(
ending: T.PsString | T.PsString[] | { p: string | string[] } | { f: string | string[] },
ps: T.PsString,
matchAccent?: boolean,
): boolean {
): boolean;
export function endsWith(
ending: T.PsString | T.PsString[] | { p: string | string[] } | { f: string | string[] },
ps: T.PsString | undefined | boolean,
matchAccent?: boolean | undefined,
): boolean | ((ps: T.PsString) => boolean) {
// curried version
if (ps === undefined || typeof ps === "boolean") {
const matchAccent = !!ps;
return (ps: T.PsString) => endsWith(ending, ps, matchAccent);
}
if (Array.isArray(ending)) {
return ending.some(e => endsWith(ps, e));
return ending.some(e => endsWith(e, ps));
}
if ("p" in ending && Array.isArray(ending.p)) {
return ending.p.some(e => endsWith(ps, { p: e }));
return ending.p.some(e => endsWith({ p: e }, ps));
}
if ("f" in ending && Array.isArray(ending.f)) {
return ending.f.some(e => endsWith(ps, { f: e }));
return ending.f.some(e => endsWith({ f: e }, ps));
}
const f = removeFVarients(ps.f);
return (