From e7e773c82589ec606ed784861b78d26c93af6c4d Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Fri, 15 Oct 2021 21:58:56 -0400 Subject: [PATCH] endsWith function --- package.json | 2 +- src/lib/p-text-helpers.test.ts | 19 ++++++++++++++++++- src/lib/p-text-helpers.ts | 21 +++++++++++++++++++++ src/library.ts | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e86a30f..c1a71a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "1.1.9", + "version": "1.2.0", "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 21af42e..36dfd1b 100644 --- a/src/lib/p-text-helpers.test.ts +++ b/src/lib/p-text-helpers.test.ts @@ -25,6 +25,7 @@ import { removeFVarients, endsInShwa, splitPsByVarients, + endsWith, } from "./p-text-helpers"; import * as T from "../types"; import { @@ -1121,4 +1122,20 @@ test("splitPsByVarients", () => { .toEqual([{ p: "حوادث", f: "hawáadis" }, { p: "حادثات", f: "haadisáat" }]); expect(splitPsByVarients({ p: "کور", f: "kor" })) .toEqual([{ p: "کور", f: "kor" }]); -}) \ No newline at end of file +}); + +test("endsWith", () => { + expect(endsWith({ p: "سړی", f: "saRey" }, { p: "ی", f: "ey" })) + .toBe(true); + // f variations should be removed in case of using DictionaryEntry + expect(endsWith({ p: "سړی", f: "saRey, saRaayyy" }, { p: "ی", f: "ey" })) + .toBe(true); + expect(endsWith({ p: "سړی", f: "saRey" }, { p: "ي", f: "ee" })) + .toBe(false); + expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه", f: "u" }, true)) + .toBe(false); + expect(endsWith({ p: "ویده", f: "weedú" }, { p: "ه", f: "u" })) + .toBe(true); + expect(endsWith({ p: "چای", f: "chaay" }, [{ p: "وی", f: "ooy" }, { p: "ای", f: "aay" }])) + .toBe(true); +}); \ No newline at end of file diff --git a/src/lib/p-text-helpers.ts b/src/lib/p-text-helpers.ts index 753dc58..e0df9eb 100644 --- a/src/lib/p-text-helpers.ts +++ b/src/lib/p-text-helpers.ts @@ -955,4 +955,25 @@ export function isPluralInflections(inf: T.PluralInflections | T.Inflections): i return inf.masc.length === 2; } return inf.fem.length === 2; +} + +/** + * 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 + */ +export function endsWith(ps: T.PsString, ending: T.PsString | T.PsString[], matchAccent?: boolean): boolean { + if (Array.isArray(ending)) { + return ending.some(e => endsWith(ps, e)); + } + const f = removeFVarients(ps.f); + return ( + ps.p.slice(-ending.p.length) === ending.p + && + (matchAccent ? f.slice(-ending.f.length) : removeAccents(f.slice(-ending.f.length))) === ending.f + ); } \ No newline at end of file diff --git a/src/library.ts b/src/library.ts index 2d8c882..575346e 100644 --- a/src/library.ts +++ b/src/library.ts @@ -36,6 +36,7 @@ import { isUnisexSet, isInflectionSet, addEnglish, + endsWith, } from "./lib/p-text-helpers"; import { getEnglishWord, @@ -132,6 +133,7 @@ export { personGender, addEnglish, parseEc, + endsWith, // protobuf helpers readDictionary, writeDictionary,