io-ts-bigint
Version:
io-ts library for bigints
57 lines • 2.44 kB
JavaScript
;
/**
* @since 0.0.1
*/
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 (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BigIntFromString = void 0;
// This file imported from io-ts-types to avoid 3 peer dependencies
var E = __importStar(require("fp-ts/Either"));
var function_1 = require("fp-ts/function");
var t = __importStar(require("io-ts"));
var NonEmptyString_1 = require("./NonEmptyString");
/**
* @import
* example { BigIntFromString } from 'io-ts-bigint/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', function (u) { return typeof u === 'bigint'; }, function (u, c) {
return function_1.pipe(t.string.validate(u, c), E.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);
//# sourceMappingURL=BigIntFromString.js.map