This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
map.js
124 lines (97 loc) · 2.7 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
/*
Map
*/"use strict"
var indexOf = require("mout/array/indexOf")
var prime = require("./index")
var Map = prime({
constructor: function Map(){
this.length = 0
this._values = []
this._keys = []
},
set: function(key, value){
var index = indexOf(this._keys, key)
if (index === -1){
this._keys.push(key)
this._values.push(value)
this.length++
} else {
this._values[index] = value
}
return this
},
get: function(key){
var index = indexOf(this._keys, key)
return (index === -1) ? null : this._values[index]
},
count: function(){
return this.length
},
forEach: function(method, context){
for (var i = 0, l = this.length; i < l; i++){
if (method.call(context, this._values[i], this._keys[i], this) === false) break
}
return this
},
map: function(method, context){
var results = new Map
this.forEach(function(value, key){
results.set(key, method.call(context, value, key, this))
}, this)
return results
},
filter: function(method, context){
var results = new Map
this.forEach(function(value, key){
if (method.call(context, value, key, this)) results.set(key, value)
}, this)
return results
},
every: function(method, context){
var every = true
this.forEach(function(value, key){
if (!method.call(context, value, key, this)) return (every = false)
}, this)
return every
},
some: function(method, context){
var some = false
this.forEach(function(value, key){
if (method.call(context, value, key, this)) return !(some = true)
}, this)
return some
},
indexOf: function(value){
var index = indexOf(this._values, value)
return (index > -1) ? this._keys[index] : null
},
remove: function(value){
var index = indexOf(this._values, value)
if (index !== -1){
this._values.splice(index, 1)
this.length--
return this._keys.splice(index, 1)[0]
}
return null
},
unset: function(key){
var index = indexOf(this._keys, key)
if (index !== -1){
this._keys.splice(index, 1)
this.length--
return this._values.splice(index, 1)[0]
}
return null
},
keys: function(){
return this._keys.slice()
},
values: function(){
return this._values.slice()
}
})
var map = function(){
return new Map
}
map.prototype = Map.prototype
module.exports = map