2018-08-27 21:55:14 +00:00
|
|
|
import * as plugins from './smartscaf.plugins';
|
2017-05-05 22:47:27 +00:00
|
|
|
|
2017-05-25 16:32:53 +00:00
|
|
|
/**
|
|
|
|
* adds a variable in string dot notation to an already more or less expanded object
|
|
|
|
*/
|
|
|
|
export let deepAddToObject = async (objectArg, varStringArg: string, valueArg: string) => {
|
2018-08-27 21:55:14 +00:00
|
|
|
let varNamesArray = varStringArg.split('.');
|
|
|
|
let referencePointer = objectArg;
|
2017-05-25 16:32:53 +00:00
|
|
|
for (let i = 0; i !== varNamesArray.length; i++) {
|
2018-08-27 21:55:14 +00:00
|
|
|
let varName = varNamesArray[i];
|
2017-05-25 16:32:53 +00:00
|
|
|
|
|
|
|
// is there a next variable ?
|
|
|
|
let varNameNext: string = (() => {
|
|
|
|
if (varNamesArray[i + 1]) {
|
2018-08-27 21:55:14 +00:00
|
|
|
return varNamesArray[i + 1];
|
2017-05-25 16:32:53 +00:00
|
|
|
}
|
2018-08-27 21:55:14 +00:00
|
|
|
return null;
|
|
|
|
})();
|
2017-05-25 16:32:53 +00:00
|
|
|
|
|
|
|
// build the tree in suppliedVariables
|
|
|
|
if (!referencePointer[varName] && !varNameNext) {
|
2018-08-27 21:55:14 +00:00
|
|
|
referencePointer[varName] = valueArg;
|
|
|
|
referencePointer = null;
|
2017-05-25 16:32:53 +00:00
|
|
|
} else if (!referencePointer[varName] && varNameNext) {
|
2018-08-27 21:55:14 +00:00
|
|
|
referencePointer[varName] = {};
|
|
|
|
referencePointer = referencePointer[varName];
|
2017-05-27 01:21:55 +00:00
|
|
|
} else if (referencePointer[varName] && varNameNext) {
|
2018-08-27 21:55:14 +00:00
|
|
|
referencePointer = referencePointer[varName];
|
2017-05-27 01:21:55 +00:00
|
|
|
} else {
|
2018-08-27 21:55:14 +00:00
|
|
|
throw new Error('Something is strange!');
|
2017-05-25 16:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 21:55:14 +00:00
|
|
|
};
|