diff --git a/lib/index.js b/lib/index.js index f33180d..595c76d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,21 +3,21 @@ require(`setimmediate`); // An order represents a pending push or shift. -const Order = channel => { +const Order = (channel) => { let order; const preonFulfilleds = []; const promise = new Promise((resolve, reject) => { order = { - resolve: value => { - preonFulfilleds.forEach(preonFulfilled => { + resolve: (value) => { + preonFulfilleds.forEach((preonFulfilled) => { preonFulfilled(value); }); resolve(value); }, - reject + reject, }; }); @@ -28,9 +28,9 @@ const Order = channel => { channel, - prethen: onFulfilled => { + prethen: (onFulfilled) => { preonFulfilleds.push(onFulfilled); - } + }, }); return { order, promise }; @@ -46,7 +46,7 @@ const Channel = function(length = 0) { const pushes = []; const shifts = []; - const matchPushesAndShifts = index => { + const matchPushesAndShifts = (index) => { while (index.push < pushes.length && index.shift < shifts.length) { const push = pushes[index.push]; const shift = shifts[index.shift]; @@ -67,7 +67,7 @@ const Channel = function(length = 0) { }; // Resolve push promises up to the end of the buffer. - const resolveBufferedPushes = index => { + const resolveBufferedPushes = (index) => { for ( let resolvedIndex = index.push + buffered; resolvedIndex < pushes.length && buffered < length; @@ -82,7 +82,7 @@ const Channel = function(length = 0) { } }; - const resolveClosedShifts = index => { + const resolveClosedShifts = (index) => { for (; index.shift < shifts.length; index.shift++) { const { cancelled, resolve } = shifts[index.shift]; @@ -160,7 +160,7 @@ const Channel = function(length = 0) { const output = Channel(); (async () => { - await readOnly.forEach(async value => { + await readOnly.forEach(async (value) => { if (await callbackfn.call(thisArg, value)) { await output.push(value); } @@ -172,11 +172,11 @@ const Channel = function(length = 0) { return output; }, - flat: depth => { + flat: (depth) => { const output = Channel(); (async () => { - await readOnly.forEach(async value => { + await readOnly.forEach(async (value) => { if (Channel.isChannel(value)) { const input = depth > 1 ? value.flat(depth - 1) : value; await input.forEach(output.push); @@ -206,13 +206,13 @@ const Channel = function(length = 0) { } }, - join: async separator => (await readOnly.values()).join(separator), + join: async (separator) => (await readOnly.values()).join(separator), map: (callbackfn, thisArg) => { const output = Channel(); (async () => { - await readOnly.forEach(value => + await readOnly.forEach((value) => output.push(callbackfn.call(thisArg, value)) ); @@ -228,7 +228,7 @@ const Channel = function(length = 0) { let previousValue = initialValue[0]; let previousValueDefined = initialValue.length > 0; - await readOnly.forEach(currentValue => { + await readOnly.forEach((currentValue) => { if (previousValueDefined) { previousValue = callbackfn(previousValue, currentValue); } else { @@ -301,12 +301,12 @@ const Channel = function(length = 0) { values: async () => { const array = []; - await readOnly.forEach(item => { + await readOnly.forEach((item) => { array.push(item); }); return array; - } + }, }) ); @@ -355,7 +355,7 @@ const Channel = function(length = 0) { return promise; }, - writeOnly: () => writeOnly + writeOnly: () => writeOnly, }) ); @@ -364,14 +364,14 @@ const Channel = function(length = 0) { ); }; -Channel.all = channels => { +Channel.all = (channels) => { const output = Channel(); (async () => { for (;;) { const values = await Promise.all(channels.map(Channel.shift)); - if (values.every(value => value === undefined)) { + if (values.every((value) => value === undefined)) { break; } else { await output.push(values); @@ -436,23 +436,23 @@ Channel.from = (values, mapfn, thisArg) => { Channel.of = (...values) => Channel.from(values); -Channel.isChannel = value => +Channel.isChannel = (value) => value !== undefined && value !== null && Object.getPrototypeOf(value) === prototype; -Channel.select = methodPromises => { +Channel.select = (methodPromises) => { if (!Array.isArray(methodPromises)) { throw new TypeError(`Channel.select: Argument must be an array.`); } const selectPromise = new Promise((resolve, reject) => { - methodPromises.forEach(async promise => { + methodPromises.forEach(async (promise) => { try { promise.prethen(() => { // We've been given a heads-up that this method will complete first // so cancel the other method calls. - methodPromises.forEach(other => { + methodPromises.forEach((other) => { if (other !== promise) { other.cancel(); } @@ -477,7 +477,7 @@ Channel.select = methodPromises => { }); return Object.assign(selectPromise, { - cancel: () => methodPromises.forEach(promise => promise.cancel()) + cancel: () => methodPromises.forEach((promise) => promise.cancel()), }); }; @@ -490,15 +490,15 @@ Channel.select = methodPromises => { const channel = Channel(); const methods = Object.keys(channel).filter( - method => typeof channel[method] === `function` + (method) => typeof channel[method] === `function` ); const arities = { reduce: 2, - slice: 2 + slice: 2, }; -methods.forEach(method => { +methods.forEach((method) => { const bound = function(...args) { const arity = arities[method] || channel[method].length; diff --git a/test/index.js b/test/index.js index 0aac8f0..7f84cd0 100644 --- a/test/index.js +++ b/test/index.js @@ -73,7 +73,7 @@ describe(`Channel function`, function() { it(`with mapfn`, async function() { assert.deepEqual( - await Channel.from([`a`, `b`, `c`], value => + await Channel.from([`a`, `b`, `c`], (value) => value.toUpperCase() ).values(), [`A`, `B`, `C`] @@ -242,7 +242,7 @@ describe(`Channel object`, function() { }); it(`every`, async function() { - const even = number => number % 2 === 0; + const even = (number) => number % 2 === 0; assert(!(await Channel.of(0, 1, 2).every(even))); assert(await Channel.of(0, 2, 4).every(even)); }); @@ -250,7 +250,7 @@ describe(`Channel object`, function() { it(`filter`, async function() { assert.deepEqual( await Channel.of(0, 1, 2, 3, 4, 5) - .filter(async value => value % 2 !== 0) + .filter(async (value) => value % 2 !== 0) .values(), [1, 3, 5] ); @@ -274,14 +274,14 @@ describe(`Channel object`, function() { it(`flatMap`, async function() { assert.deepEqual( await Channel.of(1, 2, 3, 4) - .flatMap(x => Channel.of(x * 2)) + .flatMap((x) => Channel.of(x * 2)) .values(), [2, 4, 6, 8] ); assert.deepEqual( await Channel.of(`it's Sunny in`, ``, `California`) - .flatMap(x => Channel.from(x.split(` `))) + .flatMap((x) => Channel.from(x.split(` `))) .values(), [`it's`, `Sunny`, `in`, ``, `California`] ); @@ -289,7 +289,7 @@ describe(`Channel object`, function() { it(`forEach`, async function() { const output = []; - await Channel.of(0, 1, 2).forEach(value => output.push(value)); + await Channel.of(0, 1, 2).forEach((value) => output.push(value)); assert.deepEqual(output, [0, 1, 2]); }); @@ -307,7 +307,7 @@ describe(`Channel object`, function() { await channel.push(`b`); await channel.push(`c`); await channel.close(); - const mapped = channel.map(value => value.toUpperCase()); + const mapped = channel.map((value) => value.toUpperCase()); assert.deepEqual(await mapped.values(), [`A`, `B`, `C`]); }); @@ -443,7 +443,7 @@ describe(`Channel object`, function() { }); it(`some`, async function() { - const even = value => value % 2 === 0; + const even = (value) => value % 2 === 0; const channel = Channel.of(0, 1, 2); assert(await channel.some(even)); assert.deepEqual(await channel.values(), [1, 2]);