-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.js
158 lines (154 loc) · 3.53 KB
/
map.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
(function (root, factory) { // Universal Module Definition (https://github.com/umdjs/umd)
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.Map = factory();
}
}(this, function () {
function Map() {
}
Map.prototype.put = function(key, val) {
this['$' + key] = val;
};
Map.prototype.get = function(key) {
return this['$' + key];
};
Map.prototype.has = function(key) {
return this.hasOwnProperty('$' + key);
};
Map.prototype.remove = function(key) {
delete this['$' + key];
};
Map.prototype.forEach = function(callback) {
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
callback(k.substring(1), this[k]);
}
};
Map.prototype.map = function(callback) {
var result = new Map;
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
result[k] = callback(k.substring(1), this[k]);
}
return result
}
Map.prototype.mapUpdate = function(callback) {
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
this[k] = callback(k.substring(1), this[k]);
}
}
Map.prototype.mapv = function(callback) {
var result = new Map;
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
result[k] = callback(this[k]);
}
return result
}
Map.prototype.some = function(callback) {
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
var x = callback(k.substring(1), this[k])
if (x) {
return x
}
}
return null
};
Map.prototype.find = Map.prototype.some
Map.prototype.all = function(callback) {
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
if (!callback(k.substring(1), this[k])) {
return false
}
}
return true
};
Map.prototype.clone = function() {
var result = new Map
for (var k in this) {
if (!this.hasOwnProperty(k)) {
continue;
}
result[k] = this[k]
}
return result
}
Map.prototype.size = function() {
var x = 0
for (var k in this) {
if (!this.hasOwnProperty(k))
continue;
x++;
}
return x;
}
Map.prototype.json = function() {
var result = {}
for (var k in this) {
if (!this.hasOwnProperty(k))
continue;
var key = k.substring(1)
result[key] = this[k]
}
return result;
}
Map.prototype.keys = function() {
var result = []
for (var k in this) {
if (!this.hasOwnProperty(k))
continue;
var k = k.substring(1)
result.push(k)
}
return result
}
// Specialized methods
Map.prototype.push = function(key, val) {
key = '$' + key
if (!this[key]) {
this[key] = [];
}
this[key].push(val);
};
Map.prototype.increment = function(key, val) {
key = '$' + key
if (!this[key]) {
this[key] = 0;
}
if (typeof val === 'undefined')
val = 1;
this[key] += val;
};
Map.groupBy = function(list, item2key) {
if (typeof item2key === 'string') {
var prty = item2key;
item2key = function(item) {
return item[prty];
};
}
var map = new Map;
for (var i=0; i<list.length; i++) {
map.push(item2key(list[i]), list[i]);
}
return map;
};
return Map
})); // end of UMD