-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('../../modules/es.map'); | ||
require('../../modules/esnext.array.group-by-map'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('Array', 'groupByMap'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('../../../modules/es.map'); | ||
require('../../../modules/esnext.array.group-by-map'); | ||
var entryVirtual = require('../../../internals/entry-virtual'); | ||
|
||
module.exports = entryVirtual('Array').groupByMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var getBuiltIn = require('../internals/get-built-in'); | ||
var bind = require('../internals/function-bind-context'); | ||
var uncurryThis = require('../internals/function-uncurry-this'); | ||
var IndexedObject = require('../internals/indexed-object'); | ||
var toObject = require('../internals/to-object'); | ||
var lengthOfArrayLike = require('../internals/length-of-array-like'); | ||
var addToUnscopables = require('../internals/add-to-unscopables'); | ||
|
||
var Map = getBuiltIn('Map'); | ||
var MapPrototype = Map.prototype; | ||
var mapGet = uncurryThis(MapPrototype.get); | ||
var mapHas = uncurryThis(MapPrototype.has); | ||
var mapSet = uncurryThis(MapPrototype.set); | ||
var push = uncurryThis([].push); | ||
|
||
// `Array.prototype.groupByMap` method | ||
// https://github.com/tc39/proposal-array-grouping | ||
$({ target: 'Array', proto: true }, { | ||
groupByMap: function groupByMap(callbackfn /* , thisArg */) { | ||
var O = toObject(this); | ||
var self = IndexedObject(O); | ||
var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined); | ||
var map = new Map(); | ||
var length = lengthOfArrayLike(self); | ||
var index = 0; | ||
var key, value; | ||
for (;length > index; index++) { | ||
value = self[index]; | ||
key = boundFunction(value, index, O); | ||
if (mapHas(map, key)) push(mapGet(map, key), value); | ||
else mapSet(map, key, [value]); | ||
} return map; | ||
} | ||
}); | ||
|
||
addToUnscopables('groupByMap'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// https://github.com/tc39/proposal-array-grouping | ||
require('../modules/esnext.array.group-by'); | ||
require('../modules/esnext.array.group-by-map'); | ||
// TODO: Remove from `core-js@4` | ||
require('../modules/esnext.typed-array.group-by'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { STRICT } from '../helpers/constants'; | ||
|
||
import Map from 'core-js-pure/es/map'; | ||
import Symbol from 'core-js-pure/es/symbol'; | ||
import from from 'core-js-pure/es/array/from'; | ||
import groupByMap from 'core-js-pure/features/array/group-by-map'; | ||
|
||
QUnit.test('Array#groupByMap', assert => { | ||
assert.isFunction(groupByMap); | ||
let array = [1]; | ||
const context = {}; | ||
groupByMap(array, function (value, key, that) { | ||
assert.same(arguments.length, 3, 'correct number of callback arguments'); | ||
assert.same(value, 1, 'correct value in callback'); | ||
assert.same(key, 0, 'correct index in callback'); | ||
assert.same(that, array, 'correct link to array in callback'); | ||
assert.same(this, context, 'correct callback context'); | ||
}, context); | ||
assert.ok(groupByMap([], it => it) instanceof Map, 'returns Map'); | ||
assert.deepEqual(from(groupByMap([1, 2, 3], it => it % 2)), [[1, [1, 3]], [0, [2]]], '#1'); | ||
assert.deepEqual( | ||
from(groupByMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], it => `i${ it % 5 }`)), | ||
[['i1', [1, 6, 11]], ['i2', [2, 7, 12]], ['i3', [3, 8]], ['i4', [4, 9]], ['i0', [5, 10]]], | ||
'#2', | ||
); | ||
assert.deepEqual(from(groupByMap(Array(3), it => it)), [[undefined, [undefined, undefined, undefined]]], '#3'); | ||
if (STRICT) { | ||
assert.throws(() => groupByMap(null, () => { /* empty */ }), TypeError); | ||
assert.throws(() => groupByMap(undefined, () => { /* empty */ }), TypeError); | ||
} | ||
array = [1]; | ||
// eslint-disable-next-line object-shorthand -- constructor | ||
array.constructor = { [Symbol.species]: function () { | ||
return { foo: 1 }; | ||
} }; | ||
assert.same(groupByMap(array, Boolean).get(true).foo, undefined, 'no @@species'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { STRICT } from '../helpers/constants'; | ||
|
||
const { from } = Array; | ||
|
||
QUnit.test('Array#groupByMap', assert => { | ||
const { groupByMap } = Array.prototype; | ||
assert.isFunction(groupByMap); | ||
assert.arity(groupByMap, 1); | ||
assert.name(groupByMap, 'groupByMap'); | ||
assert.looksNative(groupByMap); | ||
assert.nonEnumerable(Array.prototype, 'groupByMap'); | ||
let array = [1]; | ||
const context = {}; | ||
array.groupByMap(function (value, key, that) { | ||
assert.same(arguments.length, 3, 'correct number of callback arguments'); | ||
assert.same(value, 1, 'correct value in callback'); | ||
assert.same(key, 0, 'correct index in callback'); | ||
assert.same(that, array, 'correct link to array in callback'); | ||
assert.same(this, context, 'correct callback context'); | ||
}, context); | ||
assert.ok([].groupByMap(it => it) instanceof Map, 'returns Map'); | ||
assert.deepEqual(from([1, 2, 3].groupByMap(it => it % 2)), [[1, [1, 3]], [0, [2]]], '#1'); | ||
assert.deepEqual( | ||
from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].groupByMap(it => `i${ it % 5 }`)), | ||
[['i1', [1, 6, 11]], ['i2', [2, 7, 12]], ['i3', [3, 8]], ['i4', [4, 9]], ['i0', [5, 10]]], | ||
'#2', | ||
); | ||
assert.deepEqual(from(Array(3).groupByMap(it => it)), [[undefined, [undefined, undefined, undefined]]], '#3'); | ||
if (STRICT) { | ||
assert.throws(() => groupByMap.call(null, () => { /* empty */ }), TypeError); | ||
assert.throws(() => groupByMap.call(undefined, () => { /* empty */ }), TypeError); | ||
} | ||
array = [1]; | ||
// eslint-disable-next-line object-shorthand -- constructor | ||
array.constructor = { [Symbol.species]: function () { | ||
return { foo: 1 }; | ||
} }; | ||
assert.same(array.groupByMap(Boolean).get(true).foo, undefined, 'no @@species'); | ||
}); |