"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Decoder: () => Decoder, at: () => at, boolean: () => boolean, fail: () => fail, fork: () => fork, json: () => json, lazy: () => lazy, literal: () => literal, literals: () => literals, null: () => null_, number: () => number, object: () => object, string: () => string, stringRecord: () => stringRecord, succeed: () => succeed, tuple: () => tuple, undefined: () => undefined_, union: () => union, unknown: () => unknown }); module.exports = __toCommonJS(src_exports); var Decoder = class _Decoder { constructor(decode) { this.decode = decode; } decodeOrThrow(value) { const decoded = this.decode(value); if (!decoded.ok) throw new Error( `Decode Error at path=${JSON.stringify( decoded.path )} message=${JSON.stringify(decoded.message)}` ); return decoded.value; } decodeOr(value, or) { const decoded = this.decode(value); return decoded.ok ? decoded.value : or; } map(fun) { return new _Decoder((value) => { const decoded = this.decode(value); if (!decoded.ok) return decoded; return { ok: true, value: fun(decoded.value) }; }); } filter(fun, message) { return new _Decoder((value) => { const decoded = this.decode(value); if (!decoded.ok) return decoded; if (!fun(decoded.value)) return error( [], typeof message === "string" ? message : message(decoded.value), value ); return decoded; }); } array() { return new _Decoder((value) => { if (!Array.isArray(value)) return expected([], "an array", value); const decoded = []; for (let i = 0; i < value.length; i++) { const d = this.decode(value[i]); if (!d.ok) return error([i, ...d.path], d.message, d.value); decoded.push(d.value); } return { ok: true, value: decoded }; }); } optional(defaultValue) { return new _Decoder( (value) => value === void 0 ? { ok: true, value: defaultValue ?? void 0 } : this.decode(value) ); } nullable(defaultValue) { return new _Decoder( (value) => value === null ? { ok: true, value: defaultValue ?? null } : this.decode(value) ); } }; var boolean = () => new Decoder( (value) => typeof value === "boolean" ? { ok: true, value } : expected([], "a boolean", value) ); var number = () => new Decoder( (value) => typeof value === "number" ? { ok: true, value } : expected([], "a number", value) ); var string = () => new Decoder( (value) => typeof value === "string" ? { ok: true, value } : expected([], "a string", value) ); var literal = (literal2) => { const message = JSON.stringify(literal2); return new Decoder( (value) => value === literal2 ? { ok: true, value: literal2 } : expected([], message, value) ); }; var literals = (literals2) => { const set = new Set(literals2); const message = `one of [${literals2.map((literal2) => JSON.stringify(literal2)).join(", ")}]`; return new Decoder( (value) => set.has(value) ? { ok: true, value } : expected([], message, value) ); }; var null_ = () => new Decoder( (value) => value === null ? { ok: true, value } : expected([], "null", value) ); var undefined_ = () => new Decoder( (value) => value === void 0 ? { ok: true, value } : expected([], "undefined", value) ); var unknown = () => new Decoder((value) => ({ ok: true, value })); var object = (fields) => new Decoder((value) => { if (value === null || typeof value !== "object" || Array.isArray(value)) return expected([], "an object", value); const decoded = {}; for (const key in fields) { const d = fields[key].decode(value[key]); if (!d.ok) return error([key, ...d.path], d.message, d.value); decoded[key] = d.value; } return { ok: true, value: decoded }; }); var tuple = (decoders) => new Decoder((value) => { if (!Array.isArray(value)) return expected([], "an array", value); if (value.length !== decoders.length) return error( [], `an array of length ${decoders.length}, found an array of length ${value.length}`, value ); const decoded = []; for (let i = 0; i < decoders.length; i++) { const d = decoders[i].decode(value[i]); if (!d.ok) return error([i, ...d.path], d.message, d.value); decoded.push(d.value); } return { ok: true, value: decoded }; }); var stringRecord = (decoder) => new Decoder((value) => { if (value === null || typeof value !== "object" || Array.isArray(value)) return expected([], "an object", value); const decoded = {}; for (const key in value) { const d = decoder.decode(value[key]); if (!d.ok) return error([key, ...d.path], d.message, d.value); decoded[key] = d.value; } return { ok: true, value: decoded }; }); var union = (decoders) => { return new Decoder((value) => { for (const decoder of decoders) { const d = decoder.decode(value); if (d.ok) return d; } return error([], `one of ${decoders.length} decoders`, value); }); }; var at = (path, decoder) => new Decoder((value) => { for (let i = 0; i < path.length; i++) { const keyOrIndex = path[i]; if (typeof keyOrIndex === "number") { if (!Array.isArray(value)) return expected(path.slice(0, i), "an array", value); const value2 = value[keyOrIndex]; if (value2 === void 0) return error( path.slice(0, i), `an array with index ${keyOrIndex}`, value ); value = value2; } else { if (value === null || typeof value !== "object") return expected(path.slice(0, i), "an object", value); const value2 = value[keyOrIndex]; if (value2 === void 0) return error( path.slice(0, i), `an object with key ${JSON.stringify(keyOrIndex)}`, value ); value = value2; } } const d = decoder.decode(value); if (!d.ok) return error([...path, ...d.path], d.message, d.value); return d; }); var fork = (decoders) => new Decoder((value) => { const decoded = []; for (const decoder of decoders) { const d = decoder.decode(value); if (!d.ok) return d; decoded.push(d.value); } return { ok: true, value: decoded }; }); var json = (decoder) => new Decoder((value) => { if (typeof value !== "string") return expected([], "a string", value); try { const parsed = JSON.parse(value); return decoder.decode(parsed); } catch (error_) { return error([], "" + error_, value); } }); var lazy = (fun) => new Decoder((value) => fun().decode(value)); var succeed = (value) => new Decoder(() => ({ ok: true, value })); var fail = (message) => new Decoder((value) => error([], message, value)); var error = (path, message, value) => ({ ok: false, path, message, value }); var expected = (path, expected2, value) => { const type = typeof value; const found = value === null ? "null" : value === void 0 ? "undefined" : type === "boolean" ? "a boolean" : type === "number" ? "a number" : type === "string" ? "a string" : type === "object" ? Array.isArray(value) ? `an array` : `an object` : `a ${type}`; return error(path, `expected ${expected2}, found ${found}`, value); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Decoder, at, boolean, fail, fork, json, lazy, literal, literals, null: null, number, object, string, stringRecord, succeed, tuple, undefined, union, unknown });