-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
160 lines (132 loc) · 3.48 KB
/
helpers.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
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
156
157
158
159
//==================================================================================================
_copy2DArray = function(from, to) {
for (var i = 0; i < from.length; i++) {
to.push(from[i].slice(0));
}
}
_makeFlattened2DArrayCopy = function(array) {
var flattenedcopy = [].concat.apply([], array);
return flattenedcopy;
}
function cloneObject(obj) {
var target = {};
for (var i in obj) {
if (obj.hasOwnProperty(i))
target[i] = obj[i];
}
return target;
}
function arrayContains(array, item) {
if (Array.prototype.indexOf) {
return !(array.indexOf(item) < 0);
}
else {
for (var i = 0; i < array.length; i++) {
if (array[i] === item)
return true;
}
return false;
}
}
function arrayIndexOf(array, item) {
if (Array.prototype.indexOf) {
return (array.indexOf(item));
}
else {
for (var i = 0; i < array.length; i++) {
if (array[i] === item)
return i;
}
return -1;
}
}
function replaceInArray(array, value, newValue) {
for(var i in array){
if(array[i] == value) {
array[i] = newValue;
break;
}
}
}
function removeFirstOccurrenceByValue(array, item) {
for(var i in array) {
if(array[i] == item) {
array.splice(i,1);
break;
}
}
}
function isInt(n) {
//return +n === n && !(n % 1);
return !(n % 1);
}
//-----------------------------------------------
// used for profiling code
Timer = function() {
this.startTime = undefined;
this.lastCheck = undefined;
this.start();
};
Timer.prototype = {
start: function() {
this.startTime = new Date().getTime();
this.lastCheck = this.startTime;
},
restart: function() {
this.start();
},
report: function() {
var current = new Date().getTime();
var elapsed = current - this.lastCheck;
return elapsed;
},
printSinceLast: function( msg ) {
var current = new Date().getTime();
var elapsed = current - this.lastCheck;
this.lastCheck = current;
console.log( msg + elapsed + "ms" );
},
};
//-----------------------------------------------
function stringifyObject(obj) {
return _printObjectInternal(obj, 1);
}
function printObject(obj) {
console.log( _printObjectInternal(obj, 0) );
}
function _printObjectInternal(o, level) {
if (level > 10) return "...[too deep]...";
var output = '';
if (typeof o == 'object')
{
if (Object.prototype.toString.call(o) === '[object Array]' ||
o instanceof Uint32Array)
{
output = '[';
for (var i = 0; i < o.length; i++) {
if (i > 0) output += ', ';
output += _printObjectInternal(o[i], level+1);
}
output += ']';
}
else
{
output = '{';
var idx = 0;
if (level == 0) output += '\n';
for (property in o) {
if (!o.hasOwnProperty(property)) continue;
if (level != 0 && idx != 0 )
output += ', ';
output += property + ': ' + _printObjectInternal(o[property], level+1);
if (level == 0)
output += '\n';
idx++;
}
output += '}';
}
}
else
output = ''+o;
return output;
}