1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:07:35 +00:00

LibJS: Add the MapIterator built-in and the key/values/entries methods

While this implementation should be complete it is based on HashMap's
iterator, which currently follows bucket-order instead of the required
insertion order. This can be simply fixed by replacing the underlying
HashMap member in Map with an enhanced one that maintains a linked
list in insertion order.
This commit is contained in:
Idan Horowitz 2021-06-12 23:58:03 +03:00 committed by Linus Groh
parent 6c0d5163a1
commit 322c8a3995
12 changed files with 286 additions and 0 deletions

View file

@ -0,0 +1,26 @@
test("length", () => {
expect(Map.prototype.entries.length).toBe(0);
});
test("basic functionality", () => {
const original = [
["a", 0],
["b", 1],
["c", 2],
];
const a = new Map(original);
const it = a.entries();
// FIXME: This test should be rewritten once we have proper iteration order
const first = it.next();
expect(first.done).toBeFalse();
expect(a.has(first.value[0])).toBeTrue();
const second = it.next();
expect(second.done).toBeFalse();
expect(a.has(second.value[0])).toBeTrue();
const third = it.next();
expect(third.done).toBeFalse();
expect(a.has(third.value[0])).toBeTrue();
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});

View file

@ -0,0 +1,26 @@
test("length", () => {
expect(Map.prototype.keys.length).toBe(0);
});
test("basic functionality", () => {
const original = [
["a", 0],
["b", 1],
["c", 2],
];
const a = new Map(original);
const it = a.keys();
// FIXME: This test should be rewritten once we have proper iteration order
const first = it.next();
expect(first.done).toBeFalse();
expect(a.has(first.value)).toBeTrue();
const second = it.next();
expect(second.done).toBeFalse();
expect(a.has(second.value)).toBeTrue();
const third = it.next();
expect(third.done).toBeFalse();
expect(a.has(third.value)).toBeTrue();
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});

View file

@ -0,0 +1,26 @@
test("length", () => {
expect(Map.prototype.values.length).toBe(0);
});
test("basic functionality", () => {
const original = [
["a", 0],
["b", 1],
["c", 2],
];
const a = new Map(original);
const it = a.values();
// FIXME: This test should be rewritten once we have proper iteration order
const first = it.next();
expect(first.done).toBeFalse();
expect([0, 1, 2].includes(first.value)).toBeTrue();
const second = it.next();
expect(second.done).toBeFalse();
expect([0, 1, 2].includes(second.value)).toBeTrue();
const third = it.next();
expect(third.done).toBeFalse();
expect([0, 1, 2].includes(third.value)).toBeTrue();
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});