UNPKG

@consolidados/results

Version:
64 lines (62 loc) 2.19 kB
'use strict'; // src/helpers/match.ts function match(matcher, cases, discriminant) { if (typeof matcher === "string" || typeof matcher === "number" || typeof matcher === "symbol") { const handler = cases[matcher]; if (handler) { return handler(); } if (cases.default) { return cases.default(); } throw new Error(`No case found for value: ${String(matcher)}`); } if (typeof matcher === "object" && matcher !== null && !discriminant) { for (const key in cases) { if (key === "default") continue; if (key in matcher) { const handler = cases[key]; if (handler) { return typeof handler === "function" ? handler(matcher[key]) : handler(); } } } if (cases.default) { return cases.default(); } } if (discriminant && typeof matcher === "object" && matcher !== null) { const discriminantValue = matcher[discriminant]; const handler = cases[discriminantValue]; if (handler) { return handler(matcher); } if (cases.default) { return cases.default(matcher); } throw new Error( `No case found for discriminant value: ${String(discriminantValue)}` ); } if (typeof matcher === "object" && matcher !== null && "isOk" in matcher && matcher.isOk()) { if (!cases.Ok) throw new Error("Missing case for Ok"); return cases.Ok(matcher.unwrap()); } if (typeof matcher === "object" && matcher !== null && "isErr" in matcher && matcher.isErr()) { if (!cases.Err) throw new Error("Missing case for Err"); return cases.Err(matcher.unwrapErr()); } if (typeof matcher === "object" && matcher !== null && "isSome" in matcher && matcher.isSome()) { if (!cases.Some) throw new Error("Missing case for Some"); return cases.Some(matcher.unwrap()); } if (typeof matcher === "object" && matcher !== null && "isNone" in matcher && matcher.isNone()) { if (!cases.None) throw new Error("Missing case for None"); return cases.None(); } throw new Error("Invalid matcher or missing case"); } globalThis.match = match; exports.match = match; //# sourceMappingURL=match.cjs.map //# sourceMappingURL=match.cjs.map