-
-
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.
Merge pull request #1256 from zloirock/group
- Loading branch information
Showing
17 changed files
with
167 additions
and
57 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
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,6 @@ | ||
require('../../modules/es.object.create'); | ||
require('../../modules/esnext.object.group-by'); | ||
|
||
var path = require('../../internals/path'); | ||
|
||
module.exports = path.Object.groupBy; |
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 |
---|---|---|
@@ -1,29 +1,30 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var call = require('../internals/function-call'); | ||
var uncurryThis = require('../internals/function-uncurry-this'); | ||
var isCallable = require('../internals/is-callable'); | ||
var aCallable = require('../internals/a-callable'); | ||
var requireObjectCoercible = require('../internals/require-object-coercible'); | ||
var iterate = require('../internals/iterate'); | ||
var Map = require('../internals/map-helpers').Map; | ||
var MapHelpers = require('../internals/map-helpers'); | ||
|
||
var Map = MapHelpers.Map; | ||
var has = MapHelpers.has; | ||
var get = MapHelpers.get; | ||
var set = MapHelpers.set; | ||
var push = uncurryThis([].push); | ||
|
||
// `Map.groupBy` method | ||
// https://github.com/tc39/proposal-collection-methods | ||
// https://github.com/tc39/proposal-array-grouping | ||
$({ target: 'Map', stat: true, forced: true }, { | ||
groupBy: function groupBy(iterable, keyDerivative) { | ||
var C = isCallable(this) ? this : Map; | ||
var newMap = new C(); | ||
aCallable(keyDerivative); | ||
var has = aCallable(newMap.has); | ||
var get = aCallable(newMap.get); | ||
var set = aCallable(newMap.set); | ||
iterate(iterable, function (element) { | ||
var derivedKey = keyDerivative(element); | ||
if (!call(has, newMap, derivedKey)) call(set, newMap, derivedKey, [element]); | ||
else push(call(get, newMap, derivedKey), element); | ||
groupBy: function groupBy(items, callbackfn) { | ||
requireObjectCoercible(items); | ||
aCallable(callbackfn); | ||
var map = new Map(); | ||
var k = 0; | ||
iterate(items, function (value) { | ||
var key = callbackfn(value, k++); | ||
if (!has(map, key)) set(map, key, [value]); | ||
else push(get(map, key), value); | ||
}); | ||
return newMap; | ||
return map; | ||
} | ||
}); |
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,29 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var getBuiltIn = require('../internals/get-built-in'); | ||
var uncurryThis = require('../internals/function-uncurry-this'); | ||
var aCallable = require('../internals/a-callable'); | ||
var requireObjectCoercible = require('../internals/require-object-coercible'); | ||
var has = require('../internals/has-own-property'); | ||
var toPropertyKey = require('../internals/to-property-key'); | ||
var iterate = require('../internals/iterate'); | ||
|
||
var create = getBuiltIn('Object', 'create'); | ||
var push = uncurryThis([].push); | ||
|
||
// `Object.groupBy` method | ||
// https://github.com/tc39/proposal-array-grouping | ||
$({ target: 'Object', stat: true, forced: true }, { | ||
groupBy: function groupBy(items, callbackfn) { | ||
requireObjectCoercible(items); | ||
aCallable(callbackfn); | ||
var obj = create(null); | ||
var k = 0; | ||
iterate(items, function (value) { | ||
var key = toPropertyKey(callbackfn(value, k++)); | ||
if (!has(obj, key)) obj[key] = [value]; | ||
else push(obj[key], value); | ||
}); | ||
return obj; | ||
} | ||
}); |
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,3 @@ | ||
// https://github.com/tc39/proposal-array-grouping | ||
require('../modules/esnext.map.group-by'); | ||
require('../modules/esnext.object.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
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,25 @@ | ||
import { createIterable } from '../helpers/helpers'; | ||
|
||
QUnit.test('Object.groupBy', assert => { | ||
const { groupBy, getPrototypeOf, entries } = Object; | ||
|
||
assert.isFunction(groupBy); | ||
assert.arity(groupBy, 2); | ||
assert.name(groupBy, 'groupBy'); | ||
assert.looksNative(groupBy); | ||
assert.nonEnumerable(Object, 'groupBy'); | ||
|
||
assert.same(getPrototypeOf(groupBy([], it => it)), null); | ||
|
||
assert.deepEqual(entries(groupBy([], it => it)), []); | ||
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]]]); | ||
|
||
const element = {}; | ||
groupBy([element], function (it, i) { | ||
assert.same(arguments.length, 2); | ||
assert.same(it, element); | ||
assert.same(i, 0); | ||
}); | ||
}); |
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,24 @@ | ||
import { createIterable } from '../helpers/helpers'; | ||
import groupBy from 'core-js-pure/full/object/group-by'; | ||
import getPrototypeOf from 'core-js-pure/es/object/get-prototype-of'; | ||
import entries from 'core-js-pure/es/object/entries'; | ||
|
||
QUnit.test('Object.groupBy', assert => { | ||
assert.isFunction(groupBy); | ||
assert.arity(groupBy, 2); | ||
assert.name(groupBy, 'groupBy'); | ||
|
||
assert.same(getPrototypeOf(groupBy([], it => it)), null); | ||
|
||
assert.deepEqual(entries(groupBy([], it => it)), []); | ||
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]]]); | ||
|
||
const element = {}; | ||
groupBy([element], function (it, i) { | ||
assert.same(arguments.length, 2); | ||
assert.same(it, element); | ||
assert.same(i, 0); | ||
}); | ||
}); |