diff --git a/doc/API.md b/doc/API.md index 29c1032..8cb31e6 100644 --- a/doc/API.md +++ b/doc/API.md @@ -18,6 +18,7 @@ - [filter(callbackfn[, thisArg]) -> Channel](#filtercallbackfn-thisarg---channel) - [forEach(callbackfn[, thisArg]) -> async](#foreachcallbackfn-thisarg---async) - [join(separator) -> async String](#joinseparator---async-string) + - [length](#length) - [map(callbackfn[, thisArg]) -> Channel](#mapcallbackfn-thisarg---channel) - [push(value) -> async bufferLength](#pushvalue---async-bufferlength) - [reduce(callbackfn[, initialValue])](#reducecallbackfn-initialvalue) @@ -232,6 +233,10 @@ The values of the channel are converted to Strings, and these Strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator. +### length + +The length of the channel's buffer. + ### map(callbackfn[, thisArg]) -> Channel `callbackfn` should be a function that accepts one argument. `map` calls diff --git a/lib/index.js b/lib/index.js index f90b03b..7037649 100644 --- a/lib/index.js +++ b/lib/index.js @@ -254,7 +254,11 @@ const Channel = function(bufferLength = 0) { } }); - Object.defineProperty(readOnly, `value`, { get: () => lastValue }); + Object.defineProperties(readOnly, { + length: { get: () => bufferLength }, + value: { get: () => lastValue } + }); + Object.freeze(readOnly); const writeOnly = Object.freeze({ diff --git a/test/index.js b/test/index.js index 90d1301..2b6817f 100644 --- a/test/index.js +++ b/test/index.js @@ -217,6 +217,10 @@ describe(`Channel object`, function() { assert.equal(await Channel.of(`a`, `b`, `c`).join(), `a,b,c`); }); + it(`length`, function() { + assert.equal(Channel(42).length, 42); + }); + it(`map`, async function() { assert.deepEqual( await Channel.of(`a`, `b`, `c`)