Add every.

This commit is contained in:
David Braun 2017-10-11 04:46:12 -04:00
parent 8da51305eb
commit 17a2688291
No known key found for this signature in database
GPG key ID: 5694EEC4D129BDCF
3 changed files with 36 additions and 0 deletions

View file

@ -180,6 +180,22 @@ Create a new `Channel` from an iterable or a [Node.js readable stream](https://n
### Channel Object ### 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 #### filter(callbackfn[, thisArg]) -> Channel
#### forEach(callbackfn[, thisArg]) -> async #### forEach(callbackfn[, thisArg]) -> async

View file

@ -103,6 +103,20 @@ const Channel = function (bufferLength = 0) {
} }
const readOnly = Object.freeze({ 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) => { filter: (callbackfn, thisArg) => {
const output = Channel() const output = Channel()

View file

@ -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 () { it(`filter`, async function () {
assert.deepEqual( assert.deepEqual(
await toArray(Channel.of(0, 1, 2, 3, 4, 5) await toArray(Channel.of(0, 1, 2, 3, 4, 5)