Add every
.
This commit is contained in:
parent
8da51305eb
commit
17a2688291
3 changed files with 36 additions and 0 deletions
16
README.md
16
README.md
|
@ -180,6 +180,22 @@ Create a new `Channel` from an iterable or a [Node.js readable stream](https://n
|
|||
|
||||
### Channel Object
|
||||
|
||||
#### every (callbackfn[, thisArg]) -> async Boolean
|
||||
|
||||
`callbackfn` should be a function that accepts one argument and returns a value
|
||||
that is coercible to the Boolean values `true` or `false`. `every` calls
|
||||
`callbackfn` once for each value present in the channel until it finds one where
|
||||
`callbackfn` returns `false`. If such a value is found, every immediately
|
||||
returns `false`. Otherwise, if `callbackfn` returned `true` for all elements,
|
||||
`every` will return `true`.
|
||||
|
||||
If a `thisArg` parameter is provided, it will be used as the `this` value for
|
||||
each invocation of `callbackfn`. If it is not provided, `undefined` is used
|
||||
instead.
|
||||
|
||||
Unlike in the Array version of `every`, `callbackfn` is called with only one
|
||||
argument.
|
||||
|
||||
#### filter(callbackfn[, thisArg]) -> Channel
|
||||
#### forEach(callbackfn[, thisArg]) -> async
|
||||
|
||||
|
|
14
lib/index.js
14
lib/index.js
|
@ -103,6 +103,20 @@ const Channel = function (bufferLength = 0) {
|
|||
}
|
||||
|
||||
const readOnly = Object.freeze({
|
||||
every: async (callbackfn, thisArg) => {
|
||||
for (;;) {
|
||||
const value = await readOnly.shift()
|
||||
|
||||
if (value === undefined) {
|
||||
return true
|
||||
} else {
|
||||
if (!callbackfn.call(thisArg, value)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
filter: (callbackfn, thisArg) => {
|
||||
const output = Channel()
|
||||
|
||||
|
|
|
@ -180,6 +180,12 @@ describe(`Channel object`, function () {
|
|||
})
|
||||
})
|
||||
|
||||
it(`every`, async function () {
|
||||
const even = (number) => number % 2 === 0
|
||||
assert(!await Channel.of(0, 1, 2).every(even))
|
||||
assert(await Channel.of(0, 2, 4).every(even))
|
||||
})
|
||||
|
||||
it(`filter`, async function () {
|
||||
assert.deepEqual(
|
||||
await toArray(Channel.of(0, 1, 2, 3, 4, 5)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue