diff --git a/doc/API.md b/doc/API.md index 6f242d4..e3a3cc3 100644 --- a/doc/API.md +++ b/doc/API.md @@ -15,6 +15,7 @@ - [Channel.from(callback | iterable | stream.Readable[, mapfn [, thisArg]]) -> read-only Channel](#channelfromcallback--iterable--streamreadable-mapfn--thisarg---read-only-channel) - [Examples](#examples-1) - [Channel Object](#channel-object) + - [concat(...arguments) -> Channel](#concatarguments---channel) - [every(callbackfn[, thisArg]) -> async Boolean](#everycallbackfn-thisarg---async-boolean) - [filter(callbackfn[, thisArg]) -> Channel](#filtercallbackfn-thisarg---channel) - [forEach(callbackfn[, thisArg]) -> async](#foreachcallbackfn-thisarg---async) @@ -202,6 +203,12 @@ const fromStream = Channel.from( ## Channel Object +### concat(...arguments) -> Channel + +When the `concat` method is called with zero or more arguments, it returns a +channel containing the values of the channel followed by the channel values of +each argument in order. + ### every(callbackfn[, thisArg]) -> async Boolean `callbackfn` should be a function that accepts one argument and returns a value diff --git a/lib/index.js b/lib/index.js index a2618e5..0d18cda 100644 --- a/lib/index.js +++ b/lib/index.js @@ -103,6 +103,26 @@ const Channel = function(length = 0) { const readOnly = Object.freeze( Object.assign(Object.create(prototype), { + concat: (...args) => { + const output = Channel(); + + (async () => { + await readOnly.forEach(output.push); + + for (let index = 0; index < args.length; index++) { + const arg = args[index]; + + await (Channel.isChannel(arg) + ? arg.forEach(output.push) + : output.push(arg)); + } + + output.close(); + })(); + + return output; + }, + every: async (callbackfn, thisArg) => { for (;;) { const value = await readOnly.shift(); diff --git a/test/index.js b/test/index.js index 87019c4..f6b01b9 100644 --- a/test/index.js +++ b/test/index.js @@ -213,6 +213,15 @@ describe(`Channel object`, function() { }); }); + it(`concat`, async function() { + assert.deepEqual( + await Channel.of(0, 1, 2) + .concat(Channel.of(3, 4, 5), 6) + .values(), + [0, 1, 2, 3, 4, 5, 6] + ); + }); + it(`every`, async function() { const even = number => number % 2 === 0; assert(!await Channel.of(0, 1, 2).every(even));