mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:44:58 +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:
parent
c10d48b72c
commit
013e2df858
6 changed files with 45 additions and 44 deletions
|
@ -73,8 +73,8 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.keys, keys, 0, attr);
|
||||
define_native_function(vm.names.entries, entries, 0, attr);
|
||||
define_native_function(vm.names.copyWithin, copy_within, 2, attr);
|
||||
define_native_function(vm.names.groupBy, group_by, 1, attr);
|
||||
define_native_function(vm.names.groupByToMap, group_by_to_map, 1, attr);
|
||||
define_native_function(vm.names.group, group, 1, attr);
|
||||
define_native_function(vm.names.groupToMap, group_to_map, 1, attr);
|
||||
|
||||
// Use define_direct_property here instead of define_native_function so that
|
||||
// Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
|
||||
|
@ -96,8 +96,8 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
|||
MUST(unscopable_list->create_data_property_or_throw(vm.names.findLastIndex, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.flat, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.flatMap, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.groupBy, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.groupByToMap, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.group, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.groupToMap, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.includes, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.keys, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.values, Value(true)));
|
||||
|
@ -1673,7 +1673,7 @@ static void add_value_to_keyed_group(GlobalObject& global_object, GroupsType& gr
|
|||
{
|
||||
// 1. For each Record { [[Key]], [[Elements]] } g of groups, do
|
||||
// a. If SameValue(g.[[Key]], key) is true, then
|
||||
// NOTE: This is performed in KeyedGroupTraits::equals for groupByToMap and Traits<JS::PropertyKey>::equals for groupBy.
|
||||
// NOTE: This is performed in KeyedGroupTraits::equals for groupToMap and Traits<JS::PropertyKey>::equals for group.
|
||||
auto existing_elements_iterator = groups.find(key);
|
||||
if (existing_elements_iterator != groups.end()) {
|
||||
// i. Assert: exactly one element of groups meets this criteria.
|
||||
|
@ -1695,8 +1695,9 @@ static void add_value_to_keyed_group(GlobalObject& global_object, GroupsType& gr
|
|||
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
||||
}
|
||||
|
||||
// 2.1 Array.prototype.groupBy ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupby
|
||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by)
|
||||
// FIXME: Spec has incorrect function ID
|
||||
// 2.1 Array.prototype.group ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupby
|
||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group)
|
||||
{
|
||||
auto callback_function = vm.argument(0);
|
||||
auto this_arg = vm.argument(1);
|
||||
|
@ -1749,8 +1750,9 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by)
|
|||
return object;
|
||||
}
|
||||
|
||||
// 2.2 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
|
||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by_to_map)
|
||||
// FIXME: Spec has incorrect function ID
|
||||
// 2.2 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
|
||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_to_map)
|
||||
{
|
||||
auto callback_function = vm.argument(0);
|
||||
auto this_arg = vm.argument(1);
|
||||
|
|
|
@ -54,8 +54,8 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(keys);
|
||||
JS_DECLARE_NATIVE_FUNCTION(entries);
|
||||
JS_DECLARE_NATIVE_FUNCTION(copy_within);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group_by);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group_by_to_map);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group_to_map);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -243,11 +243,10 @@ namespace JS {
|
|||
P(globalThis) \
|
||||
P(granularity) \
|
||||
P(group) \
|
||||
P(groupBy) \
|
||||
P(groupByToMap) \
|
||||
P(groupCollapsed) \
|
||||
P(groupEnd) \
|
||||
P(groups) \
|
||||
P(groupToMap) \
|
||||
P(has) \
|
||||
P(hasIndices) \
|
||||
P(hasOwn) \
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]);
|
|
@ -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);
|
Loading…
Add table
Add a link
Reference in a new issue