| angular.module('xos.UITutorial') |
| .service('codeToString', function(){ |
| this.toString = code => { |
| if(angular.isArray(code)){ |
| return code.map(item => this.toString(item)); |
| else if(angular.isObject(code)){ |
| Object.keys(code).forEach(key => { |
| tmp[key] = this.toString(code[key]) |
| return code.toString().split('\n').join('').replace(/ +(?= )/gmi, ''); |
| this.toCode = string => { |
| code = JSON.parse(string); |
| if(angular.isArray(code)){ |
| return code.map(item => this.toCode(item)); |
| else if(angular.isObject(code)){ |
| Object.keys(code).forEach(key => { |
| tmp[key] = this.toCode(code[key]) |
| if(!angular.isNumber(code) && code.indexOf('function') !== -1){ |
| // create a closure to host our arguments |
| var func = new Function(`return ${code}`); |
| // invoke the original function passing arguments |
| // in this case it is a string |
| else if(Number.isNaN(code)){ |