UNPKG

neverthrow-pattern

Version:
67 lines (65 loc) 1.37 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/patterns.ts var patterns_exports = {}; __export(patterns_exports, { err: () => err, ok: () => ok }); import { Ok, Err } from "neverthrow"; import { isMatching, P } from "ts-pattern"; function ok(...args) { return { [P.matcher]() { return { match(value) { if (!(value instanceof Ok)) { return { matched: false }; } if (args.length === 0) { return { matched: true }; } return { // @ts-ignore matched: isMatching(args[0], value.value) }; } }; } }; } function err(...args) { return { [P.matcher]() { return { match(value) { if (!(value instanceof Err)) { return { matched: false }; } if (args.length === 0) { return { matched: true }; } return { // @ts-ignore matched: isMatching(args[0], value.error) }; } }; } }; } export { patterns_exports as NP, patterns_exports as NeverthrowPattern };