From 66989dee67a151a1f6ddb8b0f127b5ba4d366270 Mon Sep 17 00:00:00 2001 From: adueck Date: Wed, 25 Jan 2023 19:33:14 +0500 Subject: [PATCH] also added validation for all the variations in the optional fields --- package-lock.json | 4 +-- package.json | 2 +- src/components/package.json | 2 +- src/lib/package.json | 2 +- src/lib/src/validate-entry.test.ts | 26 ++++++++++++++ src/lib/src/validate-entry.ts | 56 +++++++++++++++++++----------- 6 files changed, 67 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0a0cc7b..25510c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pashto-inflector", - "version": "5.6.0", + "version": "5.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pashto-inflector", - "version": "5.6.0", + "version": "5.7.0", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 3b1d0dd..10e74f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pashto-inflector", - "version": "5.6.0", + "version": "5.7.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/components/package.json b/src/components/package.json index c2a6a31..32e8fae 100644 --- a/src/components/package.json +++ b/src/components/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/ps-react", - "version": "5.6.0", + "version": "5.7.0", "description": "Pashto inflector library module with React components", "main": "dist/components/library.js", "module": "dist/components/library.js", diff --git a/src/lib/package.json b/src/lib/package.json index 7a42c80..121ef71 100644 --- a/src/lib/package.json +++ b/src/lib/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/inflect", - "version": "5.6.0", + "version": "5.7.0", "description": "Pashto inflector library", "main": "dist/index.js", "types": "dist/lib/library.d.ts", diff --git a/src/lib/src/validate-entry.test.ts b/src/lib/src/validate-entry.test.ts index 8de1570..41e2702 100644 --- a/src/lib/src/validate-entry.test.ts +++ b/src/lib/src/validate-entry.test.ts @@ -253,6 +253,32 @@ const toTest: { erroneousFields: ["f"], }, }, + { + input: {"ts":1527815870,"i":183,"p":"اثر","f":"asar","g":"asar","e":"influence, impression, tracks, affect","r":4,"c":"n. m.","app":"اثرات, آثار","apf":"asráat, aasáar"}, + output: { ok: true }, + }, + { + input: {"ts":1527815870,"i":183,"p":"اثر","f":"asar","g":"asar","e":"influence, impression, tracks, affect","r":4,"c":"n. m.","app":"اثرات, آثار","apf":"asráat, aa sáar"}, + output: { + errors: ["spacing discrepency between app and apf"], + p: "اثر", + f: "asar", + e: "influence, impression, tracks, affect", + ts: 1527815870, + erroneousFields: ["app", "apf"], + }, + }, + { + input: {"ts":1527815870,"i":183,"p":"اثر","f":"asar","g":"asar","e":"influence, impression, tracks, affect","r":4,"c":"n. m.","app":"اثرات, آثار","apf":"asráat"}, + output: { + errors: ["difference in variation length between app and apf", "script and phonetics do not match for app and apf"], + p: "اثر", + f: "asar", + e: "influence, impression, tracks, affect", + ts: 1527815870, + erroneousFields: ["app", "apf"], + }, + }, ]; test("validateEntry should work", () => { diff --git a/src/lib/src/validate-entry.ts b/src/lib/src/validate-entry.ts index 7b1c6a4..59b588a 100644 --- a/src/lib/src/validate-entry.ts +++ b/src/lib/src/validate-entry.ts @@ -83,33 +83,49 @@ export function validateEntry(entry: T.DictionaryEntry): T.DictionaryEntryError erroneousFields.add(errField); return; } - if (!phoneticsToDiacritics(p, f) && !entry.diacExcept) { - errors.add(`script and phonetics do not match for ${pField} and ${fField}`); - erroneousFields.add(pField) - erroneousFields.add(fField); - } - const firstF = removeFVarients(f); - if (firstF.includes("-")) { - if (firstF.includes(" ")) { - errors.add(`presence of both hyphen and space in ${fField}`); - erroneousFields.add(fField); - } - const fWords = firstF.split("-"); - const pWords = p.split(" "); - if (fWords.length !== pWords.length) { - errors.add(`hyphen/spacing discrepency between ${pField} and ${fField}`); + if (!isRequired && p.includes(", ")) { + const pVars = p.split(", "); + const fVars = f.split(", "); + if (pVars.length !== fVars.length) { + errors.add(`difference in variation length between ${pField} and ${fField}`); erroneousFields.add(pField); erroneousFields.add(fField); } + pVars.forEach((pVar, i) => { + checkPhoneticsAndSpacing(pVar, fVars[i] || ""); + }); } else { - // check spacing - const fWords = firstF.split(" "); - const pWords = p.split(" "); - if (fWords.length !== pWords.length) { - errors.add(`spacing discrepency between ${pField} and ${fField}`); + checkPhoneticsAndSpacing(p, f); + } + function checkPhoneticsAndSpacing(p: string, f: string) { + if (!phoneticsToDiacritics(p, f) && !entry.diacExcept) { + errors.add(`script and phonetics do not match for ${pField} and ${fField}`); erroneousFields.add(pField); erroneousFields.add(fField); } + const firstF = removeFVarients(f); + if (firstF.includes("-")) { + if (firstF.includes(" ")) { + errors.add(`presence of both hyphen and space in ${fField}`); + erroneousFields.add(fField); + } + const fWords = firstF.split("-"); + const pWords = p.split(" "); + if (fWords.length !== pWords.length) { + errors.add(`hyphen/spacing discrepency between ${pField} and ${fField}`); + erroneousFields.add(pField); + erroneousFields.add(fField); + } + } else { + // check spacing + const fWords = firstF.split(" "); + const pWords = p.split(" "); + if (fWords.length !== pWords.length) { + errors.add(`spacing discrepency between ${pField} and ${fField}`); + erroneousFields.add(pField); + erroneousFields.add(fField); + } + } } }); if ((entry.separationAtP && !entry.separationAtF)) {