From f07e8496623e07d9e6ca2da521ed2a48a2ab85d4 Mon Sep 17 00:00:00 2001 From: David Braun Date: Wed, 18 Oct 2017 14:28:01 -0400 Subject: [PATCH] Add `toString`. --- doc/API.md | 5 +++++ lib/index.js | 4 +++- test/index.js | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/API.md b/doc/API.md index 768c555..2749d23 100644 --- a/doc/API.md +++ b/doc/API.md @@ -24,6 +24,7 @@ - [shift() -> async](#shift---async) - [slice(start[, end]) -> Channel](#slicestart-end---channel) - [some(callbackfn[, thisArg])](#somecallbackfn-thisarg) + - [toString()](#tostring) - [Functional API](#functional-api) @@ -305,6 +306,10 @@ Unlike in `Array`'s method, `callbackfn` is called with only one argument. `some` acts like the "exists" quantifier in mathematics. In particular, for an empty channel, it returns `false`. +### toString() -> String + +Return `"Channel(n)"` where `n` is the length of the buffer. + # Functional API There is a parallel API to support functional-style programming. Every channel diff --git a/lib/index.js b/lib/index.js index aa5664f..23418c1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -239,7 +239,9 @@ const Channel = function(bufferLength = 0) { } } } - } + }, + + toString: () => `Channel(${bufferLength})` }); Object.defineProperty(readOnly, `value`, { get: () => lastValue }); diff --git a/test/index.js b/test/index.js index f3eb702..6da61a6 100644 --- a/test/index.js +++ b/test/index.js @@ -356,6 +356,10 @@ describe(`Channel object`, function() { assert(!await Channel.of(1, 3, 5).some(even)); }); + it(`toString`, function() { + assert.equal(Channel(10).toString(), `Channel(10)`); + }); + it(`value`, async function() { const channel = Channel(); (async () => {