@sweepbright/iter-helpers
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -69,3 +69,9 @@ import { ConcurrentMapOptions } from "./ConcurrentMap"; | ||
} | ||
/** | ||
* Create a new chain | ||
* | ||
* Chain is a wrapper allowing to perform a chain of operations on an iterable. | ||
* Such operations can be various transformations of data using methods of the chain. | ||
*/ | ||
export declare function chain<T>(source: Iter<T>): Chain<T>; | ||
export {}; |
@@ -118,2 +118,8 @@ "use strict"; | ||
} | ||
/** | ||
* Create a new chain | ||
* | ||
* Chain is a wrapper allowing to perform a chain of operations on an iterable. | ||
* Such operations can be various transformations of data using methods of the chain. | ||
*/ | ||
function chain(source) { | ||
@@ -120,0 +126,0 @@ return new Chain(source); |
@@ -8,3 +8,8 @@ import { Iter } from "./Iter"; | ||
} | ||
/** | ||
* Creates an async iterable from a list of inputs | ||
* | ||
* Useful when you need to mix multiple sources of data into one | ||
*/ | ||
export declare function mux<T extends Iter<unknown>>(inputs: T[]): AsyncIterable<Iteratee<T>>; | ||
export {}; |
@@ -26,2 +26,7 @@ "use strict"; | ||
exports.Mux = Mux; | ||
/** | ||
* Creates an async iterable from a list of inputs | ||
* | ||
* Useful when you need to mix multiple sources of data into one | ||
*/ | ||
function mux(inputs) { | ||
@@ -28,0 +33,0 @@ return new Mux(inputs); |
@@ -6,12 +6,8 @@ "use strict"; | ||
it("groups items into batches", async () => { | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).batch(3).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([ | ||
[[1, 2, 3]], | ||
[[4, 5, 6]], | ||
[[7, 8, 9]], | ||
[[10]], | ||
]); | ||
const result = await (0, Chain_1.chain)([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | ||
.batch(3) | ||
.toArray(); | ||
expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]); | ||
}); | ||
}); | ||
//# sourceMappingURL=batch.spec.js.map |
@@ -5,6 +5,12 @@ "use strict"; | ||
describe("chain", () => { | ||
it("has a consume method which resolves when the iteration is done", async () => { | ||
//💡Rule: don't forget to call `.consume()` or `.toArray()` | ||
// in order to start the iteration. | ||
//💡Rule: `await` any of this method's result in order to | ||
// wait for the iteration to be done. | ||
await (0, Chain_1.chain)([1, 2, 3]).consume(); | ||
}); | ||
it("consumes an array", async () => { | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[1], [2], [3]]); | ||
const result = await (0, Chain_1.chain)([1, 2, 3]).toArray(); | ||
expect(result).toEqual([1, 2, 3]); | ||
}); | ||
@@ -17,17 +23,6 @@ it("consumes an async iterator", async () => { | ||
} | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)(generate()).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[1], [2], [3]]); | ||
const result = await (0, Chain_1.chain)(generate()).toArray(); | ||
expect(result).toEqual([1, 2, 3]); | ||
}); | ||
describe("toArray", () => { | ||
it("consumes an async generator to array", async () => { | ||
async function* generate() { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
expect(await (0, Chain_1.chain)(generate()).toArray()).toEqual([1, 2, 3]); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=chain.spec.js.map |
@@ -10,3 +10,2 @@ "use strict"; | ||
it("calls a function for each item in parallel returning the transformed item", async () => { | ||
const consumer = jest.fn(); | ||
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
@@ -24,3 +23,3 @@ const startTime = Date.now(); | ||
let maxConcurrency = 0; | ||
await (0, Chain_1.chain)(input) | ||
const result = await (0, Chain_1.chain)(input) | ||
.concurrentMap({ | ||
@@ -38,16 +37,5 @@ // The only required option is concurrency | ||
}, mapper) | ||
.consume(consumer); | ||
.toArray(); | ||
const endTime = Date.now(); | ||
expect(consumer.mock.calls).toEqual([ | ||
[2], | ||
[4], | ||
[6], | ||
[8], | ||
[10], | ||
[12], | ||
[14], | ||
[16], | ||
[18], | ||
[20], | ||
]); | ||
expect(result).toEqual([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]); | ||
expect(maxConcurrency).toBe(concurrency); | ||
@@ -58,16 +46,16 @@ // The overall calculation time should be `concurrency` times | ||
const overheadFactor = 0.1; | ||
expect(endTime - startTime).toBeLessThan(Math.ceil(input.length / concurrency) * delay * (1 + overheadFactor)); | ||
expect(endTime - startTime).toBeLessThan(Math.ceil(input.length / concurrency) * | ||
delay * | ||
(1 + overheadFactor)); | ||
}); | ||
it("should process all items", async () => { | ||
const consumer = jest.fn(); | ||
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
await (0, Chain_1.chain)(input) | ||
const result = await (0, Chain_1.chain)(input) | ||
.concurrentMap({ concurrency: 4 }, async (a) => { | ||
return a; | ||
}) | ||
.consume(consumer); | ||
expect(consumer).toHaveBeenCalledTimes(input.length); | ||
.toArray(); | ||
expect(result).toHaveLength(input.length); | ||
}); | ||
it("should process all items with conditional stopping of the chain source", async () => { | ||
const consumer = jest.fn(); | ||
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
@@ -79,3 +67,3 @@ const fifo = new Fifo_1.Fifo(); | ||
let counter = 0; | ||
await (0, Chain_1.chain)(fifo) | ||
const result = await (0, Chain_1.chain)(fifo) | ||
.concurrentMap({ concurrency: 4 }, async (a) => { | ||
@@ -90,6 +78,6 @@ return a * 2; | ||
}) | ||
.consume(consumer); | ||
expect(consumer).toHaveBeenCalledTimes(input.length); | ||
.toArray(); | ||
expect(result).toHaveLength(input.length); | ||
}); | ||
}); | ||
//# sourceMappingURL=concurrentMap.spec.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Chain_1 = require("../Chain"); | ||
const Range_1 = require("../Range"); | ||
describe("chain.filter", () => { | ||
it("calls a function for each item filtering out the items for which the function returns false", async () => { | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]) | ||
const result = await (0, Chain_1.chain)((0, Range_1.range)(0, 10)) | ||
.filter((item) => item % 2 === 0) | ||
.consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[2]]); | ||
.toArray(); | ||
expect(result).toEqual([0, 2, 4, 6, 8]); | ||
}); | ||
}); | ||
//# sourceMappingURL=filter.spec.js.map |
@@ -6,20 +6,8 @@ "use strict"; | ||
it("flattens arrays into separate items", async () => { | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]) | ||
const result = await (0, Chain_1.chain)([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]) | ||
.flatten() | ||
.consume(consumer); | ||
expect(consumer.mock.calls).toEqual([ | ||
[1], | ||
[2], | ||
[3], | ||
[4], | ||
[5], | ||
[6], | ||
[7], | ||
[8], | ||
[9], | ||
[10], | ||
]); | ||
.toArray(); | ||
expect(result).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
}); | ||
}); | ||
//# sourceMappingURL=flatten.spec.js.map |
@@ -6,9 +6,8 @@ "use strict"; | ||
it("calls a function for each item returning the transformed item", async () => { | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]) | ||
const result = await (0, Chain_1.chain)([1, 2, 3]) | ||
.map((item) => item * 2) | ||
.consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[2], [4], [6]]); | ||
.toArray(); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
}); | ||
//# sourceMappingURL=map.spec.js.map |
@@ -11,5 +11,4 @@ "use strict"; | ||
} | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]).pipe(operator).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[2], [4], [6]]); | ||
const result = await (0, Chain_1.chain)([1, 2, 3]).pipe(operator).toArray(); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
@@ -24,5 +23,4 @@ it("calls a `process` method on an operator object. The `process` method can be just an operator function", async () => { | ||
} | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]).pipe(new MyOperator()).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[2], [4], [6]]); | ||
const result = await (0, Chain_1.chain)([1, 2, 3]).pipe(new MyOperator()).toArray(); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
@@ -35,7 +33,6 @@ it("calls a `process` method on an operator object. The `process` method can use its own chain internally", async () => { | ||
} | ||
const consumer = jest.fn(); | ||
await (0, Chain_1.chain)([1, 2, 3]).pipe(new MyOperator()).consume(consumer); | ||
expect(consumer.mock.calls).toEqual([[2], [4], [6]]); | ||
const result = await (0, Chain_1.chain)([1, 2, 3]).pipe(new MyOperator()).toArray(); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
}); | ||
//# sourceMappingURL=pipe.spec.js.map |
{ | ||
"name": "@sweepbright/iter-helpers", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -49,1 +49,5 @@ # Iter Helpers | ||
- `range(start, end?, step?)` - creates an iterator of numbers | ||
### Mux | ||
- `mux(iterators)` - multiplexes multiple iterators into one |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53
84962
1232