ObjTransmute
Transmutes ( converts ) object to another one using defined rules and config ( optional )
Usage:
const ObjTransmute = ; // ObjTransmute( {obj|Object}, {rules|Object}[, {config|Object}] ) => {Object} var obj = a: 1 b: 'Hello world!' other1: null rules = c: 'a' // 'a' - originProp => result.c = obj.a { // context: // this.obj = obj // this.rules = rules // this.config = config // this.result - result object in current state // arguments: // d = obj[ originProp ] = obj.d = undefined - obj value of same property 'd' // originProp = 'd' // obj = this.obj // prop = 'd' return objb + thisobja; } e: from: 'a1' // originProp default: 'Bye world!' // working only when get() returns undefined descriptor: enumerable: false // result.e will be nonenumerable { // arguments: // a1 = obj[ originProp ] = obj.a1 = undefined // originProp = 'a1' // obj = this.obj // prop = 'e' return a1 === undefined ? null : a1 + '!!!'; } f: from: 'a1' // originProp default: 'Bye world!' // value by default if obj.a1 === undefined g: // same as d example but on undefined returns default value default: 'default' // working only when get() returns undefined { return g && g + '!!!' || undefined } ; c: 1 d: 'Hello world!1' e: null f: 'Bye world!' g: 'default' /* ------------ Using config ------------- */ var config = otherProps: true descriptor: enumerable: false // now all options by default will be nonenumerable ; b: 'Hello world!' // not defined in rules - passes through c: 1 d: 'Hello world!1' e: null f: 'Bye world!' g: 'default' other1: null // not defined in rules - passes through /* ------------ Using Default Object ------------- */ var defaultOptions = other1: 123 ; var config = otherProps: true saveOrigin: true // define get by default { return value || defaultOptions prop } ; a: 1 // origin b: 'Hello world!' // origin c: 1 d: 'Hello world!1' e: null f: 'Bye world!' g: 'default' other1: 123 // unused value but using default get() returns value from defaultOptions /* ------------ Using Require Option ------------- */ // Require option is used when we need other property to be calculated before current propertyvar obj = a: 'Hello world' b: 1 rules = c: require: 'd' // tells that first we need to get result.d { return thisresultd + 1 } d: require: 'e' // tells that first we need to get result.e { return thisresulte * 2 } { return thisobjb + 2 } f: from: 'a' // we get value from obj.a require: 'c' 'e' // and require result.c and result.e to be already calculated { return a + thisresultc + thisresulte } ; c: 7 d: 6 e: 3 f: 'Hello world10'