1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:47:47 +00:00

LibJS: Implement 'Relative Indexing Method' proposal (.at())

Still stage 3, but already implemented in major engines and unlikely to
change - there isn't much to change here anyway. :^)

See:

- https://github.com/tc39/proposal-relative-indexing-method
- https://tc39.es/proposal-relative-indexing-method/
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
This commit is contained in:
Linus Groh 2021-03-12 18:13:12 +01:00 committed by Andreas Kling
parent 9769542bc2
commit 2d8362cceb
10 changed files with 155 additions and 10 deletions

View file

@ -0,0 +1,15 @@
test("basic functionality", () => {
expect(Array.prototype.at).toHaveLength(1);
const array = ["a", "b", "c"];
expect(array.at(0)).toBe("a");
expect(array.at(1)).toBe("b");
expect(array.at(2)).toBe("c");
expect(array.at(3)).toBeUndefined();
expect(array.at(Infinity)).toBeUndefined();
expect(array.at(-1)).toBe("c");
expect(array.at(-2)).toBe("b");
expect(array.at(-3)).toBe("a");
expect(array.at(-4)).toBeUndefined();
expect(array.at(-Infinity)).toBeUndefined();
});

View file

@ -0,0 +1,15 @@
test("basic functionality", () => {
expect(String.prototype.at).toHaveLength(1);
const string = "abc";
expect(string.at(0)).toBe("a");
expect(string.at(1)).toBe("b");
expect(string.at(2)).toBe("c");
expect(string.at(3)).toBeUndefined();
expect(string.at(Infinity)).toBeUndefined();
expect(string.at(-1)).toBe("c");
expect(string.at(-2)).toBe("b");
expect(string.at(-3)).toBe("a");
expect(string.at(-4)).toBeUndefined();
expect(string.at(-Infinity)).toBeUndefined();
});

View file

@ -0,0 +1,33 @@
// Update when more typed arrays get added
const TYPED_ARRAYS = [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
test("basic functionality", () => {
TYPED_ARRAYS.forEach(T => {
expect(T.prototype.at).toHaveLength(1);
const typedArray = new T(3);
typedArray[0] = 1;
typedArray[1] = 2;
typedArray[2] = 3;
expect(typedArray.at(0)).toBe(1);
expect(typedArray.at(1)).toBe(2);
expect(typedArray.at(2)).toBe(3);
expect(typedArray.at(3)).toBeUndefined();
expect(typedArray.at(Infinity)).toBeUndefined();
expect(typedArray.at(-1)).toBe(3);
expect(typedArray.at(-2)).toBe(2);
expect(typedArray.at(-3)).toBe(1);
expect(typedArray.at(-4)).toBeUndefined();
expect(typedArray.at(-Infinity)).toBeUndefined();
});
});