Skip to content

Commit

Permalink
add a fix of Safari { Object, Map }.groupBy bug that does not suppo…
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 11, 2024
1 parent 5b908c2 commit 80f1d23
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [`RegExp.escape`](https://github.com/tc39/proposal-regex-escaping) [moved to hex-escape semantics](https://github.com/tc39/proposal-regex-escaping/pull/67)
- Some minor updates of [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) Stage 3 proposal like [explicit-resource-management/217](https://github.com/tc39/proposal-explicit-resource-management/pull/217)
- Engines bugs fixes:
- Added a fix of [Safari `{ Object, Map }.groupBy` bug that does not support iterable primitives](https://bugs.webkit.org/show_bug.cgi?id=271524)
- Added a fix of [Safari bug with double call of constructor in `Array.fromAsync`](https://bugs.webkit.org/show_bug.cgi?id=271703)
- Compat data improvements:
- [`URL.parse`](https://url.spec.whatwg.org/#dom-url-parse) added and marked as supported [from FF 126](https://bugzilla.mozilla.org/show_bug.cgi?id=1887611)
Expand Down
12 changes: 8 additions & 4 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,12 @@ export const data = {
safari: '10.0',
},
'es.map.group-by': {
bun: '1.0.19',
// https://bugs.webkit.org/show_bug.cgi?id=271524
bun: '1.1.2', // bun: '1.0.19',
chrome: '117',
firefox: '119',
safari: '17.4',
// https://bugs.webkit.org/show_bug.cgi?id=271524
// safari: '17.4',
},
'es.math.acosh': {
chrome: '54',
Expand Down Expand Up @@ -972,10 +974,12 @@ export const data = {
safari: '9.0',
},
'es.object.group-by': {
bun: '1.0.19',
// https://bugs.webkit.org/show_bug.cgi?id=271524
bun: '1.1.2', // bun: '1.0.19',
chrome: '117',
firefox: '119',
safari: '17.4',
// https://bugs.webkit.org/show_bug.cgi?id=271524
// safari: '17.4',
},
'es.object.has-own': {
chrome: '93',
Expand Down
9 changes: 8 additions & 1 deletion packages/core-js/modules/es.map.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ var requireObjectCoercible = require('../internals/require-object-coercible');
var iterate = require('../internals/iterate');
var MapHelpers = require('../internals/map-helpers');
var IS_PURE = require('../internals/is-pure');
var fails = require('../internals/fails');

var Map = MapHelpers.Map;
var has = MapHelpers.has;
var get = MapHelpers.get;
var set = MapHelpers.set;
var push = uncurryThis([].push);

var DOES_NOT_WORK_WITH_PRIMITIVES = IS_PURE || fails(function () {
return Map.groupBy('ab', function (it) {
return it;
}).get('a').length !== 1;
});

// `Map.groupBy` method
// https://github.com/tc39/proposal-array-grouping
$({ target: 'Map', stat: true, forced: IS_PURE }, {
$({ target: 'Map', stat: true, forced: IS_PURE || DOES_NOT_WORK_WITH_PRIMITIVES }, {
groupBy: function groupBy(items, callbackfn) {
requireObjectCoercible(items);
aCallable(callbackfn);
Expand Down
11 changes: 10 additions & 1 deletion packages/core-js/modules/es.object.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ var aCallable = require('../internals/a-callable');
var requireObjectCoercible = require('../internals/require-object-coercible');
var toPropertyKey = require('../internals/to-property-key');
var iterate = require('../internals/iterate');
var fails = require('../internals/fails');

// eslint-disable-next-line es/no-object-map-groupby -- testing
var nativeGroupBy = Object.groupBy;
var create = getBuiltIn('Object', 'create');
var push = uncurryThis([].push);

var DOES_NOT_WORK_WITH_PRIMITIVES = !nativeGroupBy || fails(function () {
return nativeGroupBy('ab', function (it) {
return it;
}).a.length !== 1;
});

// `Object.groupBy` method
// https://github.com/tc39/proposal-array-grouping
$({ target: 'Object', stat: true }, {
$({ target: 'Object', stat: true, forced: DOES_NOT_WORK_WITH_PRIMITIVES }, {
groupBy: function groupBy(items, callbackfn) {
requireObjectCoercible(items);
aCallable(callbackfn);
Expand Down
10 changes: 8 additions & 2 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,10 @@ GLOBAL.tests = {
&& map[Symbol.toStringTag];
}],
'es.map.group-by': function () {
return Map.groupBy;
// https://bugs.webkit.org/show_bug.cgi?id=271524
return Map.groupBy('ab', function (it) {
return it;
}).get('a').length === 1;
},
'es.math.acosh': function () {
// V8 bug: https://code.google.com/p/v8/issues/detail?id=3509
Expand Down Expand Up @@ -893,7 +896,10 @@ GLOBAL.tests = {
return Object.getPrototypeOf('qwe');
},
'es.object.group-by': function () {
return Object.groupBy;
// https://bugs.webkit.org/show_bug.cgi?id=271524
return Object.groupBy('ab', function (it) {
return it;
}).a.length === 1;
},
'es.object.has-own': function () {
return Object.hasOwn;
Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.map.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ QUnit.test('Map.groupBy', assert => {
assert.deepEqual(toArray(groupBy([1, 2], it => it ** 2)), [[1, [1]], [4, [2]]]);
assert.deepEqual(toArray(groupBy([1, 2, 1], it => it ** 2)), [[1, [1, 1]], [4, [2]]]);
assert.deepEqual(toArray(groupBy(createIterable([1, 2]), it => it ** 2)), [[1, [1]], [4, [2]]]);
assert.deepEqual(toArray(groupBy('qwe', it => it)), [['q', ['q']], ['w', ['w']], ['e', ['e']]], 'iterable string');

const element = {};
groupBy([element], function (it, i) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.object.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ QUnit.test('Object.groupBy', assert => {
assert.deepEqual(entries(groupBy([1, 2], it => it ** 2)), [['1', [1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy([1, 2, 1], it => it ** 2)), [['1', [1, 1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy(createIterable([1, 2]), it => it ** 2)), [['1', [1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy('qwe', it => it)), [['q', ['q']], ['w', ['w']], ['e', ['e']]], 'iterable string');

const element = {};
groupBy([element], function (it, i) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit-pure/es.map.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ QUnit.test('Map.groupBy', assert => {
assert.deepEqual(from(groupBy([1, 2], it => it ** 2)), [[1, [1]], [4, [2]]]);
assert.deepEqual(from(groupBy([1, 2, 1], it => it ** 2)), [[1, [1, 1]], [4, [2]]]);
assert.deepEqual(from(groupBy(createIterable([1, 2]), it => it ** 2)), [[1, [1]], [4, [2]]]);
assert.deepEqual(from(groupBy('qwe', it => it)), [['q', ['q']], ['w', ['w']], ['e', ['e']]], 'iterable string');

const element = {};
groupBy([element], function (it, i) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit-pure/es.object.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ QUnit.test('Object.groupBy', assert => {
assert.deepEqual(entries(groupBy([1, 2], it => it ** 2)), [['1', [1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy([1, 2, 1], it => it ** 2)), [['1', [1, 1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy(createIterable([1, 2]), it => it ** 2)), [['1', [1]], ['4', [2]]]);
assert.deepEqual(entries(groupBy('qwe', it => it)), [['q', ['q']], ['w', ['w']], ['e', ['e']]], 'iterable string');

const element = {};
groupBy([element], function (it, i) {
Expand Down

0 comments on commit 80f1d23

Please sign in to comment.