8000 generalize groupBy to return Seq and not Map · powercoder23/immutable-js@2c3b502 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c3b502

Browse files
committed
generalize groupBy to return Seq and not Map
1 parent 02079c5 commit 2c3b502

File tree

5 files changed

+99
-87
lines changed

5 files changed

+99
-87
lines changed

dist/Immutable.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,26 +598,26 @@ declare module 'immutable' {
598598
): Sequence<K, V>;
599599

600600
/**
601-
* Returns a `Map` of counts, grouped by the return value of the
601+
* Returns a `Sequence` of counts, grouped by the return value of the
602602
* `grouper` function.
603603
*
604-
* Note: Because this returns a Map, this method is not lazy.
604+
* Note: This is not a lazy operation.
605605
*/
606606
countBy<G>(
607607
grouper: (value?: V, key?: K, seq?: Sequence<K, V>) => G,
608608
thisArg?: any
609-
): Map<G, number>;
609+
): Sequence<G, number>;
610610

611611
/**
612-
* Returns a `Map` of sequences, grouped by the return value of the
612+
* Returns a `Sequence` of `Sequences`, grouped by the return value of the
613613
* `grouper` function.
614614
*
615-
* Note: Because this returns a Map, this method is not lazy.
615+
* Note: This is not a lazy operation.
616616
*/
617617
groupBy<G>(
618618
grouper: (value?: V, key?: K, seq?: Sequence<K, V>) => G,
619619
thisArg?: any
620-
): Map<G, Sequence<K, V>>;
620+
): Sequence<G, Sequence<K, V>>;
621621

622622
sort(comparator?: (valueA: V, valueB: V) => number): Sequence<K, V>;
623623

@@ -823,13 +823,13 @@ declare module 'immutable' {
823823
): IndexedSequence<T>;
824824

825825
/**
826-
* Returns Map<G, IndexedSequence<T>>
826+
* Returns Sequence<G, IndexedSequence<T>>
827827
* @override
828828
*/
829829
groupBy<G>(
830830
grouper: (value?: T, index?: number, seq?: IndexedSequence<T>) => G,
831831
thisArg?: any
832-
): Map<G, any/*IndexedSequence<T>*/>; // Bug: exposing this causes the type checker to implode.
832+
): Sequence<G, any/*IndexedSequence<T>*/>; // Bug: exposing this causes the type checker to implode.
833833

834834
/**
835835
* Returns an IndexedSequence

dist/Immutable.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,21 @@ var $Sequence = Sequence;
272272
}
273273
return this.filter(predicate, thisArg).count();
274274
},
275-
countBy: function(mapper, context) {
276-
var seq = this;
277-
return OrderedMap.empty().withMutations((function(map) {
278-
seq.forEach((function(value, key, collection) {
279-
map.update(mapper(value, key, collection), increment);
280-
}));
275+
countBy: function(grouper, context) {
276+
var $__0 = this;
277+
var groupMap = {};
278+
var groups = [];
279+
this.forEach((function(v, k) {
280+
var g = grouper.call(context, v, k, $__0);
281+
var h = hash(g);
282+
if (!groupMap.hasOwnProperty(h)) {
283+
groupMap[h] = groups.length;
284+
groups.push([g, 1]);
285+
} else {
286+
groups[groupMap[h]][1]++;
287+
}
281288
}));
289+
return $Sequence(groups).fromEntrySeq();
282290
},
283291
concat: function() {
284292
for (var values = [],
@@ -557,8 +565,8 @@ var $Sequence = Sequence;
557565
skipUntil: function(predicate, thisArg) {
558566
return this.skipWhile(not(predicate), thisArg);
559567
},
560-
groupBy: function(mapper, context) {
561-
return groupByFactory(this, mapper, context, true);
568+
groupBy: function(grouper, context) {
569+
return groupByFactory(this, grouper, context, true);
562570
},
563571
sort: function(comparator) {
564572
return this.sortBy(valueMapper, comparator);
@@ -754,8 +762,8 @@ var $IndexedSequence = IndexedSequence;
754762
skipWhile: function(predicate, thisArg) {
755763
return skipWhileFactory(this, predicate, thisArg, false);
756764
},
757-
groupBy: function(mapper, context) {
758-
return groupByFactory(this, mapper, context, false);
765+
groupBy: function(grouper, context) {
766+
return groupByFactory(this, grouper, context, false);
759767
},
760768
sortBy: function(mapper, comparator) {
761769
comparator = comparator || defaultComparator;
@@ -917,9 +925,6 @@ function returnTrue() {
917925
function returnThis() {
918926
return this;
919927
}
920-
function increment(value) {
921-
return (value || 0) + 1;
922-
}
923928
function filterFactory(sequence, predicate, context, useKeys) {
924929
var filterSequence = sequence.__makeSequence();
925930
filterSequence.__iterateUncached = function(fn, reverse, reverseIndices) {
@@ -935,19 +940,21 @@ function filterFactory(sequence, predicate, context, useKeys) {
935940
};
936941
return filterSequence;
937942
}
938-
function groupByFactory(seq, mapper, context, useKeys) {
939-
var groups = OrderedMap.empty().withMutations((function(map) {
940-
seq.forEach((function(value, key, collection) {
941-
var groupKey = mapper.call(context, value, key, seq);
942-
var group = map.get(groupKey);
943-
if (!group) {
944-
group = [];
945-
map.set(groupKey, group);
946-
}
947-
group.push(useKeys ? [key, value] : value);
948-
}));
943+
function groupByFactory(seq, grouper, context, useKeys) {
944+
var groupMap = {};
945+
var groups = [];
946+
seq.forEach((function(v, k) {
947+
var g = grouper.call(context, v, k, seq);
948+
var h = hash(g);
949+
var e = useKeys ? [k, v] : v;
950+
if (!groupMap.hasOwnProperty(h)) {
951+
groupMap[h] = groups.length;
952+
groups.push([g, [e]]);
953+
} else {
954+
groups[groupMap[h]][1].push(e);
955+
}
949956
}));
950-
return groups.map(useKeys ? (function(group) {
957+
return Sequence(groups).fromEntrySeq().map(useKeys ? (function(group) {
951958
return Sequence(group).fromEntrySeq();
952959
}) : (function(group) {
953960
return Sequence(group);

0 commit comments

Comments
 (0)
0