Created
February 13, 2018 14:18
-
-
Save r2g/b40bc97459f47ea14abaf12e5ea4f2ec to your computer and use it in GitHub Desktop.
Revisions
-
r2g created this gist
Feb 13, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,273 @@ /** * Replacements dragon that needs to be slayed with the almighty sword of Sir. God * It has been slayed. Praise be to the Most High, Jehovah Jireh! My Provider. Thank You God ( Feb 13, 2018 1714 Hrs ) */ class DynamicSchema { constructor () { this.obj = { "loanGuarantor_code": { "loanGuarantor_confirm": { "loanGuarantor_process": [], "app_start": [] } }, "loanApplication": { "loanApplication_loanType": { "loanApplication_confirm": { "loanApplication_loanType": { "loanApplication_process": [], "app_start": [] } } } }, "loanRepayment": { "loanRepayment_loanType": { "loanRepayment_confirm": { "loanRepayment_process": [], "app_start": [] } } }, "app_start": [], "exit": [] // name_biller :{ // name_account :{ // name_biller:{ // abc:{ // def: [] // } // } // } // } }; this.matches = [ 'loanType' ]; this.replacements = [ ['typeA', 'typeB'], ['A1','A2'] ]; } start () { let replacementsArray = this.getKeyMatches ( this.obj, this.matches, this.replacements ); // console.log ( JSON.stringify ( replacementsArray, null, 4 ) ); this.replaceKeys ( this.obj, replacementsArray ) ; } getKeyMatches ( obj, matches, keyReplacements ) { let pendingRecursive = 1; let keys = []; let replacementsObj = []; //recursive funcion to loop through an object let loopThroughObject = ( obj ) => { //get all keys for the object var objectKeys = Object.keys ( obj ); //loop through the object objectKeys.some( ( key ) => { let currentObject = obj [ key ]; if ( currentObject && typeof currentObject === 'object') { for ( let index in matches ) { if ( index ) { let match = matches [ index ]; let replacements = keyReplacements [ index ]; //check if we find a match for our current if ( key.includes ( match ) ){ //this variable holds our new object definition let newObj = {}; for ( let replacement of replacements ) { //replace with the new name let newKeyName = key.replace ( match, replacement ); //add the new object with its new name to our new object newObj [ newKeyName ] = currentObject; } newObj.app_start = []; let rplc = { key : key, value: newObj }; // console.log ( JSON.stringify ( rplc, null, 4 ) ); replacementsObj.push ( rplc ); } } } pendingRecursive++; loopThroughObject ( currentObject ); } }); //decrement the recursive flag if (--pendingRecursive === 0){ return replacementsObj; } }; return loopThroughObject ( obj ) ; } replaceKeys ( originalObject, replacementsArray ) { let pendingRecursive = 1; let keys = []; let modifiedObj = originalObject; for ( let replacementObject of replacementsArray ) { let key = replacementObject.key; let value = replacementObject.value; let path = this.getPath ( modifiedObj, key ); this.updateObject ( modifiedObj, value, path ); } console.log ( JSON.stringify ( modifiedObj, null, 4 ) ); } getPathOld ( obj, theKey, path ) { // console.log ( JSON.stringify ( arguments,null, 4 ) ); //stop getting the path if the object passed is not an object if ( typeof obj !== 'object' ) { return; } //loop through all keys for ( var key in obj ) { //if the key exists in the object if ( obj.hasOwnProperty ( key ) ) { var t = path; var currentObject = obj[key]; //if the path is undefined if ( !path ) { path = key; } else { path = path + '.' + key; } if(key === theKey) { return path; } else if(typeof currentObject !== 'object'){ path = t; } var res = this.getPath ( currentObject, theKey, path) ; if ( res ) { return res; } } } } getPath ( obj, theKey ) { //we flatten the key to dotted seperators let flattened = this.flatten ( obj ) ; let keys = Object.keys ( flattened ) ; let pathArray = []; //we find the instance of the key for ( let key of keys ) { if ( key.includes ( theKey ) ) { let keyIndex = key.split ( '.' ).indexOf ( theKey ); let path = key.split('.', keyIndex+1).join("."); return path; } } } flatten ( data ) { var result = {}; function recurse (cur, prop) { if (Object(cur) !== cur) { result[prop] = cur; } else if (Array.isArray(cur)) { for(var i=0, l=cur.length; i<l; i++) recurse(cur[i], prop + "[" + i + "]"); if (l == 0) result[prop] = []; } else { var isEmpty = true; for (var p in cur) { isEmpty = false; recurse(cur[p], prop ? prop+"."+p : p); } if (isEmpty && prop) result[prop] = {}; } } recurse(data, ""); return result; } updateObject ( object, newValue, path ) { console.log ( 'path:', path ); var stack = path.split('.'); while(stack.length>1){ let currentVal = stack.shift(); object = object[currentVal]; } // console.log ( newValue ); let keys = Object.keys ( newValue ); for ( let key of keys ) { object [ key ] = newValue [ key ]; } delete object [stack.shift () ]; } } //init our dynamic schema let dynamicSchema = new DynamicSchema ( ); //attempt the attemptments dynamicSchema.start ( );