Skip to content

Commit 1b19e44

Browse files
authored
Merge pull request #30 from FrontEndDev-org/feat/v1.x
Feat/v1.x
2 parents 53a8da0 + e15a4b5 commit 1b19e44

12 files changed

+218
-39
lines changed

src/callbackCurry.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
1-
export type Callback<T> = (err: null | undefined | Error, res: T) => any;
1+
export type Callback<T = void> = (err: null | undefined | void | Error, res: T) => any;
22

3-
export type CallbackFunction0<T> = (callback: Callback<T>) => any;
4-
export type CallbackFunction1<A, T> = (a: A, callback: Callback<T>) => any;
5-
export type CallbackFunction2<A, B, T> = (a: A, b: B, callback: Callback<T>) => any;
6-
export type CallbackFunction3<A, B, C, T> = (a: A, b: B, c: C, callback: Callback<T>) => any;
7-
export type CallbackFunction4<A, B, C, D, T> = (a: A, b: B, c: C, d: D, callback: Callback<T>) => any;
8-
export type CallbackFunction5<A, B, C, D, E, T> = (a: A, b: B, c: C, d: D, e: E, callback: Callback<T>) => any;
9-
export type CallbackFunction6<A, B, C, D, E, F, T> = (a: A, b: B, c: C, d: D, e: E, f: F, callback: Callback<T>) => any;
3+
export type CallbackFunction0<T = void> = (callback: Callback<T>) => any;
4+
export type CallbackFunction1<A, T = void> = (a: A, callback: Callback<T>) => any;
5+
export type CallbackFunction2<A, B, T = void> = (a: A, b: B, callback: Callback<T>) => any;
6+
export type CallbackFunction3<A, B, C, T = void> = (a: A, b: B, c: C, callback: Callback<T>) => any;
7+
export type CallbackFunction4<A, B, C, D, T = void> = (a: A, b: B, c: C, d: D, callback: Callback<T>) => any;
8+
export type CallbackFunction5<A, B, C, D, E, T = void> = (a: A, b: B, c: C, d: D, e: E, callback: Callback<T>) => any;
9+
export type CallbackFunction6<A, B, C, D, E, F, T = void> = (
10+
a: A,
11+
b: B,
12+
c: C,
13+
d: D,
14+
e: E,
15+
f: F,
16+
callback: Callback<T>
17+
) => any;
1018

1119
export type CallbackCurried<T> = (callback: Callback<T>) => any;
1220

13-
export function callbackCurry<T>(cf: CallbackFunction0<T>): CallbackCurried<T>;
14-
export function callbackCurry<A, T>(cf: CallbackFunction1<A, T>, a: A): CallbackCurried<T>;
15-
export function callbackCurry<A, B, T>(cf: CallbackFunction2<A, B, T>, a: A, b: B): CallbackCurried<T>;
16-
export function callbackCurry<A, B, C, T>(cf: CallbackFunction3<A, B, C, T>, a: A, b: B, c: C): CallbackCurried<T>;
17-
export function callbackCurry<A, B, C, D, T>(
21+
export function callbackCurry<T = void>(cf: CallbackFunction0<T>): CallbackCurried<T>;
22+
export function callbackCurry<A, T = void>(cf: CallbackFunction1<A, T>, a: A): CallbackCurried<T>;
23+
export function callbackCurry<A, B, T = void>(cf: CallbackFunction2<A, B, T>, a: A, b: B): CallbackCurried<T>;
24+
export function callbackCurry<A, B, C, T = void>(
25+
cf: CallbackFunction3<A, B, C, T>,
26+
a: A,
27+
b: B,
28+
c: C
29+
): CallbackCurried<T>;
30+
export function callbackCurry<A, B, C, D, T = void>(
1831
cf: CallbackFunction4<A, B, C, D, T>,
1932
a: A,
2033
b: B,
2134
c: C,
2235
d: D
2336
): CallbackCurried<T>;
24-
export function callbackCurry<A, B, C, D, E, T>(
37+
export function callbackCurry<A, B, C, D, E, T = void>(
2538
cf: CallbackFunction5<A, B, C, D, E, T>,
2639
a: A,
2740
b: B,
2841
c: C,
2942
d: D,
3043
e: E
3144
): CallbackCurried<T>;
32-
export function callbackCurry<A, B, C, D, E, F, T>(
45+
export function callbackCurry<A, B, C, D, E, F, T = void>(
3346
cf: CallbackFunction6<A, B, C, D, E, F, T>,
3447
a: A,
3548
b: B,

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export { pkgName, pkgVersion } from './const';
1+
export * from './const';
22
export * from './callbackCurry';
33
export * from './errorNormalize';
44
export * from './tryFunction';
55
export * from './tryCallback';
66
export * from './tryPromise';
77
export * from './tryFlatten';
8+
export * from './types';
File renamed without changes.

src/tryCallback.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@ import type {
99
} from './callbackCurry';
1010
import { callbackCurry } from './callbackCurry';
1111
import { errorNormalize } from './errorNormalize';
12-
import type { FlattenReturn } from './types/return';
12+
import type { FlattenReturn } from './types';
1313

14-
export function tryCallback<T>(cf: CallbackFunction0<T>): Promise<FlattenReturn<T>>;
15-
export function tryCallback<A, T>(cf: CallbackFunction1<A, T>, a: A): Promise<FlattenReturn<T>>;
16-
export function tryCallback<A, B, T>(cf: CallbackFunction2<A, B, T>, a: A, b: B): Promise<FlattenReturn<T>>;
17-
export function tryCallback<A, B, C, T>(cf: CallbackFunction3<A, B, C, T>, a: A, b: B, c: C): Promise<FlattenReturn<T>>;
18-
export function tryCallback<A, B, C, D, T>(
19-
cf: CallbackFunction4<A, B, C, D, T>,
14+
export function tryCallback(cf: CallbackFunction0): Promise<FlattenReturn>;
15+
export function tryCallback<T = void>(cf: CallbackFunction0<T>): Promise<FlattenReturn<T>>;
16+
export function tryCallback<A, T = void>(cf: CallbackFunction1<A, T>, a: A): Promise<FlattenReturn<T>>;
17+
export function tryCallback<A, B, T = void>(cf: CallbackFunction2<A, B, T>, a: A, b: B): Promise<FlattenReturn<T>>;
18+
export function tryCallback<A, B, C, T = void>(
19+
cf: CallbackFunction3<A, B, C, T>,
20+
a: A,
21+
b: B,
22+
c: C
23+
): Promise<FlattenReturn<T>>;
24+
export function tryCallback<A, B, C, D, T = void>(
25+
cf: CallbackFunction4<A, B, C, D>,
2026
a: A,
2127
b: B,
2228
c: C,
2329
d: D
2430
): Promise<FlattenReturn<T>>;
25-
export function tryCallback<A, B, C, D, E, T>(
31+
export function tryCallback<A, B, C, D, E, T = void>(
2632
cf: CallbackFunction5<A, B, C, D, E, T>,
2733
a: A,
2834
b: B,
2935
c: C,
3036
d: D,
3137
e: E
3238
): Promise<FlattenReturn<T>>;
33-
export function tryCallback<A, B, C, D, E, F, T>(
39+
export function tryCallback<A, B, C, D, E, F, T = void>(
3440
cf: CallbackFunction6<A, B, C, D, E, F, T>,
3541
a: A,
3642
b: B,

src/tryFlatten.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { CallbackFunction0 } from './callbackCurry';
22
import { tryCallback } from './tryCallback';
33
import { tryPromise } from './tryPromise';
44
import { tryFunction, type SyncFunction } from './tryFunction';
5-
import type { FlattenReturn } from './types/return';
5+
import type { FlattenReturn } from './types';
66

77
export type FlattenAble<T> = SyncFunction<T> | CallbackFunction0<T> | PromiseLike<T>;
88

src/tryFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { errorNormalize } from './errorNormalize';
2-
import type { FlattenReturn } from './types/return';
2+
import type { FlattenReturn } from './types';
33

44
export type SyncFunction<T> = () => T;
55

src/tryPromise.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { errorNormalize } from './errorNormalize';
2-
import type { FlattenReturn } from './types/return';
2+
import type { FlattenReturn } from './types';
33

44
export function tryPromise<T>(promise: PromiseLike<T>): PromiseLike<FlattenReturn<T>> {
55
return promise.then(

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type FlattenReturn<T = void> = readonly [Error, undefined] | readonly [null, T];

src/types/return.d.ts

-1
This file was deleted.

test/callbackCurry.test.ts

+41-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import { callbackCurry } from '../src/callbackCurry';
2-
import type { Callback } from '../src/callbackCurry';
1+
import { callbackCurry } from '../src';
2+
import type { Callback } from '../src';
33
import { assertNumber } from './helpers';
44

5-
test('0', () => {
5+
test('0 input + 0 result', () => {
6+
const cf = (cb: Callback) => {
7+
cb(null);
8+
};
9+
const fn = vi.fn();
10+
const cc = callbackCurry(cf);
11+
cc((err, res) => {
12+
fn(res);
13+
});
14+
expect(fn.mock.calls[0][0]).toBe(undefined);
15+
});
16+
17+
test('0 input + 1 result', () => {
618
const cf = (cb: Callback<number>) => {
719
cb(null, 0);
820
};
@@ -15,7 +27,19 @@ test('0', () => {
1527
expect(fn.mock.calls[0][0]).toBe(0);
1628
});
1729

18-
test('1', async () => {
30+
test('1 input + 0 result', async () => {
31+
const cf = (a: string, cb: Callback) => {
32+
cb();
33+
};
34+
const fn = vi.fn();
35+
const cc = callbackCurry(cf, '');
36+
cc((err) => {
37+
fn();
38+
});
39+
expect(fn.mock.calls[0][0]).toBe(undefined);
40+
});
41+
42+
test('1 input + 1 result', async () => {
1943
const cf = (a: string, cb: Callback<number>) => {
2044
cb(null, 0);
2145
};
@@ -28,7 +52,19 @@ test('1', async () => {
2852
expect(fn.mock.calls[0][0]).toBe(0);
2953
});
3054

31-
test('', async () => {
55+
test('2 input + 0 result', async () => {
56+
const cf = (a: string, b: 'ba' | 'bb', cb: Callback) => {
57+
cb(undefined);
58+
};
59+
const fn = vi.fn();
60+
const cc = callbackCurry(cf, '', 'ba');
61+
cc((err) => {
62+
fn();
63+
});
64+
expect(fn.mock.calls[0][0]).toBe(undefined);
65+
});
66+
67+
test('2 input + 1 result', async () => {
3268
const cf = (a: string, b: 'ba' | 'bb', cb: Callback<number>) => {
3369
cb(null, 0);
3470
};

test/tryCallback.test.ts

+127-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,47 @@ import type { CallbackFunction0, CallbackFunction1, CallbackFunction2 } from '..
33
import { tryCallback } from '../src';
44
import { assertError, assertNull, assertNumber, assertUndefined } from './helpers';
55

6-
describe('tryCallbackFlatten 0', () => {
6+
describe('tryCallbackFlatten 0 input + 0 result', () => {
7+
test('resolved', async () => {
8+
const callbackFunction = (callback: (err: Error | null) => void) => {
9+
setTimeout(() => {
10+
callback(null);
11+
});
12+
};
13+
const [err, res] = await tryCallback(callbackFunction);
14+
15+
if (err) {
16+
assertError(err);
17+
} else {
18+
assertNull(err);
19+
}
20+
21+
expect(err).toBe(null);
22+
expect(res).toBe(undefined);
23+
});
24+
25+
test('rejected', async () => {
26+
const callbackFunction = (callback: (err: Error | null) => void) => {
27+
setTimeout(() => {
28+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
29+
// @ts-ignore
30+
callback(new Error('1'));
31+
});
32+
};
33+
const [err, res] = await tryCallback(callbackFunction);
34+
35+
if (err) {
36+
assertError(err);
37+
} else {
38+
assertNull(err);
39+
}
40+
41+
expect(err?.message).toBe('1');
42+
expect(res).toBeUndefined();
43+
});
44+
});
45+
46+
describe('tryCallbackFlatten 0 input + 1 result', () => {
747
test('resolved', async () => {
848
const callbackFunction: CallbackFunction0<number> = (callback) => {
949
setTimeout(() => {
@@ -47,7 +87,49 @@ describe('tryCallbackFlatten 0', () => {
4787
});
4888
});
4989

50-
describe('tryCallbackFlatten 1', () => {
90+
describe('tryCallbackFlatten 1 input + 0 result', () => {
91+
test('resolved', async () => {
92+
const callbackFunction: CallbackFunction1<string> = (a, callback) => {
93+
setTimeout(() => {
94+
callback(null);
95+
});
96+
};
97+
const [err, res] = await tryCallback(callbackFunction, '');
98+
99+
if (err) {
100+
assertError(err);
101+
assertUndefined(res);
102+
} else {
103+
assertNull(err);
104+
}
105+
106+
expect(err).toBe(null);
107+
expect(res).toBeUndefined();
108+
});
109+
110+
test('rejected', async () => {
111+
const callbackFunction: CallbackFunction1<string> = (a, callback) => {
112+
setTimeout(() => {
113+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
114+
// @ts-ignore
115+
callback(new Error('1'));
116+
});
117+
};
118+
const [err, res] = await tryCallback(callbackFunction, '');
119+
120+
if (err) {
121+
assertError(err);
122+
assertUndefined(res);
123+
} else {
124+
assertNull(err);
125+
}
126+
127+
expect(err?.message).toBe('1');
128+
expect(res).toBeUndefined();
129+
});
130+
});
131+
132+
describe('tryCallbackFlatten 1 input + 1 result', () => {
51133
test('resolved', async () => {
52134
const callbackFunction: CallbackFunction1<string, number> = (a, callback) => {
53135
setTimeout(() => {
@@ -91,7 +173,49 @@ describe('tryCallbackFlatten 1', () => {
91173
});
92174
});
93175

94-
describe('tryCallbackFlatten 2', () => {
176+
describe('tryCallbackFlatten 2 input + 0 result', () => {
177+
test('resolved', async () => {
178+
const callbackFunction = (a: string, b: 'b1' | 'b2', callback: (err: Error | null) => void) => {
179+
setTimeout(() => {
180+
callback(null);
181+
});
182+
};
183+
const [err, res] = await tryCallback(callbackFunction, '', 'b1');
184+
185+
if (err) {
186+
assertError(err);
187+
assertUndefined(res);
188+
} else {
189+
assertNull(err);
190+
}
191+
192+
expect(err).toBe(null);
193+
expect(res).toBeUndefined();
194+
});
195+
196+
test('rejected', async () => {
197+
const callbackFunction = (a: string, b: 'b1' | 'b2', callback: (err: Error | null) => void) => {
198+
setTimeout(() => {
199+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
200+
// @ts-ignore
201+
callback(new Error('1'));
202+
});
203+
};
204+
const [err, res] = await tryCallback(callbackFunction, '', 'b1');
205+
206+
if (err) {
207+
assertError(err);
208+
assertUndefined(res);
209+
} else {
210+
assertNull(err);
211+
}
212+
213+
expect(err?.message).toBe('1');
214+
expect(res).toBeUndefined();
215+
});
216+
});
217+
218+
describe('tryCallbackFlatten 2 input + 1 result', () => {
95219
test('resolved', async () => {
96220
const callbackFunction: CallbackFunction2<string, 'b1' | 'b2', number> = (a, b, callback) => {
97221
setTimeout(() => {

tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
"sourceMap": true,
77
"skipLibCheck": true,
88
"esModuleInterop": true,
9-
"isolatedModules": true,
109
"resolveJsonModule": true,
1110
"verbatimModuleSyntax": true,
1211
"useDefineForClassFields": true,
1312
"allowSyntheticDefaultImports": true,
1413
"forceConsistentCasingInFileNames": true,
14+
"moduleResolution": "Node",
1515
"target": "ES2020",
1616
"module": "ES2020",
17-
"moduleResolution": "Node",
1817
"lib": ["ES2020"],
1918
"types": ["vitest/globals"]
2019
},

0 commit comments

Comments
 (0)