-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.js
110 lines (103 loc) · 2.67 KB
/
util.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
"use strict";
Array.prototype.contains = function(x) { return this.indexOf(x) != -1; };
Array.prototype.pluck = function(prop) { return this.map(function(x) { return x[prop]; }); };
Array.prototype.forAdjacentPairs = function(callback, thisPtr) {
var l = this.length;
for (var i = 0, j = 1; j < l; i = j++) {
var ti = this[i], tj = this[j];
if(ti !== undefined && tj !== undefined)
callback.call(thisPtr, ti, tj, i, j, this);
}
};
Array.prototype.forEveryPair = function(callback, thisPtr) {
var l = this.length;
for(var i = 0; i < l; i++) {
for(var j = i + 1; j < l; j++) {
var ti = this[i], tj = this[j];
if(ti !== undefined && tj !== undefined)
callback.call(thisPtr, ti, tj, i, j, this);
}
}
};
Array.prototype.remove = function(element) {
var l = this.length;
for(var i = 0; i < l; i++) {
if(this[i] == element) {
this.splice(i, 1);
return true;
}
}
return false;
};
Object.values = function(obj) {
var values = [];
Object.forEach(obj, function(v) { values.push(v) });
return values;
};
Object.size = function(obj) { var i = 0; for(var k in obj) ++i; return i; }
Object.reduce = function(obj, f, start, thisPtr) {
var current = start || 0;
for(var k in obj) {
if(obj.hasOwnProperty(k)) {
current = f.call(thisPtr, current, obj[k], k, obj)
}
}
return current;
};
Object.forEach = function(obj, f, thisPtr) {
for(var i in obj) {
var oi = obj[i];
if(oi !== undefined) {
f.call(thisPtr, oi, i, obj);
}
}
};
Object.some = function(obj, f, thisPtr) {
for(var i in obj) {
var oi = obj[i];
if(oi !== undefined && f.call(thisPtr, oi, i, obj) === true) {
return true;
}
}
return false;
};
Object.every = function(obj, f, thisPtr) {
for(var i in obj) {
var oi = obj[i];
if(oi !== undefined && f.call(thisPtr, oi, i, obj) !== true) {
return false;
}
}
return true;
};
Object.forEachPair = function(obj, f, thisPtr) {
for(var i in obj) {
for(var j in obj) {
var oi = obj[i], oj = obj[j];
if(i < j && oi !== undefined && oj !== undefined) {
f.call(thisPtr, oi, oj, i, j, obj);
}
}
}
};
Object.isEmpty = function(obj) {
for (var prop in obj) if (obj.hasOwnProperty(prop)) return false;
return true;
};
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
// 26 Feb 16:19:34
Date.prototype.timestamp = function() {
return [
this.getDate(),
months[this.getMonth()],
[
pad(this.getHours()),
pad(this.getMinutes()),
pad(this.getSeconds())
].join(':')
].join(' ');
}