utils in another file
This commit is contained in:
+1
-29
@@ -1,38 +1,10 @@
|
||||
const placing = require('./placing');
|
||||
const rendering = require('./rendering');
|
||||
|
||||
/**
|
||||
* Merge resources by reading object keys and keeping reference value only if it's type is different from the source
|
||||
* @param ref - reference object/value
|
||||
* @param src - source object/value
|
||||
* @returns {*}
|
||||
*/
|
||||
const merge = (ref, src) => {
|
||||
if (typeof ref !== typeof src) {
|
||||
return ref;
|
||||
} else if (ref.length && !src.length) {
|
||||
return ref;
|
||||
} else if (ref.length && src.length) {
|
||||
return src;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
return src;
|
||||
}
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
'placing': placing().defaultOptions,
|
||||
'rendering': rendering().defaultOptions
|
||||
};
|
||||
|
||||
|
||||
const self = {
|
||||
options: DEFAULT_OPTIONS,
|
||||
compute: (data) => {
|
||||
const options = merge(DEFAULT_OPTIONS, data['options']);
|
||||
const options = data['options'] || {};
|
||||
|
||||
let nodes = {};
|
||||
const nodeList = (data['nodes'] || []).filter(n => typeof n.name === 'string');
|
||||
|
||||
+6
-6
@@ -1,5 +1,4 @@
|
||||
const ezclone = (a) => JSON.parse(JSON.stringify(a));
|
||||
const newmap = (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill(fill));
|
||||
const utils = require('./utils');
|
||||
|
||||
/**
|
||||
* @typedef Node1
|
||||
@@ -21,10 +20,11 @@ const DEFAULT_OPTIONS = {
|
||||
'diagonals': true,
|
||||
};
|
||||
|
||||
module.exports = (options = DEFAULT_OPTIONS) => {
|
||||
module.exports = (options) => {
|
||||
|
||||
options = utils.merge(DEFAULT_OPTIONS, options);
|
||||
|
||||
const self = {
|
||||
defaultOptions: DEFAULT_OPTIONS,
|
||||
|
||||
/**
|
||||
* Get the current bounds of the graph of nodes
|
||||
@@ -55,7 +55,7 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
||||
*/
|
||||
getNewPos: (nodes) => {
|
||||
const b = self.getBounds(nodes);
|
||||
const map = newmap(b.w, b.h, false);
|
||||
const map = utils.newMap(b.w, b.h, false);
|
||||
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
||||
list.forEach(n => {
|
||||
map[n.x - b.x][n.y - b.y] = true;
|
||||
@@ -250,7 +250,7 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
||||
const free = [];
|
||||
|
||||
const tryPos = (key, x, y) => {
|
||||
const nodes2 = ezclone(nodes);
|
||||
const nodes2 = utils.ezClone(nodes);
|
||||
nodes2[key].x = x;
|
||||
nodes2[key].y = y;
|
||||
return self.applyLinks(nodes2, links, depth + 1);
|
||||
|
||||
+5
-2
@@ -1,4 +1,5 @@
|
||||
const convert = require('xml-js');
|
||||
const utils = require('./utils');
|
||||
|
||||
let list = {};
|
||||
try {
|
||||
@@ -39,9 +40,11 @@ const DEFAULT_OPTIONS = {
|
||||
const DEFAULT_SCALE = 0.4;
|
||||
const LINK_MARGIN = (1 - DEFAULT_SCALE) / 2;
|
||||
|
||||
module.exports = (options = DEFAULT_OPTIONS) => {
|
||||
module.exports = (options) => {
|
||||
|
||||
options = utils.merge(DEFAULT_OPTIONS, options);
|
||||
|
||||
const self = {
|
||||
defaultOptions: DEFAULT_OPTIONS,
|
||||
|
||||
/**
|
||||
* Find icon data from given name
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
const self = {
|
||||
/**
|
||||
* Merge resources by reading object keys and keeping reference value only if it's type is different from the source
|
||||
* @param ref - reference object/value
|
||||
* @param src - source object/value
|
||||
* @returns {*}
|
||||
*/
|
||||
merge: (ref, src) => {
|
||||
if (typeof ref !== typeof src) {
|
||||
return ref;
|
||||
} else if (ref.length && !src.length) {
|
||||
return ref;
|
||||
} else if (ref.length && src.length) {
|
||||
return src;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach((key) => out[key] = self.merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
return src;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Clone any JS variable or object
|
||||
* @param {*} arg
|
||||
* @returns {any}
|
||||
*/
|
||||
ezClone: (arg) => JSON.parse(JSON.stringify(arg)),
|
||||
|
||||
/**
|
||||
* Create a new map of the defined bounds and filling
|
||||
* @param {number} w
|
||||
* @param {number} h
|
||||
* @param {*} fill
|
||||
* @returns {any[][]}
|
||||
*/
|
||||
newMap: (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill(fill))
|
||||
};
|
||||
|
||||
module.exports = self;
|
||||
Reference in New Issue
Block a user