From acbe165d3fa8c16d1c3d137dd8981b05ecbbfd96 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Sat, 16 Oct 2021 11:23:01 -0400 Subject: [PATCH] ability to curry endsWith --- package.json | 2 +- src/lib/p-text-helpers.test.ts | 35 ++++++++++++++++++++-------------- src/lib/p-text-helpers.ts | 31 +++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 457ceca..272cafe 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/p-text-helpers.test.ts b/src/lib/p-text-helpers.test.ts index ff3a08d..a4c7236 100644 --- a/src/lib/p-text-helpers.test.ts +++ b/src/lib/p-text-helpers.test.ts @@ -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); }); \ No newline at end of file diff --git a/src/lib/p-text-helpers.ts b/src/lib/p-text-helpers.ts index dd70188..b23c460 100644 --- a/src/lib/p-text-helpers.ts +++ b/src/lib/p-text-helpers.ts @@ -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 (