Add values.

This commit is contained in:
David Braun 2017-10-18 14:38:35 -04:00
parent f07e849662
commit b9901df29a
3 changed files with 46 additions and 31 deletions

View file

@ -25,6 +25,7 @@
- [slice(start[, end]) -> Channel](#slicestart-end---channel) - [slice(start[, end]) -> Channel](#slicestart-end---channel)
- [some(callbackfn[, thisArg])](#somecallbackfn-thisarg) - [some(callbackfn[, thisArg])](#somecallbackfn-thisarg)
- [toString()](#tostring) - [toString()](#tostring)
- [values() -> async iterator](#values---async-iterator)
- [Functional API](#functional-api) - [Functional API](#functional-api)
<!-- /TOC --> <!-- /TOC -->
@ -310,6 +311,10 @@ empty channel, it returns `false`.
Return `"Channel(n)"` where `n` is the length of the buffer. Return `"Channel(n)"` where `n` is the length of the buffer.
### values() -> async iterator
Return an iterator over the values in the channel.
# Functional API # Functional API
There is a parallel API to support functional-style programming. Every channel There is a parallel API to support functional-style programming. Every channel

View file

@ -241,7 +241,17 @@ const Channel = function(bufferLength = 0) {
} }
}, },
toString: () => `Channel(${bufferLength})` toString: () => `Channel(${bufferLength})`,
values: async () => {
const array = [];
await readOnly.forEach(item => {
array.push(item);
});
return array;
}
}); });
Object.defineProperty(readOnly, `value`, { get: () => lastValue }); Object.defineProperty(readOnly, `value`, { get: () => lastValue });

View file

@ -18,16 +18,6 @@ const assertRejects = async (callback, reason) => {
assert.fail(null, reason, `Missing expected rejection.`); assert.fail(null, reason, `Missing expected rejection.`);
}; };
const toArray = async channel => {
const array = [];
await channel.forEach(item => {
array.push(item);
});
return array;
};
describe(`Channel`, function() { describe(`Channel`, function() {
it(`allows the use of new`, function() { it(`allows the use of new`, function() {
return new Channel(); return new Channel();
@ -58,7 +48,7 @@ describe(`Channel`, function() {
describe(`from`, function() { describe(`from`, function() {
it(`iterable`, async function() { it(`iterable`, async function() {
assert.deepEqual(await toArray(Channel.from([0, 1, 2])), [0, 1, 2]); assert.deepEqual(await Channel.from([0, 1, 2]).values(), [0, 1, 2]);
}); });
it(`Node.js's stream.readOnly`, async function() { it(`Node.js's stream.readOnly`, async function() {
@ -66,7 +56,7 @@ describe(`Channel`, function() {
readOnly.write(0); readOnly.write(0);
readOnly.write(1); readOnly.write(1);
readOnly.end(2); readOnly.end(2);
assert.deepEqual(await toArray(Channel.from(readOnly)), [0, 1, 2]); assert.deepEqual(await Channel.from(readOnly).values(), [0, 1, 2]);
}); });
}); });
@ -76,7 +66,7 @@ describe(`Channel`, function() {
}); });
it(`of`, async function() { it(`of`, async function() {
assert.deepEqual(await toArray(Channel.of(0, 1, 2)), [0, 1, 2]); assert.deepEqual(await Channel.of(0, 1, 2).values(), [0, 1, 2]);
}); });
describe(`select`, function() { describe(`select`, function() {
@ -144,21 +134,21 @@ describe(`functional interface`, async function() {
describe(`slice`, function() { describe(`slice`, function() {
it(`full application`, async function() { it(`full application`, async function() {
assert.deepEqual( assert.deepEqual(
await toArray(Channel.slice(1, 4, Channel.of(0, 1, 2, 3, 4))), await Channel.slice(1, 4, Channel.of(0, 1, 2, 3, 4)).values(),
[1, 2, 3] [1, 2, 3]
); );
}); });
it(`single argument application`, async function() { it(`single argument application`, async function() {
assert.deepEqual( assert.deepEqual(
await toArray(Channel.slice(1)(4)(Channel.of(0, 1, 2, 3, 4))), await Channel.slice(1)(4)(Channel.of(0, 1, 2, 3, 4)).values(),
[1, 2, 3] [1, 2, 3]
); );
}); });
it(`double argument application`, async function() { it(`double argument application`, async function() {
assert.deepEqual( assert.deepEqual(
await toArray(Channel.slice(1, 4)(Channel.of(0, 1, 2, 3, 4))), await Channel.slice(1, 4)(Channel.of(0, 1, 2, 3, 4)).values(),
[1, 2, 3] [1, 2, 3]
); );
}); });
@ -200,9 +190,9 @@ describe(`Channel object`, function() {
it(`filter`, async function() { it(`filter`, async function() {
assert.deepEqual( assert.deepEqual(
await toArray( await Channel.of(0, 1, 2, 3, 4, 5)
Channel.of(0, 1, 2, 3, 4, 5).filter(value => value % 2 !== 0) .filter(value => value % 2 !== 0)
), .values(),
[1, 3, 5] [1, 3, 5]
); );
}); });
@ -214,7 +204,7 @@ describe(`Channel object`, function() {
output.close(); output.close();
})(); })();
assert.deepEqual(await toArray(output), [0, 1, 2]); assert.deepEqual(await output.values(), [0, 1, 2]);
}); });
it(`join`, async function() { it(`join`, async function() {
@ -223,9 +213,9 @@ describe(`Channel object`, function() {
it(`map`, async function() { it(`map`, async function() {
assert.deepEqual( assert.deepEqual(
await toArray( await Channel.of(`a`, `b`, `c`)
Channel.of(`a`, `b`, `c`).map(value => value.toUpperCase()) .map(value => value.toUpperCase())
), .values(),
[`A`, `B`, `C`] [`A`, `B`, `C`]
); );
}); });
@ -336,15 +326,21 @@ describe(`Channel object`, function() {
describe(`slice`, function() { describe(`slice`, function() {
it(`start`, async function() { it(`start`, async function() {
assert.deepEqual(await toArray(Channel.of(0, 1, 2).slice(1)), [1, 2]); assert.deepEqual(
await Channel.of(0, 1, 2)
.slice(1)
.values(),
[1, 2]
);
}); });
it(`end`, async function() { it(`end`, async function() {
assert.deepEqual(await toArray(Channel.of(0, 1, 2, 3, 4).slice(1, 4)), [ assert.deepEqual(
1, await Channel.of(0, 1, 2, 3, 4)
2, .slice(1, 4)
3 .values(),
]); [1, 2, 3]
);
}); });
}); });
@ -352,7 +348,7 @@ describe(`Channel object`, function() {
const even = value => value % 2 === 0; const even = value => value % 2 === 0;
const channel = Channel.of(0, 1, 2); const channel = Channel.of(0, 1, 2);
assert(await channel.some(even)); assert(await channel.some(even));
assert.deepEqual(await toArray(channel), [1, 2]); assert.deepEqual(await channel.values(), [1, 2]);
assert(!await Channel.of(1, 3, 5).some(even)); assert(!await Channel.of(1, 3, 5).some(even));
}); });
@ -378,6 +374,10 @@ describe(`Channel object`, function() {
assert.equal(channel.value, undefined); assert.equal(channel.value, undefined);
}); });
it(`values`, async function() {
assert.deepEqual(await Channel.of(0, 1, 2).values(), [0, 1, 2]);
});
describe(`writeOnly`, function() { describe(`writeOnly`, function() {
it(`provides only write methods`, async function() { it(`provides only write methods`, async function() {
const channel = Channel(); const channel = Channel();