mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:27:45 +00:00
LibJS: Implement Array.prototype.groupBy
This commit is contained in:
parent
4a14455dff
commit
48cc1c97d5
5 changed files with 202 additions and 1 deletions
|
@ -304,4 +304,17 @@ describe("ability to work with generic non-array objects", () => {
|
|||
2,
|
||||
]);
|
||||
});
|
||||
|
||||
test("groupBy", () => {
|
||||
const visited = [];
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
const result = Array.prototype.groupBy.call(o, (value, _, object) => {
|
||||
expect(object).toBe(o);
|
||||
visited.push(value);
|
||||
return value !== undefined ? value.startsWith("b") : false;
|
||||
});
|
||||
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
|
||||
expect(result.false).toEqual(["foo", undefined, undefined]);
|
||||
expect(result.true).toEqual(["bar", "baz"]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
test("length is 1", () => {
|
||||
expect(Array.prototype.groupBy).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("callback must be a function", () => {
|
||||
expect(() => {
|
||||
[].groupBy(undefined);
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||
});
|
||||
|
||||
test("null or undefined this value", () => {
|
||||
expect(() => {
|
||||
Array.prototype.groupBy.call();
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.groupBy.call(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.groupBy.call(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6];
|
||||
const visited = [];
|
||||
|
||||
const firstResult = array.groupBy(value => {
|
||||
visited.push(value);
|
||||
return value % 2 === 0;
|
||||
});
|
||||
|
||||
expect(visited).toEqual([1, 2, 3, 4, 5, 6]);
|
||||
expect(firstResult.true).toEqual([2, 4, 6]);
|
||||
expect(firstResult.false).toEqual([1, 3, 5]);
|
||||
|
||||
const firstKeys = Object.keys(firstResult);
|
||||
expect(firstKeys).toHaveLength(2);
|
||||
expect(firstKeys[0]).toBe("false");
|
||||
expect(firstKeys[1]).toBe("true");
|
||||
|
||||
const secondResult = array.groupBy((_, index) => {
|
||||
return index < array.length / 2;
|
||||
});
|
||||
|
||||
expect(secondResult.true).toEqual([1, 2, 3]);
|
||||
expect(secondResult.false).toEqual([4, 5, 6]);
|
||||
|
||||
const secondKeys = Object.keys(secondResult);
|
||||
expect(secondKeys).toHaveLength(2);
|
||||
expect(secondKeys[0]).toBe("true");
|
||||
expect(secondKeys[1]).toBe("false");
|
||||
|
||||
const thisArg = [7, 8, 9, 10, 11, 12];
|
||||
const thirdResult = array.groupBy(function (_, __, arrayVisited) {
|
||||
expect(arrayVisited).toBe(array);
|
||||
expect(this).toBe(thisArg);
|
||||
}, thisArg);
|
||||
|
||||
expect(thirdResult.undefined).not.toBe(array);
|
||||
expect(thirdResult.undefined).not.toBe(thisArg);
|
||||
expect(thirdResult.undefined).toEqual(array);
|
||||
|
||||
const thirdKeys = Object.keys(thirdResult);
|
||||
expect(thirdKeys).toHaveLength(1);
|
||||
expect(thirdKeys[0]).toBe("undefined");
|
||||
});
|
||||
|
||||
test("is unscopable", () => {
|
||||
expect(Array.prototype[Symbol.unscopables].groupBy).toBeTrue();
|
||||
const array = [];
|
||||
with (array) {
|
||||
expect(() => {
|
||||
groupBy;
|
||||
}).toThrowWithMessage(ReferenceError, "'groupBy' is not defined");
|
||||
}
|
||||
});
|
||||
|
||||
test("never calls callback with empty array", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[].groupBy(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toEqual({});
|
||||
expect(callbackCalled).toBe(0);
|
||||
});
|
||||
|
||||
test("calls callback once for every item", () => {
|
||||
var callbackCalled = 0;
|
||||
const result = [1, 2, 3].groupBy(() => {
|
||||
callbackCalled++;
|
||||
});
|
||||
expect(result.undefined).toEqual([1, 2, 3]);
|
||||
expect(callbackCalled).toBe(3);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue