UNPKG

@typed/io

Version:

Runtime IO type system

27 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.map = exports.Map = void 0; const effects_1 = require("@typed/effects"); const either_1 = require("@typed/either"); const strings_1 = require("@typed/strings"); const G = require("../guard"); const Decoder_1 = require("./Decoder"); const refinement_1 = require("./refinement"); const UnknownMap = Decoder_1.Decoder.fromGuard(G.Map, `ReadonlyMap<unknown, unknown>`); exports.Map = UnknownMap; exports.map = (key, value) => { return refinement_1.refinement(UnknownMap, function* (map) { const entries = yield* effects_1.sequence(function* (keyValuePair) { const k = yield* Decoder_1.catchDecodeFailure(key.decode(keyValuePair[0])); const v = yield* Decoder_1.catchDecodeFailure(value.decode(keyValuePair[1])); if (either_1.isLeft(k) || either_1.isLeft(v)) { const expected = `ReadonlyMap<${key.expected}, ${value.expected}>`; const actual = strings_1.toString(map); return yield* Decoder_1.decodeFailure(Decoder_1.DecodeError.create(expected, actual)); } return [either_1.fromRight(k), either_1.fromRight(v)]; }, Array.from(map)); return new Map(entries); }, `ReadonlyMap<${key.expected}, ${value.expected}>`); }; //# sourceMappingURL=Map.js.map