forked from grapheco/InteractiveGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
155 lines (155 loc) · 3.98 KB
/
Copy pathutils.js
File metadata and controls
155 lines (155 loc) · 3.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Created by bluejoe on 2018/2/6.
*/
///////////////////////////////////
class Utils {
static distinct(arr) {
var ae = [];
arr.forEach((t) => {
if (ae.indexOf(t) < 0)
ae.push(t);
});
return ae;
}
static partOf(keys, src) {
var filtered = {};
keys.forEach((key) => {
if (src[key] !== undefined) {
filtered[key] = src[key];
}
});
return filtered;
}
static flatMap(arr, func) {
var ae = [];
arr.forEach((t) => {
var r = func(t);
r.forEach((ri) => {
ae.push(ri);
});
});
return ae;
}
static toArray(it) {
var arr = [];
while (true) {
var v = it.next();
if (v.done)
break;
arr.push(v.value);
}
return arr;
}
static deepClone(value) {
if (value === undefined)
return undefined;
if (value === null)
return null;
if (typeof (value) == 'string'
|| typeof (value) == 'number'
|| typeof (value) == 'boolean'
|| value instanceof Function) {
return value;
}
if (value instanceof Array) {
var arr = value;
return arr.map((item) => {
return Utils.deepClone(item);
});
}
if (typeof (value) == 'object') {
return Utils._deepCloneObject(value);
}
throw new TypeError("unsupported type: " + typeof (value));
}
static _deepCloneObject(src) {
var dest = {};
for (let key in src) {
if (src.hasOwnProperty(key)) {
var value = src[key];
dest[key] = Utils.deepClone(value);
}
}
return dest;
}
/**
* base + delta
* @param base
* @param delta
*/
static deepExtend(base, delta) {
//do not working on base object
var dest = Utils._deepCloneObject(base);
for (let key in delta) {
if (delta.hasOwnProperty(key)) {
var baseValue = base[key];
var extValue = delta[key];
if (typeof (extValue) == 'object' &&
typeof (baseValue) == 'object') {
dest[key] = Utils.deepExtend(baseValue, extValue);
continue;
}
//base={a:{x:...}}, ext={a:2}
dest[key] = Utils.deepClone(extValue);
}
}
return dest;
}
/**
* evalate each property which is a Function(currentObject)
*/
static evaluate(o) {
for (let key in o) {
if (o[key] instanceof Function) {
var fun = o[key];
o[key] = fun(o);
}
}
}
static toMap(o) {
var m = new Map();
for (let key in o) {
if (o.hasOwnProperty(key)) {
m.set(key, o[key]);
}
}
return m;
}
}
exports.Utils = Utils;
//////////////Point/////////////////
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
delta(delta) {
this.x += delta.x;
this.y += delta.y;
}
expand(width, height) {
return new Rect(this.x - width / 2, this.y - height / 2, this.x + width / 2, this.y + height / 2);
}
}
exports.Point = Point;
//////////////Rect/////////////////
class Rect {
constructor(x1, y1, x2, y2) {
this.x1 = Math.min(x1, x2);
this.y1 = Math.min(y1, y2);
this.x2 = Math.max(x1, x2);
this.y2 = Math.max(y1, y2);
}
center() {
return new Point((this.x1 + this.x2) / 2, (this.y1 + this.y2) / 2);
}
width() {
return this.x2 - this.x1;
}
height() {
return this.y2 - this.y1;
}
}
exports.Rect = Rect;