Skip to content

Commit

Permalink
refactor: fixes linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 12, 2019
1 parent 47e0465 commit 2edc4f5
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 38 deletions.
7 changes: 5 additions & 2 deletions src/compose/cancellable.ts
Expand Up @@ -11,8 +11,11 @@ function cancellable<T>(
promise: Promise<T>,
create?: boolean
): ICancellable & Promise<T>;
function cancellable<A, T>(promise: A & Promise<T>, create?: boolean) {
const p: A & ICancellable & Promise<T> = asNew(promise, create);
function cancellable<A, T>(
promise: A & Promise<T>,
create?: boolean
): ICancellable & Promise<T> {
const p = asNew(promise, create) as ICancellable & Promise<T>;

if (mark.get(p, 'cancellable')) return p;
mark.set(p, 'cancellable');
Expand Down
7 changes: 5 additions & 2 deletions src/compose/deferrable.ts
Expand Up @@ -12,8 +12,11 @@ function deferrable<T>(
promise: Promise<T>,
create?: boolean
): IDeferrable & Promise<T>;
function deferrable<A, T>(promise: A & Promise<T>, create?: boolean) {
const p: A & IDeferrable & Promise<T> = asNew(promise, create);
function deferrable<A, T>(
promise: A & Promise<T>,
create?: boolean
): IDeferrable & Promise<T> {
const p = asNew(promise, create) as IDeferrable & Promise<T>;

if (mark.get(p, 'deferrable')) return p;

Expand Down
6 changes: 2 additions & 4 deletions src/compose/delay.ts
@@ -1,6 +1,7 @@
import wait from '~/create/wait';
import { asNew, intercept } from '~/helpers';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default function delay(ms: number, delayRejection: boolean = false) {
const init = Date.now();

Expand All @@ -12,10 +13,7 @@ export default function delay(ms: number, delayRejection: boolean = false) {

function trunk<A, T>(promise: A & Promise<T>, create?: false): A & Promise<T>;
function trunk<T>(promise: Promise<T>, create?: boolean): Promise<T>;
function trunk<A, T>(
promise: A & Promise<T>,
create?: boolean
): A & Promise<T> {
function trunk<A, T>(promise: A & Promise<T>, create?: boolean): Promise<T> {
return intercept(asNew(promise, create), (px) => {
return px
.then((val) => delayer().then(() => val))
Expand Down
7 changes: 5 additions & 2 deletions src/compose/status.ts
Expand Up @@ -8,8 +8,11 @@ function status<A, T>(
create?: false
): A & IStatus & Promise<T>;
function status<T>(promise: Promise<T>, create?: boolean): IStatus & Promise<T>;
function status<A, T>(promise: A & Promise<T>, create?: boolean) {
const p: A & IStatus & Promise<T> = asNew(promise, create);
function status<A, T>(
promise: A & Promise<T>,
create?: boolean
): IStatus & Promise<T> {
const p = asNew(promise, create) as IStatus & Promise<T>;
if (mark.get(p, 'status')) return p;

mark.set(p, 'status');
Expand Down
7 changes: 5 additions & 2 deletions src/compose/timed.ts
Expand Up @@ -8,8 +8,11 @@ function timed<A, T>(
create?: false
): A & ITimed & Promise<T>;
function timed<T>(promise: Promise<T>, create?: boolean): ITimed & Promise<T>;
function timed<A, T>(promise: A & Promise<T>, create?: boolean) {
const p: A & ITimed & Promise<T> = asNew(promise, create);
function timed<A, T>(
promise: A & Promise<T>,
create?: boolean
): ITimed & Promise<T> {
const p = asNew(promise, create) as ITimed & Promise<T>;
if (mark.get(p, 'timed')) return p;
mark.set(p, 'timed');

Expand Down
6 changes: 5 additions & 1 deletion src/compose/timeout.ts
Expand Up @@ -3,6 +3,7 @@ import deferrable from './deferrable';
import { ICancellable, IDeferrable } from '~/types';
import { asNew, intercept } from '~/helpers';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default function timeout(ms: number, reason?: boolean | Error) {
function trunk<A, T>(
promise: A & Promise<T>,
Expand All @@ -12,7 +13,10 @@ export default function timeout(ms: number, reason?: boolean | Error) {
promise: Promise<T>,
create?: boolean
): ICancellable & IDeferrable & Promise<T>;
function trunk<A, T>(promise: A & Promise<T>, create?: boolean) {
function trunk<A, T>(
promise: A & Promise<T>,
create?: boolean
): ICancellable & IDeferrable & Promise<T> {
const shouldCancel = reason === undefined || reason === false;
const p = cancellable(deferrable(asNew(promise, create)));

Expand Down
6 changes: 3 additions & 3 deletions src/create/deferred.ts
@@ -1,14 +1,14 @@
import mark from '~/helpers/mark';
import { IDeferrable } from '~/types';

export default function deferred() {
export default function deferred(): IDeferrable & Promise<any> {
let _resolve: any;
let _reject: any;

const p: IDeferrable & Promise<any> = new Promise((resolve, reject) => {
const p = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
}) as any;
}) as IDeferrable & Promise<any>;

mark.set(p, 'deferrable');
p.resolve = _resolve;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/as-new.ts
@@ -1,3 +1,3 @@
export default function asNew<T>(p: Promise<T>, create?: boolean): any {
export default function asNew<T>(p: Promise<T>, create?: boolean): Promise<T> {
return create ? p.then((x) => x) : p;
}
2 changes: 1 addition & 1 deletion src/helpers/intercept.ts
Expand Up @@ -17,7 +17,7 @@ export default function intercept<A, T>(

p[INTERCEPT_SYMBOL] = intercept = [interceptFn];
const _then = promise.then;
const run = () => {
const run = (): Promise<any> => {
const res = p[RESPONSE_SYMBOL];
return (
res ||
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/mark.ts
@@ -1,17 +1,17 @@
const getDefaults = () => ({
const defaults = {
cancellable: false,
resetable: false,
deferrable: false,
timed: false,
status: false
});
};

export const MARK_SYMBOL = Symbol('mark');

export default {
set<A, T>(promise: A & Promise<T>, ...as: string[]): A {
if (!promise.hasOwnProperty(MARK_SYMBOL)) {
Object.assign(promise, { [MARK_SYMBOL]: getDefaults() });
Object.assign(promise, { [MARK_SYMBOL]: Object.assign({}, defaults) });
}

as.forEach((key) => ((promise as any)[MARK_SYMBOL][key] = true));
Expand Down
4 changes: 3 additions & 1 deletion src/utils/compose.ts
@@ -1,4 +1,6 @@
export default function compose(...fns: Array<(value: any) => any>) {
export default function compose(
...fns: Array<(value: any) => any>
): (value: any) => any {
if (fns.length === 0) return (arg: any) => arg;
if (fns.length === 1) return fns[0];

Expand Down
4 changes: 2 additions & 2 deletions src/utils/control.ts
Expand Up @@ -3,8 +3,8 @@ export default function control<T, A extends any[]>(
generator:
| ((...args: A) => IterableIterator<Promise<T>>)
| ((...args: A) => IterableIterator<T>)
) {
return async (...args: A): Promise<T> => {
): (...args: A) => Promise<T> {
return async (...args) => {
const iterator = generator(...args);
let value;
let done;
Expand Down
9 changes: 7 additions & 2 deletions test/parallel.test.ts
@@ -1,8 +1,13 @@
import parallel from '~/parallel';
import lazy from '~/create/lazy';

const createArr = (arr = [1, 2, 3, 4]) => arr.map((x) => Promise.resolve(x));
const createDelayedArr = (n = 50, ms = 1) => {
const createArr = (arr: number[] = [1, 2, 3, 4]): Array<Promise<number>> => {
return arr.map((x) => Promise.resolve(x));
};
const createDelayedArr = (
n: number = 50,
ms: number = 1
): Array<Promise<number>> => {
return Array(50)
.fill(0)
.map((_, i) => {
Expand Down
9 changes: 7 additions & 2 deletions test/series.test.ts
@@ -1,8 +1,13 @@
import series from '~/series';
import lazy from '~/create/lazy';

const createArr = (arr = [1, 2, 3, 4]) => arr.map((x) => Promise.resolve(x));
const createDelayedArr = (n = 50, ms = 1) => {
const createArr = (arr: number[] = [1, 2, 3, 4]): Array<Promise<number>> => {
return arr.map((x) => Promise.resolve(x));
};
const createDelayedArr = (
n: number = 50,
ms: number = 1
): Array<Promise<number>> => {
return Array(50)
.fill(0)
.map((_, i) => {
Expand Down
4 changes: 0 additions & 4 deletions test/utils/compose.test.ts
Expand Up @@ -3,19 +3,15 @@ import compose from '~/utils/compose';
test(`Doesn't throw on empty`, () => {
expect(() => compose()).not.toThrow();
});

test(`Returns function`, () => {
expect(typeof compose()).toBe('function');
});

test(`Works for 0 args`, () => {
expect(compose()(100)).toBe(100);
});

test(`Works for 1 arg`, () => {
expect(compose((x) => x * 2)(100)).toBe(200);
});

test(`Executes in order`, () => {
const fns = [
(x: number) => x / 2,
Expand Down
10 changes: 5 additions & 5 deletions test/utils/control.test.ts
@@ -1,7 +1,7 @@
import control from '~/utils/control';

test(`Succeeds with test => true`, async () => {
function* gen() {
function* gen(): IterableIterator<number> {
return 1;
}
const fn = control(() => true, gen);
Expand All @@ -11,7 +11,7 @@ test(`Succeeds with test => true`, async () => {
await expect(fnp()).resolves.toBe(1);
});
test(`Doesn't resolve with test => false`, async () => {
function* gen() {
function* gen(): IterableIterator<number> {
return 1;
}
const fn = control(() => false, gen);
Expand All @@ -25,7 +25,7 @@ test(`Doesn't resolve with test => false`, async () => {
expect(res).toEqual([false, false]);
});
test(`Rejects with test => Error`, async () => {
function* gen() {
function* gen(): IterableIterator<number> {
return 1;
}
const fn = control(() => Error(), gen);
Expand All @@ -35,7 +35,7 @@ test(`Rejects with test => Error`, async () => {
await expect(fnp()).rejects.toThrowError();
});
test(`Rejects with test throwing Error`, async () => {
function* gen() {
function* gen(): IterableIterator<number> {
return 1;
}
const fn = control(() => {
Expand All @@ -54,7 +54,7 @@ test(`Rejects with generator throwing Error`, async () => {
await expect(fn()).rejects.toThrowError();
});
test(`Yields and succeeds with test => true`, async () => {
function* gen(n = 10) {
function* gen(n = 10): IterableIterator<Promise<number> | number> {
n = yield Promise.resolve(n);
n = n * (yield Promise.resolve(10));
n = yield 10 * n;
Expand Down
2 changes: 1 addition & 1 deletion test/utils/is-promise.test.ts
Expand Up @@ -8,7 +8,7 @@ test(`Returns true for promise`, () => {

test(`Returns true for thenables`, () => {
const a = { then() {} };
const b = () => {};
const b = (): void => {};
b.then = () => {};

expect(isPromise(a)).toBe(true);
Expand Down

0 comments on commit 2edc4f5

Please sign in to comment.