File tree Expand file tree Collapse file tree 2 files changed +27
-21
lines changed Expand file tree Collapse file tree 2 files changed +27
-21
lines changed Original file line number Diff line number Diff line change @@ -25,42 +25,48 @@ export function* group ( key , iterable ) {
25
25
26
26
if ( first . done ) return ;
27
27
28
- let item = first . value ;
29
- let nextkey = key ( item ) ;
28
+ let currval = first . value ;
29
+ let currkey = key ( currval ) ;
30
30
31
- let currkey , buffer ;
31
+ let tgtkey ;
32
32
33
- grouping : while ( true ) {
34
-
35
- currkey = nextkey ;
36
- buffer = [ item ] ;
33
+ const grouper = function * ( ) {
37
34
38
35
while ( true ) {
39
36
40
- let current = iterator . next ( ) ;
37
+ yield currval ;
41
38
42
- if ( current . done ) break grouping ;
39
+ let event = iterator . next ( ) ;
40
+ if ( event . done ) return ;
43
41
44
- item = current . value ;
42
+ currval = event . value ;
43
+ currkey = key ( currval ) ;
45
44
46
- nextkey = key ( item ) ;
45
+ if ( currkey !== tgtkey ) return ;
47
46
48
- if ( nextkey !== currkey ) {
47
+ }
49
48
50
- yield [ currkey , buffer ] ;
51
- continue grouping ;
49
+ } ;
52
50
53
- }
51
+ while ( true ) {
54
52
55
- buffer . push ( item ) ;
53
+ tgtkey = currkey ;
56
54
57
- }
55
+ const g = grouper ( ) ;
58
56
59
- break grouping ;
57
+ yield [ tgtkey , g ] ;
60
58
61
- }
59
+ while ( currkey === tgtkey ) {
60
+
61
+ let event = iterator . next ( ) ;
62
+ if ( event . done ) return ;
62
63
63
- yield [ currkey , buffer ] ;
64
+ currval = event . value ;
65
+ currkey = key ( currval ) ;
66
+
67
+ }
68
+
69
+ }
64
70
65
71
}
66
72
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ test( 'group for infinite sequence of something' , t => {
60
60
61
61
const v = Math . random ( ) ;
62
62
63
- [ k , g ] = next ( group ( identity , repeat ( v ) ) ) ;
63
+ const [ k , g ] = next ( group ( identity , repeat ( v ) ) ) ;
64
64
65
65
t . deepEqual ( k , v ) ;
66
66
You can’t perform that action at this time.
0 commit comments