From 35b91c12b709193ed20e89ebc173e9ec57ee45b7 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Fri, 3 Sep 2021 15:27:02 +0400 Subject: [PATCH] add hasAccents utility --- package.json | 2 +- src/lib/accent-helpers.test.ts | 12 ++++++++++ src/lib/accent-helpers.ts | 40 ++++++++++++++++++++-------------- src/library.ts | 2 ++ 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 4a4cccb..cfa459d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "0.9.4", + "version": "0.9.5", "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/accent-helpers.test.ts b/src/lib/accent-helpers.test.ts index 63c517a..30b36a4 100644 --- a/src/lib/accent-helpers.test.ts +++ b/src/lib/accent-helpers.test.ts @@ -13,6 +13,7 @@ import { accentFSylsOnNFromEnd, accentOnNFromEnd, splitUpSyllables, + hasAccents, } from "./accent-helpers"; const toAccentFront = [ @@ -74,3 +75,14 @@ test(`accentOnNFromEnd should work`, () => { f: "leedúley", }); }); + +test(`has accents should work`, () => { + const accents = ["koRanúy", "wutáaq", "gÚta", "taté", "bít", "sóra", "kúcha"]; + const noAccents = ["koRanuy", "wutaaq", "gUta", "tate", "bit", "sora", "kucha"]; + accents.forEach((x) => { + expect(hasAccents(x)).toBe(true); + }); + noAccents.forEach((x) => { + expect(hasAccents(x)).toBe(false); + }); +}) diff --git a/src/lib/accent-helpers.ts b/src/lib/accent-helpers.ts index ad4c74d..d92223c 100644 --- a/src/lib/accent-helpers.ts +++ b/src/lib/accent-helpers.ts @@ -80,17 +80,18 @@ export function accentOnNFromEnd(ps: T.PsString, n: number): T.PsString { ); } +const accentReplacer = [ + { vowel: "a", accented: "á" }, + { vowel: "e", accented: "é" }, + { vowel: "i", accented: "í" }, + { vowel: "o", accented: "ó" }, + { vowel: "u", accented: "ú" }, + { vowel: "U", accented: "Ú" }, +]; + function accentSyllable(s: string): string { - const replacer = [ - { vowel: "a", accented: "á" }, - { vowel: "e", accented: "é" }, - { vowel: "i", accented: "í" }, - { vowel: "o", accented: "ó" }, - { vowel: "u", accented: "ú" }, - { vowel: "U", accented: "Ú" }, - ]; return s.replace(/a|e|i|o|u|U/, (match) => { - const r = replacer.find((x) => x.vowel === match); + const r = accentReplacer.find((x) => x.vowel === match); /* istanbul ignore next */ return r?.accented || ""; }); @@ -105,11 +106,18 @@ export function removeAccents(s: T.PsString | string): T.PsString | string { removeAccents(s.f), ); } - return s.replace(/á/g, "a") - .replace(/é/g, "e") - .replace(/í/g, "i") - .replace(/ó/g, "o") - .replace(/ú/g, "u") - .replace(/Á/g, "A") - .replace(/Ú/g, "U"); + return s.replace(/á|é|í|ó|ú|Ú/, (match) => { + const r = accentReplacer.find((x) => x.accented === match); + /* istanbul ignore next */ + return r?.vowel || ""; + }); +} + +/** + * Determines if a string has any accents on it + * + * @param s a string of Pashto phonetics + */ +export function hasAccents(s: string): boolean { + return accentReplacer.some((x) => s.includes(x.accented)); } diff --git a/src/library.ts b/src/library.ts index d723f3a..6982424 100644 --- a/src/library.ts +++ b/src/library.ts @@ -75,6 +75,7 @@ import { } from "./lib/diacritics"; import { removeAccents, + hasAccents, } from "./lib/accent-helpers"; import defaultTextOptions from "./lib/default-text-options"; import * as grammarUnits from "./lib/grammar-units"; @@ -106,6 +107,7 @@ export { isInflectionSet, personFromVerbBlockPos, removeAccents, + hasAccents, // protobuf helpers readDictionary, writeDictionary,