forked from ongardie/raftscope
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.js
More file actions
148 lines (123 loc) · 3.62 KB
/
util.js
File metadata and controls
148 lines (123 loc) · 3.62 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
/* jshint globalstrict: true */
/* jshint browser: true */
/* jshint devel: true */
/* jshint jquery: true */
'use strict';
var util = {};
// Really big number. Infinity is problematic because
// JSON.stringify(Infinity) returns 'null'.
util.Inf = 1e300;
util.value = function (v) {
return function () {
return v;
};
};
util.randomBetween = function (min, max) {
return Math.random() * (max - min) + min;
};
// Use with sort for numbers.
util.numericCompare = function (a, b) {
return a - b;
};
util.circleCoord = function (frac, cx, cy, r) {
var radians = 2 * Math.PI * (0.75 + frac);
return {
x: cx + r * Math.cos(radians),
y: cy + r * Math.sin(radians),
};
};
util.countTrue = function (bools) {
var count = 0;
bools.forEach(function (b) {
if (b)
count += 1;
});
return count;
};
util.makeMap = function (keys, value) {
var m = {};
keys.forEach(function (key) {
m[key] = value;
});
return m;
};
util.mapValues = function (m) {
return $.map(m, function (v) {
return v;
});
};
util.clone = function (object) {
return jQuery.extend(true, {}, object);
};
// From http://stackoverflow.com/a/6713782
util.equals = function (x, y) {
if (x === y) return true;
// if both x and y are null or undefined and exactly the same
if (!( x instanceof Object ) || !( y instanceof Object )) return false;
// if they are not strictly equal, they both need to be Objects
if (x.constructor !== y.constructor) return false;
// they must have the exact same prototype chain, the closest we can do is
// test there constructor.
var p;
for (p in x) {
if (!x.hasOwnProperty(p)) continue;
// other properties were tested using x.constructor === y.constructor
if (!y.hasOwnProperty(p)) return false;
// allows to compare x[ p ] and y[ p ] when set to undefined
if (x[p] === y[p]) continue;
// if they have the same strict value or identity then they are equal
if (typeof( x[p] ) !== "object") return false;
// Numbers, Strings, Functions, Booleans must be strictly equal
if (!util.equals(x[p], y[p])) return false;
// Objects and Arrays must be tested recursively
}
for (p in y) {
if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false;
// allows x[ p ] to be set to undefined
}
return true;
};
// Transforms the simulation speed from a linear slider
// to a logarithmically scaling time factor.
util.speedSliderTransform = function (v) {
v = Math.pow(10, v);
if (v < 1)
return 1;
else
return v;
};
util.greatestLower = function (a, gt) {
var bs = function (low, high) {
if (high < low)
return low - 1;
var mid = Math.floor((low + high) / 2);
if (gt(a[mid]))
return bs(low, mid - 1);
else
return bs(mid + 1, high);
};
return bs(0, a.length - 1);
};
util.clamp = function (value, low, high) {
if (value < low)
return low;
if (value > high)
return high;
return value;
};
util.relativeTime = function (time, now) {
if (time == util.Inf)
return 'infinity';
var sign = time > now ? '+' : '';
return sign + ((time - now) / 1e3).toFixed(3) + 'ms';
};
util.getButton = function (label) {
return $('<button type="button" class="btn btn-default"></button>').text(label);
};
util.srvArraySub = function(a, b) {
var base = {};
b.forEach(function(srv){base[srv.id]=true;});
return a.filter(function(srv){
return base[srv.id] === undefined;
});
};