Repository: mostjs/core
Pull request #149 (2017-10-09 15:57:14)

fix: simplify ZipArrayValuesSink logic
Use take(array.length) to remove duplicate logic in ZipArrayValuesSink

Fix: #147 

briancavalier: ❤️ the simplification
briancavalier: @TylorS Heads-up that I rebased and force pushed this branch.
briancavalier: 73d77c9 adds a regression test based on @TylorS's [webpackbin](https://www.webpackbin.com/bins/-KvrqudMj6C-0H494Itg). 
TylorS: @briancavalier this was rebased from master and force pushed

Patch:
diff --git a/packages/core/src/combinator/zipItems.js b/packages/core/src/combinator/zipItems.js
index 7241765..a236729 100644
--- a/packages/core/src/combinator/zipItems.js
+++ b/packages/core/src/combinator/zipItems.js
@@ -1,15 +1,16 @@
 /** @license MIT License (c) copyright 2017 original author or authors */
 
-import { empty } from '../source/empty'
 import Pipe from '../sink/Pipe'
+import { empty, isCanonicalEmpty } from '../source/empty'
+import { take } from './slice'
 
 export const withItems = (items, stream) =>
   zipItems(keepLeft, items, stream)
 
 export const zipItems = (f, items, stream) =>
-  items.length === 0 || stream === empty()
+  isCanonicalEmpty(stream) || items.length === 0
     ? empty()
-    : new ZipItems(f, items, stream)
+    : new ZipItems(f, items, take(items.length, stream))
 
 const keepLeft = (a, _) => a
 
@@ -36,10 +37,6 @@ class ZipItemsSink extends Pipe {
   event (t, b) {
     const f = this.f
     this.sink.event(t, f(this.items[this.index], b))
-
     this.index += 1
-    if (this.index >= this.items.length) {
-      this.sink.end(t)
-    }
   }
 }


Test patch:
diff --git a/packages/core/test/combinator/zipItems-test.js b/packages/core/test/combinator/zipItems-test.js
index 8ead31f..5059556 100644
--- a/packages/core/test/combinator/zipItems-test.js
+++ b/packages/core/test/combinator/zipItems-test.js
@@ -3,7 +3,11 @@ import { describe, it } from 'mocha'
 import { eq, fail } from '@briancavalier/assert'
 
 import { zipItems, withItems } from '../../src/combinator/zipItems'
+import { map } from '../../src/combinator/transform'
+import { scan } from '../../src/combinator/scan'
+import { switchLatest } from '../../src/combinator/switch'
 import { empty } from '../../src/source/empty'
+import { periodic } from '../../src/source/periodic'
 
 import { collectEventsFor, makeEvents } from '../helper/testEnv'
 
@@ -80,5 +84,28 @@ describe('zipItems', () => {
           { time: 1, value: 'b' }
         ]))
     })
+
+    describe('given https://www.webpackbin.com/bins/-KvrqudMj6C-0H494Itg', () => {
+      it('should contain expected number of items', () => {
+        // Regression test based on
+        // https://www.webpackbin.com/bins/-KvrqudMj6C-0H494Itg
+        const a1 = [1, 2]
+        const a2 = [10, 20, 30]
+        const expectedCount = (a1.length * a2.length) + 1
+
+        const t1 = 2
+        const t2 = t1 / 2
+
+        const s1 = withItems(a1, periodic(t1))
+        const s2 = withItems(a2, periodic(t2))
+
+        const add = (x, y) => x + y
+
+        const scanned = switchLatest(map(a => scan(add, a, s2), s1))
+
+        return collectEventsFor(t1 * a1.length, scanned)
+          .then(events => eq(expectedCount, events.length))
+      })
+    })
   })
 })