Ok, I've raised the issue on esdiscuss now and I got an answer from Mozilla's Jason Orendorff:
- This is a problem with ES6 maps.
- The solution will come in the form of ES7 value objects for keys instead of objects.
- It was considered before to let people specify
.equals
and.hashCode
but it was rejected in favor of value objects. (for good reasons in my opinion). - The only solution as of now is to roll your own collection.
A basic such collection (concept, don't use in production code) was offered by Bradley on the ESDiscuss thread and might look something like this:
function HashMap(hash) {
var map = new Map;
var _set = map.set;
var _get = map.get;
var _has = map.has;
var _delete = map.delete;
map.set = function (k,v) {
return _set.call(map, hash(k), v);
}
map.get = function (k) {
return _get.call(map, hash(k));
}
map.has = function (k) {
return _has.call(map, hash(k));
}
map.delete = function (k) {
return _delete.call(map, hash(k));
}
return map;
}
function TupleMap() {
return new HashMap(function (tuple) {
var keys = Object.keys(tuple).sort();
return keys.map(function (tupleKey) { // hash based on JSON stringification
return JSON.stringify(tupleKey) + JSON.stringify(tuple[tupleKey]);
}).join('\n');
return hashed;
});
}
A better solution is to use something like MontageJS/Collections which allows for specification of hash/equals functions.
You can see the API docs here.