macoolka-type-model
Version:
`macoolka-type-model` is a library for define model in TypeScript. It easily build a type contain field and method to your Application. It provide a generation model for type and validition
125 lines • 5.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildGraph = exports.IgnoreNames = exports.createVis = void 0;
/**
* @file
*/
var graph_1 = require("macoolka-algorithms/lib/graph");
var predicate_1 = require("./predicate");
var A = __importStar(require("fp-ts/Array"));
var pipeable_1 = require("fp-ts/pipeable");
var helper_1 = require("./helper");
var O = __importStar(require("fp-ts/Option"));
var Module_1 = require("./Module");
var E = __importStar(require("fp-ts/Either"));
/**
* Build Vis from Graph
* @desczh
* 用Graph建立Vis
* @since 0.2.0
*
*/
var createVis = function (a) { return ({
nodes: (0, pipeable_1.pipe)(a.getAllVertices(), A.map(function (a) { return ({ id: a.getKey(), label: a.toString() }); })),
edges: (0, pipeable_1.pipe)(a.getAllEdges(), A.map(function (a) { return ({
from: a.startVertex.getKey(),
to: a.endVertex.getKey()
}); }))
}); };
exports.createVis = createVis;
exports.IgnoreNames = ['NonEmptyArray', 'MaybeArray', 'object', 'Date', 'Object'];
/**
* Build Graph with MModule
* @desczh
* 用MModule建立Graph
* @since 0.2.0
*/
var buildGraph = function (ignoreNames) { return function (as) {
ignoreNames = __spreadArray(__spreadArray([], exports.IgnoreNames, true), ignoreNames, true);
var graphVertexs = [];
var graphEdges = [];
var errors = [];
var addVertex = function (name) { return graphVertexs.map(function (a) { return a.getKey(); }).includes(name)
? errors.push((0, Module_1.typeNameRepeat)({ model: as.name, name: name }))
: graphVertexs.push(new graph_1.GraphVertex(name)); };
var addGraphEdge = function (from, to) {
if (exports.IgnoreNames.includes(from) || exports.IgnoreNames.includes(to)) {
return;
}
var fromOption = (0, pipeable_1.pipe)(graphVertexs, A.findFirst(function (a) { return a.value === from; }));
var toOption = (0, pipeable_1.pipe)(graphVertexs, A.findFirst(function (a) { return a.value === to; }));
(0, pipeable_1.pipe)(fromOption, O.map(function (a) {
(0, pipeable_1.pipe)(toOption, O.map(function (b) {
var edge = new graph_1.GraphEdge(a, b);
(0, pipeable_1.pipe)(graphEdges.every(function (a) { return a.getKey() !== edge.getKey(); }), function (value) { return value ? graphEdges.push(edge) : void 0; });
}), O.getOrElse(function () {
if (!ignoreNames.includes(to))
errors.push((0, Module_1.typeNotFound)({ model: as.name, name: to }));
}));
}), O.getOrElse(function () {
if (!ignoreNames.includes(from))
errors.push((0, Module_1.typeNotFound)({ model: as.name, name: from }));
}));
};
(0, helper_1.foreach)(as, {
model: function (model, schema) {
addVertex(model.name);
},
typealiases: function (a, schema) {
addVertex(a.name);
}
});
(0, helper_1.foreach)(as, {
type: function (type, field, model, schema) {
(0, pipeable_1.pipe)(type, O.fromPredicate(predicate_1.isTypeScalar), O.map(function (a) {
return addGraphEdge(model.name, a.value);
}));
(0, pipeable_1.pipe)(type, O.fromPredicate(predicate_1.isTypeUnionScalar), O.map(function (a) {
return (0, pipeable_1.pipe)(a.values, A.map(function (type) { return addGraphEdge(model.name, type); }));
}));
},
implement: function (name, model, schema) {
addGraphEdge(model.name, name);
}
});
return (0, pipeable_1.pipe)(errors, E.fromPredicate(function (a) { return a.length === 0; }, function () { return errors.join('\n'); }), E.map(function () {
var graph = new graph_1.Graph(true);
graphEdges.map(function (a) { return graph.addEdge(a); });
graphVertexs.map(function (a) { return graph.addVertex(a); });
return graph;
}));
}; };
exports.buildGraph = buildGraph;
//# sourceMappingURL=graph.js.map