Bug 1142337 - Implement NormalizedMap without __noSuchMethod__. r=clokep

This commit is contained in:
aleth 2015-08-07 19:44:06 +02:00
Родитель 49f295a76c
Коммит 06f7c518bf
2 изменённых файлов: 34 добавлений и 10 удалений

Просмотреть файл

@ -32,15 +32,20 @@ NormalizedMap.prototype = {
_normalize: null,
// Anything that accepts a key as an input needs to be manually overridden.
delete: function(aKey) this._map.delete(this._normalize(aKey)),
get: function(aKey) this._map.get(this._normalize(aKey)),
has: function(aKey) this._map.has(this._normalize(aKey)),
set: function(aKey, aValue) this._map.set(this._normalize(aKey), aValue),
delete(key) { return this._map.delete(this._normalize(key)); },
get(key) { return this._map.get(this._normalize(key)); },
has(key) { return this._map.has(this._normalize(key)); },
set(key, val) {
this._map.set(this._normalize(key), val);
return this;
},
// Properties must be manually forwarded.
get size() this._map.size,
// Here's where the magic happens. If a method is called that isn't defined
// here, just pass it to the internal _map object.
__noSuchMethod__: function(aId, aArgs) this._map[aId].apply(this._map, aArgs)
// The remaining methods are unaffected. Delegate until super is available.
get size() { return this._map.size; },
[Symbol.iterator]() { return this._map[Symbol.iterator](); },
entries() { return this._map.entries(); },
keys() { return this._map.keys(); },
values() { return this._map.values(); },
clear() { this._map.clear(); },
forEach(aCallback, aThis) { this._map.forEach(aCallback, aThis); }
};

Просмотреть файл

@ -7,6 +7,8 @@ function test_setter_getter() {
let m = new NormalizedMap(aStr => aStr.toLowerCase());
m.set("foo", "bar");
m.set("BaZ", "blah");
do_check_eq(m.has("FOO"), true);
do_check_eq(m.has("BaZ"), true);
do_check_eq(m.get("FOO"), "bar");
let keys = [v for (v of m.keys())];
@ -44,10 +46,27 @@ function test_iterator() {
run_next_test();
}
function test_delete() {
let m = new NormalizedMap(aStr => aStr.toLowerCase());
m.set("foo", "bar");
m.set("BaZ", "blah");
do_check_eq(m.delete("blah"), false);
do_check_eq(m.delete("FOO"), true);
do_check_eq(m.size, 1);
do_check_eq(m.delete("baz"), true);
do_check_eq(m.size, 0);
run_next_test();
}
function run_test() {
add_test(test_setter_getter);
add_test(test_constructor);
add_test(test_iterator);
add_test(test_delete);
run_next_test();
}