archieve-projects/微信机器人/node_modules/cmd-ts/dist/esm/union.js

24 lines
820 B
JavaScript
Executable File

import { fromFn, typeDef } from './type';
import * as Result from './Result';
/**
* Take one of the types. Merge the metadata from left to right.
* If nothing matches, prints all the errors.
*/
export function union(ts, { combineErrors = errors => errors.join('\n'), } = {}) {
const merged = Object.assign({}, ...ts.map(x => typeDef(x)));
return {
...merged,
async from(input) {
const errors = [];
for (const t of ts) {
const decoded = await Result.safeAsync(fromFn(t)(input));
if (Result.isOk(decoded)) {
return decoded.value;
}
errors.push(decoded.error.message);
}
throw new Error(combineErrors(errors));
},
};
}
//# sourceMappingURL=union.js.map