UNPKG

macoolka-io

Version:

`macoolka-io` is Runtime type system for IO decoding/encoding.

62 lines 2.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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.nonEmptyArray = void 0; /** * Collection for Array * @desczh * 数组类型集合 * @file */ var t = __importStar(require("io-ts")); var NonEmptyArray_1 = require("fp-ts/NonEmptyArray"); var Array_1 = require("fp-ts/Array"); var Either_1 = require("fp-ts/Either"); var Option_1 = require("fp-ts/Option"); /** * Get A non empty array * @desczh * 得到一个非空数组 * @example * import { nonEmptyArray } from 'macoolka-io' * import { isLeft } from 'fp-ts/Either' * * const input:string[] = [] * assert(isLeft(nonEmptyArray.decode(input))) * * @since 0.2.0 */ function nonEmptyArray(codec, name) { if (name === void 0) { name = "NonEmptyArray<".concat(codec.name, ">"); } var arr = t.array(codec); return new t.Type(name, function (u) { return arr.is(u) && (0, Array_1.isNonEmpty)(u); }, function (u, c) { return Either_1.either.chain(arr.validate(u, c), function (as) { var onea = (0, NonEmptyArray_1.fromArray)(as); return (0, Option_1.isNone)(onea) ? t.failure(u, c) : t.success(onea.value); }); }, function (nea) { return arr.encode(nea); }); } exports.nonEmptyArray = nonEmptyArray; //# sourceMappingURL=array.js.map