UNPKG

io-ts-types

Version:

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

46 lines (45 loc) 1.92 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.IntFromString = void 0; /** * @since 0.4.4 */ var t = __importStar(require("io-ts")); var NumberFromString_1 = require("./NumberFromString"); var pipeable_1 = require("fp-ts/lib/pipeable"); var Either_1 = require("fp-ts/lib/Either"); /** * A codec that succeeds if a string can be parsed to an integer * * @example * import { IntFromString } from 'io-ts-types/lib/IntFromString' * import { right } from 'fp-ts/lib/Either' * import { PathReporter } from 'io-ts/lib/PathReporter' * * assert.deepStrictEqual(IntFromString.decode('1'), right(1)) * assert.deepStrictEqual(PathReporter.report(IntFromString.decode('1.1')), ['Invalid value "1.1" supplied to : IntFromString']) * * @since 0.4.4 */ exports.IntFromString = new t.Type('IntFromString', t.Int.is, function (u, c) { return pipeable_1.pipe(NumberFromString_1.NumberFromString.validate(u, c), Either_1.chain(function (n) { return (t.Int.is(n) ? t.success(n) : t.failure(u, c)); })); }, NumberFromString_1.NumberFromString.encode);