diff --git a/package.json b/package.json index de6b7da..e79d90d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "0.8.5", + "version": "0.9.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/App.tsx b/src/App.tsx index 532764b..3248ee2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -24,7 +24,7 @@ import { } from "react-bootstrap"; import * as T from "./types"; import defualtTextOptions from "./lib/default-text-options"; - +const textOptionsLocalStorageName = "textOptions2"; type VerbType = "simple" | "stative compound" | "dynamic compound"; const verbTypes: VerbType[] = [ "simple", @@ -66,7 +66,7 @@ function App() { const regularIrregular = localStorage.getItem("regularIrregular") as "regular" | "irregular"; const transitivitiyShowing = localStorage.getItem("transitivityShowing") as undefined | T.Transitivity; const theme = localStorage.getItem("theme"); - const textOptionst = localStorage.getItem("textOptions"); + const textOptionst = localStorage.getItem(textOptionsLocalStorageName); if (regularIrregular) { setRegularIrregular(regularIrregular); } @@ -95,7 +95,7 @@ function App() { localStorage.setItem("regularIrregular", regularIrregular); localStorage.setItem("verbTypeShowing", verbTypeShowing); localStorage.setItem("transitivityShowing", transitivityShowing); - localStorage.setItem("textOptions", JSON.stringify(textOptions)); + localStorage.setItem(textOptionsLocalStorageName, JSON.stringify(textOptions)); localStorage.setItem("theme", theme); }); @@ -305,14 +305,15 @@ function App() {
Pashto Spelling
{ setTextOptions({ ...textOptions, - spelling: p as "Afghan" | "Pakistani", + spelling: p as T.Spelling, }); }} /> diff --git a/src/components/Pashto.tsx b/src/components/Pashto.tsx index b64388c..014a5d7 100644 --- a/src/components/Pashto.tsx +++ b/src/components/Pashto.tsx @@ -7,7 +7,7 @@ */ import { - convertAfToPkSpelling, + convertSpelling, } from "../lib/convert-spelling"; import { phoneticsToDiacritics @@ -23,9 +23,7 @@ const Pashto = ({ opts, children: text }: { const p = opts.diacritics ? (phoneticsToDiacritics(ps.p, ps.f) || ps.p) : ps.p; - return opts.spelling === "Afghan" - ? p - : convertAfToPkSpelling(p); + return convertSpelling(p, opts.spelling); } const style = opts.pTextSize === "normal" ? undefined diff --git a/src/lib/convert-spelling.test.ts b/src/lib/convert-spelling.test.ts index 12061e9..3669d34 100644 --- a/src/lib/convert-spelling.test.ts +++ b/src/lib/convert-spelling.test.ts @@ -7,11 +7,11 @@ */ import { - convertAfToPkSpelling, - convertPkToAfSpelling, + convertSpelling, + revertSpelling, } from "./convert-spelling"; -const pairs = [ +const pairsWPakistaniUndotted = [ ["سړی", "سړے"], ["موسیٰ", "موسیٰ"], ["فرمايي", "فرمائی"], @@ -27,14 +27,44 @@ const pairs = [ ["ضمائر", "ضمائر"], ]; -pairs.forEach((pair) => { - test(`${pair[0]} should be converted to ${pair[1]} in Pakistani spelling`, () => { - const converted = convertAfToPkSpelling(pair[0]); +const pairsWPakistaniDotted = [ + ["سړی", "سړے"], + ["موسیٰ", "موسیٰ"], + ["فرمايي", "فرمائي"], + ["چای", "چائ"], + ["زوی", "زوئ"], + ["ښويېدل", "ښوئېدل"], + ["ويي", "وئي"], + ["دوستي", "دوستي"], + ["هييت", "هييت"], + ["ښيي", "ښيي"], + ["ستاينه", "ستائينه"], + ["فرمايل", "فرمائيل"], + ["ضمائر", "ضمائر"], +]; + +pairsWPakistaniDotted.forEach((pair) => { + test(`${pair[0]} should be converted to ${pair[1]} in Pakistani ي spelling`, () => { + const converted = convertSpelling(pair[0], "Pakistani ي"); expect(converted).toBe(pair[1]); }); - - test(`${pair[1]} should be converted to ${pair[0]} in Afghan spelling`, () => { - const converted = convertPkToAfSpelling(pair[1]); - expect(converted).toBe(pair[0]); + test(`${pair[1]} should be reverted to ${pair[0]} in Pakistani ي spelling`, () => { + const reverted = revertSpelling(pair[1], "Pakistani ي"); + expect(reverted).toBe(pair[0]); + }); +}); + +pairsWPakistaniUndotted.forEach((pair) => { + test(`${pair[0]} should be converted to ${pair[1]} in Pakistani ی spelling`, () => { + const converted = convertSpelling(pair[0], "Pakistani ی"); + expect(converted).toBe(pair[1]); + }); + test(`${pair[0]} should stay the same`, () => { + const converted = convertSpelling(pair[0], "Afghan"); + expect(converted).toBe(pair[0]); + }); + test(`${pair[1]} should be reverted to ${pair[0]} in Pakistani ی spelling`, () => { + const reverted = revertSpelling(pair[1], "Pakistani ی"); + expect(reverted).toBe(pair[0]); }); }); diff --git a/src/lib/convert-spelling.ts b/src/lib/convert-spelling.ts index df75218..36130e9 100644 --- a/src/lib/convert-spelling.ts +++ b/src/lib/convert-spelling.ts @@ -6,28 +6,50 @@ * */ -export function convertAfToPkSpelling(input: string): string { - const converted = input - .replace(/ای(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائ") +import * as T from "../types"; + +/** + * takes a string of standard Afghan Pashto text and puts it into the same or a different spelling system + * + * @param input + * @param spelling + * @returns + */ +export function convertSpelling(input: string, spelling: T.Spelling): string { + if (spelling === "Afghan") { + return input; + } + return input.replace(/ای(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائ") .replace(/وی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وئ") .replace(/ی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ے") - .replace(/ي(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ی") + .replace(/ي(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, (spelling === "Pakistani ی") + ? "ی" + : "ي") .replace(/(?:ای|اي)(?=ي|ی|ې)/g, "ائ") .replace(/(?:وی|وي)(?=ي|ی|ې)/g, "وئ") .replace(/(?:ای|اي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائي") .replace(/(?:وی|وي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وئي"); - return converted; } -export function convertPkToAfSpelling(input: string): string { - const converted = input - .replace(/ی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ي") +/** + * Takes a string of a given spelling system and puts it into standardAfghan Pashto text + * + * @param input + * @param spelling + */ +export function revertSpelling(input: string, spelling: T.Spelling): string { + if (spelling === "Afghan") { + return input; + } + return input + .replace(new RegExp(`${spelling === "Pakistani ی" + ? "ی" + : "ي"}(?![\u0621-\u065f\u0670-\u06d3\u06d5])`, "g"), "ي") .replace(/ے(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ی") .replace(/ائ(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ای") .replace(/وئ(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وی") .replace(/(?:ائی|ائي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "اي") .replace(/(?:وئی|وئي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وي") .replace(/ائ(?=ي|ی|ې)/g, "اي") - .replace(/وئ(?=ي|ی|ې)/g, "وي"); - return converted; + .replace(/وئ(?=ي|ی|ې)/g, "وي");; } diff --git a/src/library.ts b/src/library.ts index 6dab1a4..e7e2beb 100644 --- a/src/library.ts +++ b/src/library.ts @@ -37,8 +37,8 @@ import { import { standardizePashto } from "./lib/standardize-pashto"; import { phoneticsToDiacritics } from "./lib/phonetics-to-diacritics"; import { - convertAfToPkSpelling, - convertPkToAfSpelling, + convertSpelling, + revertSpelling, } from "./lib/convert-spelling"; import { dictionaryEntryBooleanFields, @@ -87,8 +87,8 @@ export { makePsString, removeFVariants, standardizePashto, - convertAfToPkSpelling, - convertPkToAfSpelling, + convertSpelling, + revertSpelling, validateEntry, isNounAdjOrVerb, simplifyPhonetics, diff --git a/src/types.ts b/src/types.ts index d030f48..5ce8075 100644 --- a/src/types.ts +++ b/src/types.ts @@ -128,11 +128,13 @@ export type DictionaryEntryError = { erroneousFields: DictionaryEntryField[], } +export type Spelling = "Afghan" | "Pakistani ی" | "Pakistani ي"; + export type TextOptions = { pTextSize: "normal" | "larger" | "largest"; phonetics: "lingdocs" | "ipa" | "alalc" | "none"; dialect: "standard" | "peshawer" | "southern"; - spelling: "Afghan" | "Pakistani"; + spelling: Spelling; diacritics: boolean; }