type enforcement
This commit is contained in:
@@ -15,6 +15,18 @@ const utils = require('./utils');
|
||||
* @property {string|undefined} direction
|
||||
*/
|
||||
|
||||
const NODE_DEF = {
|
||||
'name': '!string',
|
||||
'x': 'number',
|
||||
'y': 'number'
|
||||
};
|
||||
|
||||
const LINK_DEF = {
|
||||
'from': '!string',
|
||||
'to': '!string',
|
||||
'direction': 'string'
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
'max-link-length': 3,
|
||||
'diagonals': true,
|
||||
@@ -307,6 +319,19 @@ module.exports = (options) => {
|
||||
* @returns {Object<string,Node1>|null}
|
||||
*/
|
||||
compute: (nodes, links) => {
|
||||
|
||||
Object.keys(nodes).forEach(key => {
|
||||
const res = utils.isValid(nodes[key], NODE_DEF);
|
||||
if (res)
|
||||
throw `node '${key}' is invalid at key ${res}`;
|
||||
});
|
||||
|
||||
links.forEach((link, i) => {
|
||||
const res = utils.isValid(link, LINK_DEF);
|
||||
if (res)
|
||||
throw `link ${i} (${link.from}->${link.to}) is invalid at key ${res}`;
|
||||
});
|
||||
|
||||
Object.values(nodes).forEach(node => {
|
||||
node.const = {
|
||||
beforeX: [],
|
||||
|
||||
@@ -23,6 +23,40 @@ try {
|
||||
* @property {string|undefined} type
|
||||
*/
|
||||
|
||||
const SUB_DEF = {
|
||||
'text': 'string',
|
||||
'icon': 'string',
|
||||
'color': 'string',
|
||||
'font': 'string',
|
||||
'font-size': 'number',
|
||||
'font-style': 'string',
|
||||
'scale': 'number',
|
||||
};
|
||||
|
||||
const NODE_DEF = {
|
||||
'name': '!string',
|
||||
'icon': '!string',
|
||||
'x': '!number',
|
||||
'y': '!number',
|
||||
'color': 'string',
|
||||
'scale': 'number',
|
||||
'top': SUB_DEF,
|
||||
'bottom': SUB_DEF,
|
||||
'left': SUB_DEF,
|
||||
'right': SUB_DEF
|
||||
};
|
||||
|
||||
const LINK_DEF = {
|
||||
'from': '!string',
|
||||
'to': '!string',
|
||||
'type': 'string',
|
||||
'color': 'string',
|
||||
'scale': 'number',
|
||||
'size': 'number',
|
||||
'top': SUB_DEF,
|
||||
'bottom': SUB_DEF,
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
'beautify': false,
|
||||
'scale': 128,
|
||||
@@ -230,8 +264,24 @@ module.exports = (options) => {
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Object<string,Node2>} nodes
|
||||
* @param {Link2[]} links
|
||||
*/
|
||||
compute: (nodes, links) => {
|
||||
|
||||
Object.keys(nodes).forEach(key => {
|
||||
const res = utils.isValid(nodes[key], NODE_DEF);
|
||||
if (res)
|
||||
throw `node '${key}' is invalid at key ${res}`;
|
||||
});
|
||||
|
||||
links.forEach((link, i) => {
|
||||
const res = utils.isValid(link, LINK_DEF);
|
||||
if (res)
|
||||
throw `link ${i} (${link.from}->${link.to}) is invalid at key ${res}`;
|
||||
});
|
||||
|
||||
const bounds = self.getBounds(nodes);
|
||||
|
||||
const data = {'g': []};
|
||||
|
||||
@@ -21,6 +21,40 @@ const self = {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Verify if an object respect it's definition
|
||||
* @param obj
|
||||
* @param def
|
||||
* @returns {null|string}
|
||||
*/
|
||||
isValid: (obj, def) => {
|
||||
const keys = Object.keys(def);
|
||||
let key;
|
||||
let type;
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
key = keys[i];
|
||||
type = (typeof obj !== 'object' || obj[key] === undefined || obj[key] === null) ? null : typeof obj[key];
|
||||
if (type === 'object' && obj[key].length > 0)
|
||||
type = 'array';
|
||||
if (typeof def[key] === 'object') {
|
||||
if (type && type !== 'object')
|
||||
return key;
|
||||
const res = self.isValid(type ? obj[key] : undefined, def[key]);
|
||||
if (res)
|
||||
return key + '.' + res;
|
||||
} else {
|
||||
if (def[key][0] === '!') {
|
||||
def[key] = def[key].substr(1);
|
||||
if (!type)
|
||||
return key;
|
||||
}
|
||||
if (type && type !== def[key])
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Clone any JS variable or object
|
||||
* @param {*} arg
|
||||
|
||||
Reference in New Issue
Block a user