-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathutilities.js
75 lines (59 loc) · 1.9 KB
/
utilities.js
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
/**
* author: Nils Gehlenborg - nils@hms.harvard.edu
*/
var Utilities = function() {
};
Utilities.generateUuid = function() {
// see broofa's answer in http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
return ( 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16) ;
}) );
};
Utilities.truncate = function(textElement, w) {
var too_large = true;
if(textElement[0][0].getBBox().width<w)
too_large = false;
while(too_large) {
var bbox = textElement[0][0].getBBox();
var width = bbox.width;
var height = bbox.height;
textElement.text(textElement.text().substring(0, textElement.text().length-1));
if(textElement[0][0].getBBox().width<w)
too_large = false;
}
return textElement.text();
}
// attach the .compare method to Array's prototype to call it on any array
Array.prototype.compare = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].compare(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
//
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};