From 7a61b27b41f10ff9908c309d994094abbd70a267 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Fri, 15 Oct 2021 22:51:51 -0400 Subject: [PATCH] better endsWith --- package.json | 2 +- src/lib/p-text-helpers.test.ts | 16 ++++++++++++++++ src/lib/p-text-helpers.ts | 18 +++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c1a71a3..457ceca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "1.2.0", + "version": "1.2.1", "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 36dfd1b..ff3a08d 100644 --- a/src/lib/p-text-helpers.test.ts +++ b/src/lib/p-text-helpers.test.ts @@ -1138,4 +1138,20 @@ test("endsWith", () => { .toBe(true); expect(endsWith({ p: "چای", f: "chaay" }, [{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }])) .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); }); \ No newline at end of file diff --git a/src/lib/p-text-helpers.ts b/src/lib/p-text-helpers.ts index e0df9eb..dd70188 100644 --- a/src/lib/p-text-helpers.ts +++ b/src/lib/p-text-helpers.ts @@ -966,14 +966,26 @@ export function isPluralInflections(inf: T.PluralInflections | T.Inflections): i * @param matchAccent - true if you want it to be accent-sensitive * @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)) { 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); 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) ); } \ No newline at end of file