Spaces:
Build error
Build error
File size: 1,429 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
const Clone = require('../../util/clone');
class Nodes {
constructor(...args) {
if (Array.isArray(args)) {
if (Array.isArray(args[0])) {
this._nodes = args;
return;
}
}
this._nodes = [];
for (let node of args) {
if (!Array.isArray(node)) node = [];
if (typeof node[0] !== 'number') node[0] = 0;
if (typeof node[1] !== 'number') node[1] = 0;
this._nodes.push([
Cast.toNumber(node[0]),
Cast.toNumber(node[1])
]);
}
}
push(node) {
this._nodes.push(node);
}
getRaw() {
return Clone.simple(this._nodes);
}
getAsObject() {
const nodes = this.getRaw();
const object = {};
let idx = 0;
for (const node of nodes) {
const key = Cast.toString(idx + 1);
object[key] = { x: node[0], y: node[1] };
idx++;
}
return object;
}
getObjects() {
const nodes = this.getRaw();
const newArray = [];
for (const node of nodes) {
newArray.push({ x: node[0], y: node[1] });
}
return newArray;
}
getCommaSeperated() {
const nodes = this.getRaw();
const flattened = nodes.flat(Infinity);
return flattened.join(',');
}
}
module.exports = Nodes; |