@ultipa-graph/ultipa-node-sdk
Version:
NodeJS SDK for ultipa-server 4.0
240 lines • 7.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printAttrStruct = exports.printAttr = exports.printAttrObj = exports.printAttrList = exports.printAnyArray = void 0;
const chalk_1 = __importDefault(require("chalk"));
const utils_1 = require("../utils");
const types_1 = require("../types");
const alias_1 = require("./alias");
const node_1 = require("./node");
const edge_1 = require("./edge");
const path_1 = require("./path");
function printAnyArray(datas) {
if (datas === null) {
console.log(null);
return;
}
console.log(datas);
}
exports.printAnyArray = printAnyArray;
function printAttrList(attr, datas) {
if (datas === null) {
console.log(null);
return;
}
if ((datas === null || datas === void 0 ? void 0 : datas.length) == 0) {
console.log(datas);
return;
}
printAnyArray(datas);
// if (attr.has_attr_data) {
// datas.forEach(d => {
// if (attr.type && d.nodes) {
// } else {
// }
// })
// }
}
exports.printAttrList = printAttrList;
function printAttrObj(attr) {
let values = attr === null || attr === void 0 ? void 0 : attr.values;
console.log(`ResultType: ${chalk_1.default.yellow(attr.type_desc)}`);
switch (attr.type) {
case types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR:
printAttrList(attr, values);
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_PATH:
values.forEach((paths, index) => {
let infos = (0, path_1.getPathsPrintInfo)(paths);
console.log(`Index: ${chalk_1.default.yellow(`${index}`)}`);
console.table(infos);
});
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_EDGE:
values.forEach((edges, index) => {
let infos = (0, edge_1.getEdgesPrintInfo)(edges, { onlyRows: true });
console.log(`Index: ${chalk_1.default.yellow(`${index}`)}`);
console.table(infos);
});
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_NODE:
values.forEach((nodes, index) => {
let infos = (0, node_1.getNodesPrintInfo)(nodes, { onlyRows: true });
console.log(`Index: ${chalk_1.default.yellow(`${index}`)}`);
console.table(infos);
});
break;
}
}
exports.printAttrObj = printAttrObj;
function printAttr(dataItem) {
if (!dataItem) {
return;
}
(0, alias_1.printAlias)(dataItem);
let attr = dataItem.asAttrs();
printAttrObj(attr);
}
exports.printAttr = printAttr;
class AttrPrint {
constructor(init) {
this.tags = init.tags;
this.header = init.header;
this.rows = init.rows;
this.prints = init.prints;
}
static log(prints) {
if (!Array.isArray(prints)) {
prints = [prints];
}
prints.forEach(p => {
var _a;
let { tags } = p;
console.log(`${tags.isEmpty() ? "" : `${chalk_1.default.green(tags.desc())} `}${p.header}`);
console.table(p.rows);
if (((_a = p.prints) === null || _a === void 0 ? void 0 : _a.length) > 0) {
AttrPrint.log(p.prints);
}
});
}
}
class AttrTags {
constructor() {
this.tags = [];
}
appendChild() {
this.tags.push(0);
}
increase() {
this.tags[this.tags.length - 1] += 1;
}
clone() {
let one = new AttrTags();
one.tags = [...this.tags];
return one;
}
isEmpty() {
var _a;
return ((_a = this.tags) === null || _a === void 0 ? void 0 : _a.length) == 0;
}
desc() {
return `Tag[${this.tags.join("_")}]`;
}
}
let attrIsList = (attr) => {
return (attr === null || attr === void 0 ? void 0 : attr.type) == types_1.ULTIPA.PropertyType.PROPERTY_LIST;
};
let attrInfo = (attr, params) => {
let isList = attrIsList(attr);
let rows = [];
let newTags = params.tags.clone();
newTags.appendChild();
let prints = [];
if (isList) {
attr.values.forEach(v => {
let vListData = v;
if (!v.type) {
if (Array.isArray(v)) {
rows.push(JSON.stringify(v));
}
else {
rows.push(v);
}
// rows.push(JSON.stringify(v))
// v = v || []
// v.forEach(vi => {
// if (Array.isArray(vi)) {
// rows.push(JSON.stringify(vi))
// } else {
// rows.push(vi)
// }
// })
return;
}
let result_type = types_1.ULTIPA.ResultType[v.type];
// rows.push({
// "*result_type": result_type
// })
switch (v.type) {
case types_1.ULTIPA.ResultType.RESULT_TYPE_PATH:
let paths = (0, path_1.getPathsPrintInfo)(vListData.paths);
rows.push(...paths);
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_EDGE:
let edges = (0, edge_1.getEdgesPrintInfo)(vListData.edges, { onlyRows: true });
rows.push(...edges);
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_NODE:
let nodes = (0, node_1.getNodesPrintInfo)(vListData.nodes, { onlyRows: true });
rows.push(...nodes);
break;
case types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR:
vListData.attrs.forEach((attr) => {
if (attrIsList(attr)) {
newTags.increase();
}
let result = printAttrStruct(attr, v.type, {
returnJson: true,
tags: newTags.clone(),
});
if (result) {
rows.push(result.json);
prints.push(...result.prints);
}
else {
rows.push(result);
}
});
break;
}
});
}
else {
rows.push(attr.values);
}
return new AttrPrint({
tags: params.tags,
header: `Result Type: ${chalk_1.default.yellow(types_1.ULTIPA.ResultType[params.result_type])} Property Type: ${chalk_1.default.yellow(utils_1.PropertyUtils.propertyGet(attr.type))}`,
rows,
prints,
});
};
function printAttrStruct(attr, result_type, params) {
if (!attr) {
return null;
}
let prints = [];
if (!params) {
params = {
tags: new AttrTags(),
};
}
let isList = attrIsList(attr);
if (!params.returnJson) {
let v = attrInfo(attr, {
result_type,
tags: params.tags,
});
prints.push(v);
AttrPrint.log(prints);
return;
}
let values = attr.values;
if (isList) {
let t = `See: ${params.tags.desc()}`;
values = t;
let v = attrInfo(attr, {
result_type,
tags: params.tags,
});
prints.push(v);
}
return {
json: Object.assign(Object.assign({}, attr), { values: values }),
prints,
};
}
exports.printAttrStruct = printAttrStruct;
//# sourceMappingURL=array.aio.js.map