UNPKG

@typed/io

Version:

Runtime IO type system

35 lines 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.partial = void 0; const effects_1 = require("@typed/effects"); const either_1 = require("@typed/either"); const maybe_1 = require("@typed/maybe"); const objects_1 = require("@typed/objects"); const strings_1 = require("@typed/strings"); const Decoder_1 = require("./Decoder"); const Record_1 = require("./Record"); const refinement_1 = require("./refinement"); exports.partial = (decoders, expected = getDefaultPartialExpected(decoders)) => { const keys = objects_1.keysOf(decoders); return refinement_1.refinement(Record_1.Record, function* (r) { const decoded = yield* effects_1.combine(...keys .filter((k) => objects_1.hasOwnProperty(k, r)) .map((k) => Decoder_1.catchDecodeFailure(effects_1.map((a) => [a, k], decoders[k].decode(r[k])), () => k))); if (decoded.every(either_1.isRight)) { return decoded .map((d) => either_1.fromRight(d)) .reduce((acc, [v, k]) => ({ ...acc, [k]: v }), {}); } const errors = decoded.filter(either_1.isLeft).map(either_1.fromLeft); return yield* Decoder_1.decodeFailure(decodePartialError(errors, expected, strings_1.toString(r))); }, expected); }; function getDefaultPartialExpected(decoders) { return `{\n ${objects_1.mapToList((key, value) => `"${key.toString()}"?: ${value.expected}`, decoders).join(`,\n `)}\n}`; } function decodePartialError(errors, expected, value) { return Decoder_1.DecodeError.create(expected, value, { errors: errors.map(([e, key]) => ({ ...e, key: maybe_1.Just.of(strings_1.toString(key)) })), }); } //# sourceMappingURL=Partial.js.map