macoolka-io
Version:
`macoolka-io` is Runtime type system for IO decoding/encoding.
124 lines • 4.78 kB
JavaScript
;
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.show = exports.formatValue = exports.withDefault = void 0;
/**
* Collection for Common type
* @desczh
* 通用类型集合
* @file
*/
var t = __importStar(require("io-ts"));
var pipeable_1 = require("fp-ts/pipeable");
var A = __importStar(require("fp-ts/Array"));
var macoolka_predicate_1 = require("macoolka-predicate");
var Eq_1 = require("fp-ts/Eq");
var macoolka_object_1 = require("macoolka-object");
var O = __importStar(require("fp-ts/Option"));
/**
* apply a default value when the value is null.
* @desczh
* 给一个类型赋缺省值
* @example
* import * as t from 'macoolka-io'
* import { right } from 'fp-ts/Either'
*
* const M = t.withDefault(t.string, '123');
* expect(M.decode(null)).toEqual(right('123'));
* expect(M.decode(null)).toEqual(right('123'));
* expect(M.decode('4')).toEqual(right('4'));
*
* const MA=t.type({
* name:t.string,
* names:t.array(t.string)
* })
*
* const MB=t.type({
* name:t.withDefault(t.string,'1'),
* names:t.withDefault(t.array(t.string),[])
* })
* expect(MB.decode({})).toEqual(right({name:'1',names:[]}));
* expect(MB.decode({name:'2'})).toEqual(right({name:'2',names:[]}));
* expect(MB.decode({names:['1']})).toEqual(right({name:'1',names:['1']}));
* expect(MB.decode({names:['3'],name:'3'})).toEqual(right({name:'3',names:['3']}));
* expect(isLeft(MA.decode({}))).toEqual(true);
*
* @since 0.2.0
*/
var withDefault = function (type, defaultValue) {
return new t.Type("withDefault(".concat(type.name, ", ").concat(JSON.stringify(defaultValue), ")"), type.is, function (v, c) {
return type.validate(v != null ? v : defaultValue, c);
}, type.encode);
};
exports.withDefault = withDefault;
/**
* @ignore
*/
function formatValue(v) {
if (typeof v === 'function') {
return t.getFunctionName(v);
}
if (typeof v === 'number' && !isFinite(v)) {
if ((0, macoolka_predicate_1.isNaN)(v)) {
return 'NaN';
}
return v > 0 ? 'Infinity' : '-Infinity';
}
return macoolka_object_1.showUnknow.show(v);
}
exports.formatValue = formatValue;
var showRecord = function (_a) {
var showValue = _a.showValue, showDecoder = _a.showDecoder, showKey = _a.showKey;
return function (e) {
function getContextPath(context) {
return (0, pipeable_1.pipe)(context, A.map(function (_a) {
var key = _a.key, type = _a.type;
return "".concat((0, pipeable_1.pipe)(showKey(key), O.getOrElse(function () { return key; })), ": ").concat((0, pipeable_1.pipe)(type, showDecoder, O.getOrElse(function () { return type.name; })));
}), function (as) { return as.join('/'); });
}
return "".concat((0, pipeable_1.pipe)(e.value, showValue, O.getOrElse(function () { return "Invalid value ".concat(formatValue(e.value), " supplied to"); })), " ").concat(getContextPath(e.context));
};
};
var defaultShowMessage = {
showValue: function (value) { return O.some("Invalid value ".concat(value, " supplied to")); },
showDecoder: function (type) { return O.some(type.name); },
showKey: function (key) { return O.some(key); }
};
/**
* Show Errors to string
* @desczh
* 格式化错误到文本
* @since 0.2.0
*/
var show = function (as, showMessage) {
var _showMessage = (0, macoolka_object_1.merge)(defaultShowMessage, showMessage);
return (0, pipeable_1.pipe)(as.map(function (e) {
return e.message !== undefined
? e.message
: showRecord(_showMessage)(e);
}), A.uniq(Eq_1.eqString), function (as) { return as.join('\n'); });
};
exports.show = show;
//# sourceMappingURL=common.js.map