io-ts-types
Version:
A collection of codecs and combinators for use with io-ts
58 lines (57 loc) • 2.33 kB
JavaScript
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.BigIntFromString = void 0;
/**
* @since 0.5.11
*/
var t = __importStar(require("io-ts"));
var pipeable_1 = require("fp-ts/lib/pipeable");
var Either_1 = require("fp-ts/lib/Either");
var NonEmptyString_1 = require("./NonEmptyString");
/**
* @example
* import { BigIntFromString } from 'io-ts-types/lib/BigIntFromString'
* import { right } from 'fp-ts/lib/Either'
* import { PathReporter } from 'io-ts/lib/PathReporter'
*
* assert.deepStrictEqual(BigIntFromString.decode('1'), right(BigInt(1)))
* assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode('1.1')), ['Invalid value "1.1" supplied to : BigIntFromString'])
* assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode('')), ['Invalid value "" supplied to : BigIntFromString'])
* assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode(' ')), ['Invalid value " " supplied to : BigIntFromString'])
*
* @since 0.5.11
*/
exports.BigIntFromString = new t.Type('BigIntFromString',
// tslint:disable-next-line
function (u) { return typeof u === 'bigint'; }, function (u, c) {
return pipeable_1.pipe(t.string.validate(u, c), Either_1.chain(function (s) {
if (!NonEmptyString_1.NonEmptyString.is(s.trim())) {
return t.failure(u, c);
}
try {
return t.success(BigInt(s));
}
catch (error) {
return t.failure(u, c);
}
}));
}, String);
;