-
Notifications
You must be signed in to change notification settings - Fork 0
/
newStdLib.js
73 lines (62 loc) · 1.26 KB
/
newStdLib.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
Object.prototype.uniq = function() {
var out = [];
for(i in this) {
if (this.hasOwnProperty(i) && out.indexOf(this[i]) == -1) {
out.push(this[i])
}
}
return out;
}
Object.prototype.uniqc = function(callback) {
var out = {};
for(i in this) {
if (this.hasOwnProperty(i)) {
var val = callback ? callback(this[i]) : this[i];
out[val] = out[val] ? ++out[val] : 1;
}
}
return out;
}
Array.prototype.index = function(i) {
return this[i];
}
Object.prototype._l = function() {
console.log(this);
return this;
}
Object.prototype.map = function (callback) {
var out = {};
for(i in this) {
if (this.hasOwnProperty(i)) {
out[i] = callback(i, this[i]);
}
}
return out
}
Object.prototype.filter = function (callback) {
var out = {};
for(i in this) {
if (this.hasOwnProperty(i) && callback(i, this[i])) {
out[i] = this[i];
}
}
return out
}
Object.prototype.keys = function (callback) {
return Object.keys(this);
}
Object.prototype.toArray = function() {
return Object.values(this);
}
Object.prototype.reduce = function (callback, initial) {
for(i in this) {
if (typeof initial === 'undefined') {
intial = this[i];
continue;
}
if (this.hasOwnProperty(i)) {
initial = callback(initial, this[i], i);
}
}
return initial
}