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

LibJS: Rename Array.prototype.groupBy{,ToMap} => group{,ToMap}

This is a normative change in the Array Grouping spec.

See: 0cf4077
This commit is contained in:
Linus Groh 2022-06-13 19:47:04 +01:00
parent c10d48b72c
commit 013e2df858
6 changed files with 45 additions and 44 deletions

View file

@ -305,10 +305,10 @@ describe("ability to work with generic non-array objects", () => {
]);
});
test("groupBy", () => {
test("group", () => {
const visited = [];
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
const result = Array.prototype.groupBy.call(o, (value, _, object) => {
const result = Array.prototype.group.call(o, (value, _, object) => {
expect(object).toBe(o);
visited.push(value);
return value !== undefined ? value.startsWith("b") : false;
@ -318,12 +318,12 @@ describe("ability to work with generic non-array objects", () => {
expect(result.true).toEqual(["bar", "baz"]);
});
test("groupByToMap", () => {
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.groupByToMap.call(o, (value, _, object) => {
const result = Array.prototype.groupToMap.call(o, (value, _, object) => {
expect(object).toBe(o);
visited.push(value);
return value !== undefined

View file

@ -1,25 +1,25 @@
test("length is 1", () => {
expect(Array.prototype.groupBy).toHaveLength(1);
expect(Array.prototype.group).toHaveLength(1);
});
describe("errors", () => {
test("callback must be a function", () => {
expect(() => {
[].groupBy(undefined);
[].group(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
test("null or undefined this value", () => {
expect(() => {
Array.prototype.groupBy.call();
Array.prototype.group.call();
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.groupBy.call(undefined);
Array.prototype.group.call(undefined);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.groupBy.call(null);
Array.prototype.group.call(null);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
});
});
@ -29,7 +29,7 @@ describe("normal behavior", () => {
const array = [1, 2, 3, 4, 5, 6];
const visited = [];
const firstResult = array.groupBy(value => {
const firstResult = array.group(value => {
visited.push(value);
return value % 2 === 0;
});
@ -43,7 +43,7 @@ describe("normal behavior", () => {
expect(firstKeys[0]).toBe("false");
expect(firstKeys[1]).toBe("true");
const secondResult = array.groupBy((_, index) => {
const secondResult = array.group((_, index) => {
return index < array.length / 2;
});
@ -56,7 +56,7 @@ describe("normal behavior", () => {
expect(secondKeys[1]).toBe("false");
const thisArg = [7, 8, 9, 10, 11, 12];
const thirdResult = array.groupBy(function (_, __, arrayVisited) {
const thirdResult = array.group(function (_, __, arrayVisited) {
expect(arrayVisited).toBe(array);
expect(this).toBe(thisArg);
}, thisArg);
@ -71,19 +71,19 @@ describe("normal behavior", () => {
});
test("is unscopable", () => {
expect(Array.prototype[Symbol.unscopables].groupBy).toBeTrue();
expect(Array.prototype[Symbol.unscopables].group).toBeTrue();
const array = [];
with (array) {
expect(() => {
groupBy;
}).toThrowWithMessage(ReferenceError, "'groupBy' is not defined");
group;
}).toThrowWithMessage(ReferenceError, "'group' is not defined");
}
});
test("never calls callback with empty array", () => {
var callbackCalled = 0;
expect(
[].groupBy(() => {
[].group(() => {
callbackCalled++;
})
).toEqual({});
@ -92,7 +92,7 @@ describe("normal behavior", () => {
test("calls callback once for every item", () => {
var callbackCalled = 0;
const result = [1, 2, 3].groupBy(() => {
const result = [1, 2, 3].group(() => {
callbackCalled++;
});
expect(result.undefined).toEqual([1, 2, 3]);

View file

@ -1,25 +1,25 @@
test("length is 1", () => {
expect(Array.prototype.groupByToMap).toHaveLength(1);
expect(Array.prototype.groupToMap).toHaveLength(1);
});
describe("errors", () => {
test("callback must be a function", () => {
expect(() => {
[].groupByToMap(undefined);
[].groupToMap(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
test("null or undefined this value", () => {
expect(() => {
Array.prototype.groupByToMap.call();
Array.prototype.groupToMap.call();
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.groupByToMap.call(undefined);
Array.prototype.groupToMap.call(undefined);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.groupByToMap.call(null);
Array.prototype.groupToMap.call(null);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
});
});
@ -31,7 +31,7 @@ describe("normal behavior", () => {
const trueObject = { true: true };
const falseObject = { false: false };
const firstResult = array.groupByToMap(value => {
const firstResult = array.groupToMap(value => {
visited.push(value);
return value % 2 === 0 ? trueObject : falseObject;
});
@ -42,7 +42,7 @@ describe("normal behavior", () => {
expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
const secondResult = array.groupByToMap((_, index) => {
const secondResult = array.groupToMap((_, index) => {
return index < array.length / 2 ? trueObject : falseObject;
});
@ -52,7 +52,7 @@ describe("normal behavior", () => {
expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
const thisArg = [7, 8, 9, 10, 11, 12];
const thirdResult = array.groupByToMap(function (_, __, arrayVisited) {
const thirdResult = array.groupToMap(function (_, __, arrayVisited) {
expect(arrayVisited).toBe(array);
expect(this).toBe(thisArg);
}, thisArg);
@ -65,18 +65,18 @@ describe("normal behavior", () => {
});
test("is unscopable", () => {
expect(Array.prototype[Symbol.unscopables].groupByToMap).toBeTrue();
expect(Array.prototype[Symbol.unscopables].groupToMap).toBeTrue();
const array = [];
with (array) {
expect(() => {
groupByToMap;
}).toThrowWithMessage(ReferenceError, "'groupByToMap' is not defined");
groupToMap;
}).toThrowWithMessage(ReferenceError, "'groupToMap' is not defined");
}
});
test("never calls callback with empty array", () => {
var callbackCalled = 0;
const result = [].groupByToMap(() => {
const result = [].groupToMap(() => {
callbackCalled++;
});
expect(result).toBeInstanceOf(Map);
@ -86,7 +86,7 @@ describe("normal behavior", () => {
test("calls callback once for every item", () => {
var callbackCalled = 0;
const result = [1, 2, 3].groupByToMap(() => {
const result = [1, 2, 3].groupToMap(() => {
callbackCalled++;
});
expect(result).toBeInstanceOf(Map);
@ -97,7 +97,7 @@ describe("normal behavior", () => {
test("still returns a Map even if the global Map constructor was changed", () => {
globalThis.Map = null;
const result = [1, 2].groupByToMap(value => {
const result = [1, 2].groupToMap(value => {
return value % 2 === 0;
});
expect(result.size).toBe(2);