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", "name": "@lingdocs/pashto-inflector",
"version": "0.9.4", "version": "0.9.5",
"author": "lingdocs.com", "author": "lingdocs.com",
"description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations", "description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations",
"homepage": "https://verbs.lingdocs.com", "homepage": "https://verbs.lingdocs.com",

View File

@ -13,6 +13,7 @@ import {
accentFSylsOnNFromEnd, accentFSylsOnNFromEnd,
accentOnNFromEnd, accentOnNFromEnd,
splitUpSyllables, splitUpSyllables,
hasAccents,
} from "./accent-helpers"; } from "./accent-helpers";
const toAccentFront = [ const toAccentFront = [
@ -74,3 +75,14 @@ test(`accentOnNFromEnd should work`, () => {
f: "leedúley", 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 {
); );
} }
function accentSyllable(s: string): string { const accentReplacer = [
const replacer = [
{ vowel: "a", accented: "á" }, { vowel: "a", accented: "á" },
{ vowel: "e", accented: "é" }, { vowel: "e", accented: "é" },
{ vowel: "i", accented: "í" }, { vowel: "i", accented: "í" },
{ vowel: "o", accented: "ó" }, { vowel: "o", accented: "ó" },
{ vowel: "u", accented: "ú" }, { vowel: "u", accented: "ú" },
{ vowel: "U", accented: "Ú" }, { vowel: "U", accented: "Ú" },
]; ];
function accentSyllable(s: string): string {
return s.replace(/a|e|i|o|u|U/, (match) => { 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 */ /* istanbul ignore next */
return r?.accented || ""; return r?.accented || "";
}); });
@ -105,11 +106,18 @@ export function removeAccents(s: T.PsString | string): T.PsString | string {
removeAccents(s.f), removeAccents(s.f),
); );
} }
return s.replace(/á/g, "a") return s.replace(/á|é|í|ó|ú|Ú/, (match) => {
.replace(/é/g, "e") const r = accentReplacer.find((x) => x.accented === match);
.replace(/í/g, "i") /* istanbul ignore next */
.replace(/ó/g, "o") return r?.vowel || "";
.replace(/ú/g, "u") });
.replace(/Á/g, "A") }
.replace(/Ú/g, "U");
/**
* 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"; } from "./lib/diacritics";
import { import {
removeAccents, removeAccents,
hasAccents,
} from "./lib/accent-helpers"; } from "./lib/accent-helpers";
import defaultTextOptions from "./lib/default-text-options"; import defaultTextOptions from "./lib/default-text-options";
import * as grammarUnits from "./lib/grammar-units"; import * as grammarUnits from "./lib/grammar-units";
@ -106,6 +107,7 @@ export {
isInflectionSet, isInflectionSet,
personFromVerbBlockPos, personFromVerbBlockPos,
removeAccents, removeAccents,
hasAccents,
// protobuf helpers // protobuf helpers
readDictionary, readDictionary,
writeDictionary, writeDictionary,