mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:27:43 +00:00
LibJS: Re-implement the Array Grouping proposal as static methods
Closes #19495.
This commit is contained in:
parent
f418605ec7
commit
419e710c1c
10 changed files with 220 additions and 276 deletions
|
@ -306,43 +306,6 @@ describe("ability to work with generic non-array objects", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test("group", () => {
|
||||
const visited = [];
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
const result = Array.prototype.group.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"]);
|
||||
});
|
||||
|
||||
test("groupToMap", () => {
|
||||
const visited = [];
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
const falseObject = { false: false };
|
||||
const trueObject = { true: true };
|
||||
const result = Array.prototype.groupToMap.call(o, (value, _, object) => {
|
||||
expect(object).toBe(o);
|
||||
visited.push(value);
|
||||
return value !== undefined
|
||||
? value.startsWith("b")
|
||||
? trueObject
|
||||
: falseObject
|
||||
: falseObject;
|
||||
});
|
||||
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
|
||||
expect(result).toBeInstanceOf(Map);
|
||||
|
||||
const falseResult = result.get(falseObject);
|
||||
expect(falseResult).toEqual(["foo", undefined, undefined]);
|
||||
|
||||
const trueResult = result.get(trueObject);
|
||||
expect(trueResult).toEqual(["bar", "baz"]);
|
||||
});
|
||||
|
||||
test("toReversed", () => {
|
||||
const result = Array.prototype.toReversed.call(o);
|
||||
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
test("length is 1", () => {
|
||||
expect(Array.prototype.group).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("callback must be a function", () => {
|
||||
expect(() => {
|
||||
[].group(undefined);
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||
});
|
||||
|
||||
test("null or undefined this value", () => {
|
||||
expect(() => {
|
||||
Array.prototype.group.call();
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.group.call(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.group.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.group(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.group((_, 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.group(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].group).toBeTrue();
|
||||
const array = [];
|
||||
with (array) {
|
||||
expect(() => {
|
||||
group;
|
||||
}).toThrowWithMessage(ReferenceError, "'group' is not defined");
|
||||
}
|
||||
});
|
||||
|
||||
test("never calls callback with empty array", () => {
|
||||
var callbackCalled = 0;
|
||||
expect(
|
||||
[].group(() => {
|
||||
callbackCalled++;
|
||||
})
|
||||
).toEqual({});
|
||||
expect(callbackCalled).toBe(0);
|
||||
});
|
||||
|
||||
test("calls callback once for every item", () => {
|
||||
var callbackCalled = 0;
|
||||
const result = [1, 2, 3].group(() => {
|
||||
callbackCalled++;
|
||||
});
|
||||
expect(result.undefined).toEqual([1, 2, 3]);
|
||||
expect(callbackCalled).toBe(3);
|
||||
});
|
||||
});
|
|
@ -1,107 +0,0 @@
|
|||
test("length is 1", () => {
|
||||
expect(Array.prototype.groupToMap).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("callback must be a function", () => {
|
||||
expect(() => {
|
||||
[].groupToMap(undefined);
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||
});
|
||||
|
||||
test("null or undefined this value", () => {
|
||||
expect(() => {
|
||||
Array.prototype.groupToMap.call();
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.groupToMap.call(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.groupToMap.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 trueObject = { true: true };
|
||||
const falseObject = { false: false };
|
||||
|
||||
const firstResult = array.groupToMap(value => {
|
||||
visited.push(value);
|
||||
return value % 2 === 0 ? trueObject : falseObject;
|
||||
});
|
||||
|
||||
expect(visited).toEqual([1, 2, 3, 4, 5, 6]);
|
||||
expect(firstResult).toBeInstanceOf(Map);
|
||||
expect(firstResult.size).toBe(2);
|
||||
expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
|
||||
expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
|
||||
|
||||
const secondResult = array.groupToMap((_, index) => {
|
||||
return index < array.length / 2 ? trueObject : falseObject;
|
||||
});
|
||||
|
||||
expect(secondResult).toBeInstanceOf(Map);
|
||||
expect(secondResult.size).toBe(2);
|
||||
expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
|
||||
expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
|
||||
|
||||
const thisArg = [7, 8, 9, 10, 11, 12];
|
||||
const thirdResult = array.groupToMap(function (_, __, arrayVisited) {
|
||||
expect(arrayVisited).toBe(array);
|
||||
expect(this).toBe(thisArg);
|
||||
}, thisArg);
|
||||
|
||||
expect(thirdResult).toBeInstanceOf(Map);
|
||||
expect(thirdResult.size).toBe(1);
|
||||
expect(thirdResult.get(undefined)).not.toBe(array);
|
||||
expect(thirdResult.get(undefined)).not.toBe(thisArg);
|
||||
expect(thirdResult.get(undefined)).toEqual(array);
|
||||
});
|
||||
|
||||
test("is unscopable", () => {
|
||||
expect(Array.prototype[Symbol.unscopables].groupToMap).toBeTrue();
|
||||
const array = [];
|
||||
with (array) {
|
||||
expect(() => {
|
||||
groupToMap;
|
||||
}).toThrowWithMessage(ReferenceError, "'groupToMap' is not defined");
|
||||
}
|
||||
});
|
||||
|
||||
test("never calls callback with empty array", () => {
|
||||
var callbackCalled = 0;
|
||||
const result = [].groupToMap(() => {
|
||||
callbackCalled++;
|
||||
});
|
||||
expect(result).toBeInstanceOf(Map);
|
||||
expect(result.size).toBe(0);
|
||||
expect(callbackCalled).toBe(0);
|
||||
});
|
||||
|
||||
test("calls callback once for every item", () => {
|
||||
var callbackCalled = 0;
|
||||
const result = [1, 2, 3].groupToMap(() => {
|
||||
callbackCalled++;
|
||||
});
|
||||
expect(result).toBeInstanceOf(Map);
|
||||
expect(result.size).toBe(1);
|
||||
expect(result.get(undefined)).toEqual([1, 2, 3]);
|
||||
expect(callbackCalled).toBe(3);
|
||||
});
|
||||
|
||||
test("still returns a Map even if the global Map constructor was changed", () => {
|
||||
globalThis.Map = null;
|
||||
const result = [1, 2].groupToMap(value => {
|
||||
return value % 2 === 0;
|
||||
});
|
||||
expect(result.size).toBe(2);
|
||||
expect(result.get(true)).toEqual([2]);
|
||||
expect(result.get(false)).toEqual([1]);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue