UNPKG

io-ts-types

Version:

A collection of codecs and combinators for use with io-ts

54 lines (53 loc) 1.95 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapOutput = void 0; /** * @since 0.3.2 */ var t = __importStar(require("io-ts")); /** * Changes the output type of the given runtime type * * @example * import * as t from 'io-ts' * import { mapOutput } from 'io-ts-types/lib/mapOutput' * import { optionFromNullable } from 'io-ts-types/lib/optionFromNullable' * import { none, some } from 'fp-ts/lib/Option' * * // Input: t.Type<Option<number>, number | null, t.mixed> * const Input = optionFromNullable(t.number) * * const toUndefined = <A>(x: A | null): A | undefined => (x === null ? undefined : x) * * // Output: t.Type<Option<number>, number | undefined, t.mixed> * const Output = mapOutput(Input, toUndefined) * * assert.strictEqual(Output.encode(none), undefined) * assert.strictEqual(Output.encode(some(1)), 1) * * @since 0.3.2 */ function mapOutput(codec, f, name) { if (name === void 0) { name = codec.name; } return new t.Type(name, codec.is, codec.validate, function (a) { return f(codec.encode(a)); }); } exports.mapOutput = mapOutput;