Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f53ba70
Add testAsync check
jguyon Aug 15, 2020
7524ceb
Add transformAsync check
jguyon Aug 15, 2020
d3507f5
Add notAsync check
jguyon Aug 15, 2020
be1afd1
Add chainAsync check
jguyon Aug 15, 2020
c64d18b
Refer to sync version in doc of async checks
jguyon Aug 15, 2020
1f89521
Use mocks in tests for test function
jguyon Aug 16, 2020
fe3cdeb
Use mocks in tests for testAsync function
jguyon Aug 16, 2020
c577d33
Use mocks in tests for transform function
jguyon Aug 16, 2020
57d17c9
Use mocks in tests for transformAsync function
jguyon Aug 16, 2020
9039c0c
Use mocks in tests for optional function
jguyon Aug 16, 2020
93b5f67
Use mocks in tests for nullable function
jguyon Aug 16, 2020
2c4c8e8
Use mocks in tests for not function
jguyon Aug 16, 2020
1223d8f
Use mocks in test for notAsync function
jguyon Aug 16, 2020
1a93412
Use mocks in tests for chain function
jguyon Aug 16, 2020
c3e5e1f
Use mocks in tests for chainAsync function
jguyon Aug 16, 2020
41e4460
Use mocks in tests for oneOf function
jguyon Aug 17, 2020
1945dfc
Use mocks in tests for shape function
jguyon Aug 17, 2020
0daa2cc
Use mocks in tests for items function
jguyon Aug 17, 2020
2706f0d
Use mocks in tests for tuple function
jguyon Aug 17, 2020
7899fff
Use mocks in tests for entries function
jguyon Aug 17, 2020
4d44147
Add oneOfAsync check
jguyon Aug 17, 2020
5dc09e4
Add optionalAsync check
jguyon Aug 17, 2020
df9a2b5
Add tests for handling of sync checks
jguyon Aug 17, 2020
452ec6a
Add nullableAsync check
jguyon Aug 17, 2020
3303dfb
Add shapeAsync check
jguyon Aug 17, 2020
3422de9
Improve typing of tuple check
jguyon Aug 18, 2020
b7cf74a
Add tupleAsync check
jguyon Aug 18, 2020
f9cd0cc
Drop support for iterables in items check
jguyon Aug 18, 2020
9c5c540
Fix documentation of ok function
jguyon Aug 18, 2020
70e4f54
Fix documentation of items check
jguyon Aug 18, 2020
369b41a
Add itemsAsync check
jguyon Aug 18, 2020
ce13edd
Fix incomplete documentation of entries check
jguyon Aug 18, 2020
3b745f0
Add entriesAsync check
jguyon Aug 18, 2020
4a95afb
Export async check types
jguyon Aug 18, 2020
f84ca54
Use kebab case for file names
jguyon Aug 18, 2020
6720df8
Make asynchronicity of function arguments optional
jguyon Aug 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/async-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Result from "./result";

export default AsyncCheck;

type AsyncCheck<I, O = I, A extends unknown[] = unknown[]> = (
value: I,
...args: A
) => Promise<Result<O>>;
255 changes: 255 additions & 0 deletions src/chain-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import AsyncCheck from "./async-check";
import MaybeAsyncCheck from "./maybe-async-check";
import ok from "./ok";

export default function chainAsync<V1, A extends unknown[]>(): AsyncCheck<
V1,
V1,
A
>;
export default function chainAsync<V1, V2, A extends unknown[]>(
...checks: [MaybeAsyncCheck<V1, V2, A>]
): AsyncCheck<V1, V2, A>;
export default function chainAsync<V1, V2, V3, A extends unknown[]>(
...checks: [MaybeAsyncCheck<V1, V2, A>, MaybeAsyncCheck<V2, V3, A>]
): AsyncCheck<V1, V3, A>;
export default function chainAsync<V1, V2, V3, V4, A extends unknown[]>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
]
): AsyncCheck<V1, V4, A>;
export default function chainAsync<V1, V2, V3, V4, V5, A extends unknown[]>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
]
): AsyncCheck<V1, V5, A>;
export default function chainAsync<V1, V2, V3, V4, V5, V6, A extends unknown[]>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
]
): AsyncCheck<V1, V6, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
]
): AsyncCheck<V1, V7, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
]
): AsyncCheck<V1, V8, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
V9,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
MaybeAsyncCheck<V8, V9, A>,
]
): AsyncCheck<V1, V9, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
V9,
V10,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
MaybeAsyncCheck<V8, V9, A>,
MaybeAsyncCheck<V9, V10, A>,
]
): AsyncCheck<V1, V10, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
V9,
V10,
V11,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
MaybeAsyncCheck<V8, V9, A>,
MaybeAsyncCheck<V9, V10, A>,
MaybeAsyncCheck<V10, V11, A>,
]
): AsyncCheck<V1, V11, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
V9,
V10,
V11,
V12,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
MaybeAsyncCheck<V8, V9, A>,
MaybeAsyncCheck<V9, V10, A>,
MaybeAsyncCheck<V10, V11, A>,
MaybeAsyncCheck<V11, V12, A>,
]
): AsyncCheck<V1, V12, A>;
export default function chainAsync<
V1,
V2,
V3,
V4,
V5,
V6,
V7,
V8,
V9,
V10,
V11,
V12,
V13,
A extends unknown[]
>(
...checks: [
MaybeAsyncCheck<V1, V2, A>,
MaybeAsyncCheck<V2, V3, A>,
MaybeAsyncCheck<V3, V4, A>,
MaybeAsyncCheck<V4, V5, A>,
MaybeAsyncCheck<V5, V6, A>,
MaybeAsyncCheck<V6, V7, A>,
MaybeAsyncCheck<V7, V8, A>,
MaybeAsyncCheck<V8, V9, A>,
MaybeAsyncCheck<V9, V10, A>,
MaybeAsyncCheck<V10, V11, A>,
MaybeAsyncCheck<V11, V12, A>,
MaybeAsyncCheck<V12, V13, A>,
]
): AsyncCheck<V1, V13, A>;

/**
* Asynchronous version of [[`chain`]].
*
* ```js
* const check = chainAsync(
* string(),
* trim(),
* testAsync(async value => value === "jerome"),
* );
*
* await check(" jerome ");
* // => {
* // isOk: true,
* // value: "jerome",
* // }
*
* await check("john");
* // => { isOk: false, ... }
* ```
*
* @param checks The async check functions to chain together.
* @returns An async check function.
*/
export default function chainAsync<A extends unknown[]>(
...checks: Array<MaybeAsyncCheck<any, any, A>>
): AsyncCheck<any, any, A> {
return async (value, ...args) => {
for (const check of checks) {
const result = await check(value, ...args);

if (result.isOk) {
value = result.value;
} else {
return result;
}
}

return ok(value);
};
}
73 changes: 73 additions & 0 deletions src/entries-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import AsyncCheck from "./async-check";
import MaybeAsyncCheck from "./maybe-async-check";
import ok from "./ok";

/**
* Asynchronous version of [[`entries`]].
*
* ```js
* const check = entriesAsync(
* pattern(/^[0-9]+$/),
* chainAsync(
* string(),
* transformAsync(async value => value.trim()),
* ),
* );
*
* await check({
* 1: " one ",
* 2: " two "
* });
* // => {
* // isOk: true,
* // value: {
* // 1: "one",
* // 2: "two",
* // },
* // }
*
* await check({ 1: 1 });
* // => { isOk: false, ... }
*
* await check({ one: "one" });
* // => { isOk: false, ... }
* ```
*
* @param checkKey The async check function to use on the keys.
* @param checkValue The async check function to use on the values.
* @returns An async check function.
*/
export default function entriesAsync<I, O, A extends unknown[]>(
checkKey: MaybeAsyncCheck<string, string, A>,
checkValue: MaybeAsyncCheck<I, O, A>,
): AsyncCheck<{ [key: string]: I }, { [key: string]: O }, A> {
return async (input, ...args) => {
const keys = Object.keys(input);
const [keyResults, valueResults] = await Promise.all([
Promise.all(keys.map((key) => checkKey(key, ...args))),
Promise.all(keys.map((key) => checkValue(input[key], ...args))),
]);
const output: { [key: string]: O } = {};

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const keyResult = keyResults[i];
const valueResult = valueResults[i];

if (keyResult.isOk) {
if (valueResult.isOk) {
output[keyResult.value] = valueResult.value;
} else {
return {
...valueResult,
path: [key, ...valueResult.path],
};
}
} else {
return keyResult;
}
}

return ok(output);
};
}
4 changes: 4 additions & 0 deletions src/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import ok from "./ok";
* check({ one: "one" });
* // => { isOk: false, ... }
* ```
*
* @param checkKey The check function to use on the keys.
* @param checkValue The check function to use on the values.
* @returns A check function.
*/
export default function entries<I, O, A extends unknown[]>(
checkKey: Check<string, string, A>,
Expand Down
Loading