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