add hasAccents utility

This commit is contained in:
lingdocs 2021-09-03 15:27:02 +04:00
parent d75a3d1cd0
commit 35b91c12b7
4 changed files with 39 additions and 17 deletions

View File

@ -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",

View File

@ -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);
});
})

View File

@ -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));
}

View File

@ -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,