forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-map.js
41 lines (32 loc) · 1.05 KB
/
multi-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
"use strict";
var Map = require("./map").Map;
module.exports = MultiMap;
function MultiMap(values, bucket, equals, hash) {
if (!(this instanceof MultiMap)) {
return new MultiMap(values, bucket, equals, hash);
}
this.bucket = bucket || this.bucket;
new (Function.prototype.bind.call(Map, this, values, equals, hash, function getDefault(key) {
var bucket = this.bucket(key);
Map.prototype.set.call(this, key, bucket);
return bucket;
}));
}
MultiMap.MultiMap = MultiMap; // hack so require("multi-map").MultiMap will work in MontageJS
MultiMap.prototype = Object.create(Map.prototype);
MultiMap.prototype.constructor = MultiMap;
MultiMap.prototype.constructClone = function (values) {
return new this.constructor(
values,
this.bucket,
this.contentEquals,
this.contentHash
);
};
MultiMap.prototype.set = function (key, newValues) {
var values = this.get(key);
values.swap(0, values.length, newValues);
};
MultiMap.prototype.bucket = function (key) {
return [];
};