UNPKG

macoolka-io

Version:

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

165 lines 6.7 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.url = exports.ipv6 = exports.ipv4 = exports.email = exports.uuid = exports.stringMatch = exports.stringMaxLength = exports.stringMinLength = exports.nonEmptyString = void 0; /** * Collection for String * @desczh * 文本类型集合 * @file */ var t = __importStar(require("io-ts")); var pipeable_1 = require("fp-ts/pipeable"); var macoolka_predicate_1 = require("macoolka-predicate"); var O = __importStar(require("fp-ts/Option")); var string_1 = require("macoolka-predicate/lib/string"); var Constant_1 = require("./Constant"); /** * A codec that succeeds if a string is not empty * @desczh * 非空文本 * @example * import { NonEmptyString } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * assert.deepStrictEqual(NonEmptyString.decode('a'), right('a')) * assert(isLeft(NonEmptyString.decode(''))) * * @since 0.2.0 */ exports.nonEmptyString = t.brand(t.string, function (s) { return s.length > 0; }, 'NonEmptyString'); /** * A codec that succeeds if a string min length is given value. * @desczh * 校验文本最小长度 * @example * import { stringMinLength } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(stringMinLength(3).decode('123')).toEqual(right('123')) * expect(isLeft(stringMinLength(3).decode('12'))).toEqual(true) * @since 0.2.0 */ var stringMinLength = function (_minLength) { return new t.Type(['stringMinLength', _minLength].join(Constant_1.NameSplit), t.string.is, function (u, c) { return (0, pipeable_1.pipe)(u, O.fromPredicate(macoolka_predicate_1.isString), O.chain(function (value) { return (0, string_1.minLength)(_minLength)(value) ? O.some(value) : O.none; }), O.fold(function () { return t.failure(u, c); }, function (value) { return t.success(value); })); }, String); }; exports.stringMinLength = stringMinLength; /** * A codec that succeeds if a string max length is given value. * @desczh * 校验文本最大长度 * @example * import { stringMaxLength } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(t.stringMaxLength(3).decode('123')).toEqual(right('123')) * expect(isLeft(t.stringMaxLength(3).decode('1234'))).toEqual(true) * @since 0.2.0 */ var stringMaxLength = function (_maxLength) { return new t.Type(['stringMaxLength', _maxLength].join(Constant_1.NameSplit), t.string.is, function (u, c) { return (0, pipeable_1.pipe)(u, O.fromPredicate(macoolka_predicate_1.isString), O.chain(function (value) { return (0, string_1.maxLength)(_maxLength)(value) ? O.some(value) : O.none; }), O.fold(function () { return t.failure(u, c); }, function (value) { return t.success(value); })); }, String); }; exports.stringMaxLength = stringMaxLength; /** * A codec that succeeds if a string match a RegExp. * @desczh * 校验文本匹配正则表达式 * @example * import { stringMatch } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(stringMatch(/^A/).decode('ABC')).toEqual(right('ABC')) * expect(isLeft(stringMatch(/^A/).decode('12'))).toEqual(true) * @since 0.2.0 */ var stringMatch = function (regexp) { return new t.Type(['stringMatch', regexp].join(Constant_1.NameSplit), t.string.is, function (u, c) { return (0, pipeable_1.pipe)(u, O.fromPredicate(macoolka_predicate_1.isString), O.chain(function (value) { return (0, string_1.match)(regexp)(value) ? O.some(value) : O.none; }), O.fold(function () { return t.failure(u, c); }, function (value) { return t.success(value); })); }, String); }; exports.stringMatch = stringMatch; /** * A codec that succeeds if a string is UUID. * @desczh * 校验文本是UUID * @example * import { uuid } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * @since 0.2.0 */ exports.uuid = t.brand(t.string, function (s) { return (0, string_1.isUUID)(s); }, 'UUID'); /** * A codec that succeeds if a string is EMAIL. * @desczh * 校验文本是EMAIL * @example * import { email } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(email.decode('a@mail.com')).toEqual(right('a@mail.com')) * expect(isLeft(email.decode('12'))).toEqual(true) * @since 0.2.0 */ exports.email = t.brand(t.string, function (s) { return (0, string_1.isEmail)(s); }, 'email'); /** * A codec that succeeds if a string is IPV4. * @desczh * 校验文本是IPV4 * @example * import { ipv4 } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(ipv4.decode('8.8.8.8')).toEqual(right('8.8.8.8')) * expect(isLeft(ipv4.decode('12'))).toEqual(true) * * @since 0.2.0 */ exports.ipv4 = t.brand(t.string, function (s) { return (0, string_1.isIpV4)(s); }, 'ipv4'); /** * A codec that succeeds if a string is IPV6. * @desczh * 校验文本是IPV6 * @example * import { ipv6 } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(ipv6.decode('2409:8a15:244a:a780:b0f5:8e9a:2c2e:5ce2')).toEqual(right('2409:8a15:244a:a780:b0f5:8e9a:2c2e:5ce2')) * expect(isLeft(ipv6.decode('8.8.8.8'))).toEqual(true) * @since 0.2.0 */ exports.ipv6 = t.brand(t.string, function (s) { return (0, string_1.isIpV6)(s); }, 'ipv6'); /** * A codec that succeeds if a string is url. * @desczh * 校验文本是url * @example * import { url } from 'macoolka-io' * import { right,isLeft } from 'fp-ts/Either' * * expect(url.decode('http://bing.com')).toEqual(right('http://bing.com')) * expect(isLeft(url.decode('8.8.8.8'))).toEqual(true) * @since 0.2.0 */ exports.url = t.brand(t.string, function (s) { return (0, string_1.isUrl)(s); }, 'Url'); //# sourceMappingURL=string.js.map