mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 09:58:14 +00:00
LibJS: Implement proposed Array.prototype.findLast{,Index}
Proposal: https://tc39.es/proposal-array-find-from-last/
This commit is contained in:
parent
44da58c0b2
commit
b6523906b3
5 changed files with 229 additions and 0 deletions
|
@ -0,0 +1,61 @@
|
|||
test("length is 1", () => {
|
||||
expect(Array.prototype.findLast).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("callback must be a function", () => {
|
||||
expect(() => {
|
||||
[].findLast(undefined);
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
expect(array.findLast(value => value === "hello")).toBe("hello");
|
||||
expect(array.findLast((value, index, arr) => index === 1)).toBe("friends");
|
||||
expect(array.findLast(value => value == "1")).toBe(1);
|
||||
expect(array.findLast(value => value === 1)).toBe(1);
|
||||
expect(array.findLast(value => typeof value !== "string")).toBeFalse();
|
||||
expect(array.findLast(value => typeof value === "boolean")).toBeFalse();
|
||||
expect(array.findLast(value => typeof value === "string")).toBe("friends");
|
||||
expect(array.findLast(value => value > 1)).toBe(2);
|
||||
expect(array.findLast(value => value >= 1)).toBe(2);
|
||||
expect(array.findLast(value => value > 1 && value < 3)).toBe(2);
|
||||
expect(array.findLast(value => value > 100)).toBeUndefined();
|
||||
expect([].findLast(value => value === 1)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("never calls callback with empty array", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[].findLast(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toBeUndefined();
|
||||
expect(callbackCalled).toBe(0);
|
||||
});
|
||||
|
||||
test("calls callback once for every item", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[1, 2, 3].findLast(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toBeUndefined();
|
||||
expect(callbackCalled).toBe(3);
|
||||
});
|
||||
|
||||
test("empty slots are treated as undefined", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[1, , , "foo", , undefined, , , 6].findLast(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
})
|
||||
).toBeUndefined();
|
||||
expect(callbackCalled).toBe(2);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,61 @@
|
|||
test("length is 1", () => {
|
||||
expect(Array.prototype.findLastIndex).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("callback must be a function", () => {
|
||||
expect(() => {
|
||||
[].findLastIndex(undefined);
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
expect(array.findLastIndex(value => value === "hello")).toBe(0);
|
||||
expect(array.findLastIndex((value, index, arr) => index === 1)).toBe(1);
|
||||
expect(array.findLastIndex(value => value == "1")).toBe(2);
|
||||
expect(array.findLastIndex(value => value === 1)).toBe(2);
|
||||
expect(array.findLastIndex(value => typeof value !== "string")).toBe(4);
|
||||
expect(array.findLastIndex(value => typeof value === "boolean")).toBe(4);
|
||||
expect(array.findLastIndex(value => typeof value === "string")).toBe(1);
|
||||
expect(array.findLastIndex(value => value > 1)).toBe(3);
|
||||
expect(array.findLastIndex(value => value >= 1)).toBe(3);
|
||||
expect(array.findLastIndex(value => value > 1 && value < 3)).toBe(3);
|
||||
expect(array.findLastIndex(value => value > 100)).toBe(-1);
|
||||
expect([].findLastIndex(value => value === 1)).toBe(-1);
|
||||
});
|
||||
|
||||
test("never calls callback with empty array", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[].findLastIndex(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toBe(-1);
|
||||
expect(callbackCalled).toBe(0);
|
||||
});
|
||||
|
||||
test("calls callback once for every item", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[1, 2, 3].findLastIndex(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toBe(-1);
|
||||
expect(callbackCalled).toBe(3);
|
||||
});
|
||||
|
||||
test("empty slots are treated as undefined", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[1, , , "foo", , undefined, , , 6].findLastIndex(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
})
|
||||
).toBe(7);
|
||||
expect(callbackCalled).toBe(2);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue