/** * Author: Charuka Rathnayaka * Email: CharukaR@99x.io **/ import { Utility } from '../utils/Utility.js'; import {GeneralDataInjector} from './GeneralDataInjector.js'; import { LevenshteinDataInjector } from './LevenshteinDataInjector.js'; export class DataInjector{ generalDataInjector: GeneralDataInjector; levenshteinDataInjector: LevenshteinDataInjector; constructor(){ this.generalDataInjector = new GeneralDataInjector(); this.levenshteinDataInjector = new LevenshteinDataInjector(); } /** * injectData * This function injects data into the schema. * @param data The data to be injected. * @param schema The schema to inject data into. * @returns { [key: string]: any } The schema with the injected data. */ injectData(data:{ [key: string]: any }, schema: { [key: string]: any }): { [key: string]: any } { const initialSchema = JSON.parse(JSON.stringify(schema)); const { remainingDataGeneral,injectedSchemaGeneral } = this.generalDataInjector.injectData(data, schema); // console.log("general remainingData", remainingDataGeneral); // console.log("general injectedSchema", injectedSchemaGeneral); const {remainingDataLevenshtein, injectedSchemaLevenshtein} = this.levenshteinDataInjector.injectData(remainingDataGeneral, injectedSchemaGeneral); // console.log("injectedSchemaLevenshtein", injectedSchemaLevenshtein); // console.log("remainingDataLevenshtein", remainingDataLevenshtein); console.log("Data Injection - ",schema,initialSchema, injectedSchemaLevenshtein) const injectedDataAfterReplacement = this.replaceDataValues(injectedSchemaLevenshtein, initialSchema); console.log("injectedDataAfterReplacement", injectedDataAfterReplacement); return injectedSchemaLevenshtein } /** * replaceDataValues * This function replaces the data values with the most similar values in the schema. * It identifes the keys with given values (given as an array in the input schema) and replaces the data values with the most similar values in the schema. * @param data The data to be replaced. * @param schema The schema to replace the data values. * @returns { [key: string]: any } The schema with the replaced data values. */ replaceDataValues(data:{ [key: string]: any }, schema: { [key: string]: any }): { [key: string]: any } { const keysWithGivenValues = Object.keys(schema).filter(key => Array.isArray(schema[key])); if(keysWithGivenValues.length === 0){ return data; } keysWithGivenValues.forEach(key => { if(data[key]){ if(schema[key].length>0){ const mostSimilarValue = Utility.getClosestMatch(data[key], schema[key]); data[key] = mostSimilarValue; }else{ console.log("Error: No values found for the key", key); } } }); return data; } }