From 26f226a7fac788fd23052df59eef852e5b03479a Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Thu, 16 Sep 2021 13:52:52 -0400 Subject: [PATCH] validate entry function --- package.json | 2 +- src/lib/fields.ts | 3 ++- src/lib/validate-entry.test.ts | 8 +++++++- src/lib/validate-entry.ts | 15 +++++++++++++++ src/library.ts | 12 ++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0dba303..224f0bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "1.0.3", + "version": "1.0.4", "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/fields.ts b/src/lib/fields.ts index db7d398..14e6d8c 100644 --- a/src/lib/fields.ts +++ b/src/lib/fields.ts @@ -26,9 +26,10 @@ export const dictionaryEntryTextFields: T.DictionaryEntryTextField[] = [ "ec", "ep", ]; + export const dictionaryEntryBooleanFields: T.DictionaryEntryBooleanField[] = [ "noInf", "shortIntrans", "noOo", "sepOo", "diacExcept", -] +]; export const dictionaryEntryNumberFields: T.DictionaryEntryNumberField[] = [ "ts", "i", "l", "separationAtP", "separationAtF", ]; \ No newline at end of file diff --git a/src/lib/validate-entry.test.ts b/src/lib/validate-entry.test.ts index b267cd7..949d5a3 100644 --- a/src/lib/validate-entry.test.ts +++ b/src/lib/validate-entry.test.ts @@ -6,8 +6,9 @@ * */ -import { validateEntry } from "./validate-entry"; +import { standardizeEntry, validateEntry } from "./validate-entry"; import * as T from "../types"; +import { standardizePhonetics } from "./standardize-pashto"; const toTest: { input: any, @@ -159,3 +160,8 @@ test("validateEntry should work", () => { expect(validateEntry(t.input as T.DictionaryEntry)).toEqual(t.output); }); }); + +test("standardizeEntry", () => { + expect(standardizeEntry({"i":195,"ts":1527822036,"p":"اجتماعي","f":"ijtimaa‘ee, ijtimaayee","g":"ijtimaaee,ijtimaayee","e":"public, social, societal","c":"adj."})) + .toEqual({"i":195,"ts":1527822036,"p":"اجتماعي","f":"ijtimaa'ee, ijtimaayee","g":"ijtimaaee,ijtimaayee","e":"public, social, societal","c":"adj."}); +}); diff --git a/src/lib/validate-entry.ts b/src/lib/validate-entry.ts index 48ca953..55202a6 100644 --- a/src/lib/validate-entry.ts +++ b/src/lib/validate-entry.ts @@ -10,6 +10,7 @@ import * as T from "../types"; import { phoneticsToDiacritics, } from "./phonetics-to-diacritics"; +import { standardizePashto, standardizePhonetics } from "./standardize-pashto"; const textFieldPairs: [T.DictionaryEntryTextField, T.DictionaryEntryTextField][] = [ ["p", "f"], @@ -28,6 +29,20 @@ const requiredFields: T.DictionaryEntryField[] = [ "ts", "i", "p", "f", "e", ]; +export function standardizeEntry(entry: T.DictionaryEntry): T.DictionaryEntry { + return textFieldPairs.reduce((e, pair) => { + return { + ...e, + ...entry[pair[0]] ? { + [pair[0]]: standardizePashto(entry[pair[0]] as string), + } : {}, + ...entry[pair[1]] ? { + [pair[1]]: standardizePhonetics(entry[pair[1]] as string), + } : {}, + }; + }, { ...entry }); +} + export function validateEntry(entry: T.DictionaryEntry): T.DictionaryEntryError | { ok: true, } | { diff --git a/src/library.ts b/src/library.ts index 934e8b4..41ba89f 100644 --- a/src/library.ts +++ b/src/library.ts @@ -36,7 +36,10 @@ import { isUnisexSet, isInflectionSet, } from "./lib/p-text-helpers"; -import { standardizePashto } from "./lib/standardize-pashto"; +import { + standardizePashto, + standardizePhonetics, +} from "./lib/standardize-pashto"; import { phoneticsToDiacritics } from "./lib/phonetics-to-diacritics"; import { convertSpelling, @@ -48,7 +51,8 @@ import { dictionaryEntryTextFields, } from "./lib/fields"; import { - validateEntry + validateEntry, + standardizeEntry, } from "./lib/validate-entry"; import { readDictionary, @@ -93,9 +97,11 @@ export { makePsString, removeFVarients, standardizePashto, + standardizePhonetics, convertSpelling, revertSpelling, validateEntry, + standardizeEntry, isNounAdjOrVerb, simplifyPhonetics, phoneticsToDiacritics, @@ -136,6 +142,8 @@ export { dictionaryEntryTextFields, dictionaryEntryNumberFields, dictionaryEntryBooleanFields, + dictionaryEntryPhoneticsTextFields, + dictionaryEntryPashtoTextFields, // TYPES Types, } \ No newline at end of file