relu-core
Version:
268 lines (261 loc) • 11.7 kB
JavaScript
var botbuilder = require('../../modules/botbuilder');
var fs = require('fs');
module.exports.JsonReader = function(session, data, leaf, ReturnType, save, parent) {
var results = Reader(data, leaf, ReturnType, save, parent);
return (results);
}
function Reader(data, leaf, ReturnType, save, parent) {
//salvataggio del tipo della variabile "data" nella variabile "type"
var str = [];
var type = typeof data;
var special = "@";
var temp = '';
//controllo dell'esistenza del nodo parent
if (parent) {
//controllo dell'esistenza del nodo parent all'interno di data
if (data[parent]) {
//controllo se il tipo di parent è object
if (typeof(data[parent]) === 'object' && Object.prototype.toString.call(data[parent]) != '[object Array]') {
//controllo se esiste il nodo leaf all'interno di parent
if (data[parent][leaf]) {
//controllo se il tipo di leaf è object
if (typeof(data[parent][leaf]) === 'object') {
//for che scorre tutti gli elementi all'interno di leaf
for (let j in data[parent][leaf]) {
//assegna alla variabile temp tutto quello che c'è dentro leaf aggiungendo tra gli elementi un carattere speciale
temp += data[parent][leaf][j] + special;
save.push(data[parent][leaf][j])
}
} else {
//controllo se leaf è un array
if (Object.prototype.toString.call(data[parent][leaf]) === '[object Array]') {
//for che scorre tutti gli elementi dentro l'array
for (let j in data[parent][leaf]) {
//assegna a temp tutti gli elementi dell'array con un carattere speciale fra loro
temp += j + special;
save.push(j);
}
} else {
//assegna a temp tutto il contenuto di leaf con l'aggiunta del carattere speciale
temp = data[parent][leaf] + special;
}
}
}
} else {
if (Object.prototype.toString.call(data[parent]) === '[object Array]') {
for (let i in data[parent]) {
if (data[parent][i][leaf]) {
if (typeof(data[parent][i][leaf]) === 'object') {
for (let j in data[parent][i][leaf]) {
temp += data[parent][i][leaf][j] + special;
save.push(data[parent][i][leaf][j]);
}
} else {
if (typeof(data[parent][i][leaf]) === 'string') {
temp += data[parent][i][leaf] + special;
save.push(data[parent][i][leaf]);
} else {
if (Object.prototype.toString.call(data[parent][i][leaf]) === '[object Array]') {
for (let j in data[parent][i][leaf]) {
temp += j + special;
save.push(j);
}
} else {
temp = data[parent][i][leaf] + special;
}
}
}
}
}
}
}
} else {
//se il nodo parent non esiste all'interno di data, controllo se il tipo di data è object
if (type === 'object') {
//un for che scorre data
for (let k in data) {
//richiamo la funzione ricorsivamente
temp += Reader(data[k], leaf, ReturnType, save, parent);
}
}
}
} else {
//uguale a prima solo che si prende in cosiderazione il caso in cui il parent non esiste
if (!parent) {
if (data[leaf]) {
if (Object.prototype.toString.call(data[leaf]) === '[object Array]') {
for (let j in data[leaf]) {
temp += data[leaf][j] + special;
save.push(data[leaf][j]);
}
} else {
if (typeof(data[leaf]) === 'object') {
for (let j in data[leaf]) {
temp += j + special;
save.push(j);
}
} else {
temp = data[leaf] + special;
}
}
} else {
if (type === 'object') {
for (let i in data) {
temp += Reader(data[i], leaf, ReturnType, save, parent);
}
}
}
}
}
if (ReturnType) {
if (ReturnType == 'array') {
//si mettono nell'array le vari parti di temp separate dal simbolo speciale e si converte in stringa
return save;
} else {
if (ReturnType == 'string') {
str = temp.split(special).toString();
//si restituisce l'array
return (str);
}
}
}
};
module.exports.addJsonNode = function(session, json, filename, node, object, text, parent) {
add(json, filename, node, object, text);
}
function add(json, filename, node, object, text, parent) {
var type = typeof json;
var arr = [];
if (parent) {
if (json[parent]) {
if (typeof(json[parent]) === 'object') {
if (json[parent][node]) {
if (typeof(json[parent][node]) === 'object') {
json[parent][node][object] = text;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
} else {
if (typeof(json[parent][node]) === 'string') {
json[parent][node] = text;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
} else {
if (Object.prototype.toString.call(json[parent][node]) === '[object Array]') {
for (let j in json[parent][node]) {
arr.push(json[parent][node][i]);
}
arr.push(text);
json[parent][node] = arr;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
}
}
}
}
}
} else {
if (type === 'object') {
for (let k in json) {
add(json[k], filename, node, object, text, parent);
}
}
}
} else {
if (!parent) {
if (json[node]) {
if (typeof(json[node]) === 'object') {
json[node][object] = text;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
} else {
if (typeof(json[node]) === 'string') {
json[node] = text;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
} else {
if (Object.prototype.toString.call(json[node]) === '[object Array]') {
for (let j in json[node]) {
arr.push(json[node][i]);
}
arr.push(text);
json[node] = arr;
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
}
}
}
} else {
if (type === 'object') {
for (let k in json) {
add(json[k], filename, node, object, text, parent);
}
}
}
}
}
}
module.exports.deleteJsonNode = function(session, json, filename, node, parent) {
Delete(json, filename, node, parent);
}
function Delete(json, filename, node, parent) {
var type = typeof json;
if (parent) {
if (json[parent]) {
if (typeof(json[parent]) === 'object') {
if (json[parent][node]) {
if (typeof(json[parent][node]) === "object" || typeof(json[parent][node]) === "string" || Object.prototype.toString.call(json[parent][node]) === '[object Array]') {
delete(json[parent][node]);
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
}
}
}
} else {
if (type === 'object') {
for (let i in json) {
Delete(json[i], filename, node, parent);
}
}
}
} else {
if (!parent) {
if (json[node]) {
if (typeof(json[node]) === "object" || typeof(json[node]) === "string" || Object.prototype.toString.call(json[node]) === '[object Array]') {
delete(json[node]);
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
console.log("error");
}
})
}
} else {
if (type === 'object') {
for (let i in json) {
Delete(json[i], filename, node, parent);
}
}
}
}
}
}