macoolka-io
Version:
`macoolka-io` is Runtime type system for IO decoding/encoding.
336 lines • 10.5 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
/**
* I18n io
* @desczh
* 国际化IO
* @file
*/
var t = __importStar(require("./io"));
var Module_1 = __importDefault(require("./Module"));
var pipeable_1 = require("fp-ts/pipeable");
var O = __importStar(require("fp-ts/Option"));
var A = __importStar(require("fp-ts/Array"));
__exportStar(require("./io"), exports);
var macoolka_predicate_1 = require("macoolka-predicate");
var macoolka_object_1 = require("macoolka-object");
//export type MonadI18NIO = MonadI18N<MessageKeys>
var init = function (M) {
if (M === void 0) { M = Module_1.default; }
/**
* Add message to a type
* @desczh
* 给一个类型增加message
* @since 0.2.0
*/
function withMessage(_a) {
var type = _a.type, message = _a.message;
return __assign(__assign({}, type), { _kind: 'message', message: message });
}
/**
* @since 0.2.0
*/
var string = withMessage({
type: t.string,
message: {
id: 'macoolka.io.string'
}
});
/**
* @since 0.2.0
*/
var number = withMessage({
type: t.number,
message: {
id: 'macoolka.io.number'
}
});
/**
* @since 0.2.0
*/
var boolean = withMessage({
type: t.boolean,
message: {
id: 'macoolka.io.boolean'
}
});
/**
* @since 0.2.0
*/
var Int = withMessage({
type: t.Int,
message: {
id: 'macoolka.io.integer'
}
});
/**
* @since 0.2.0
*/
var dateFromISOString = withMessage({
type: t.dateFromISOString,
message: {
id: 'macoolka.io.dateFromISOString'
}
});
/**
* @since 0.2.0
*/
var dateFromNumber = withMessage({
type: t.dateFromNumber,
message: {
id: 'macoolka.io.dateFromNumber'
}
});
/**
* @since 0.2.0
*/
var dateFromUnixTime = withMessage({
type: t.dateFromUnixTime,
message: {
id: 'macoolka.io.dateFromUnixTime'
}
});
/**
* @since 0.2.0
*/
var uuid = withMessage({
type: t.uuid,
message: {
id: 'macoolka.io.uuid'
}
});
/**
* @since 0.2.0
*/
var ipv4 = withMessage({
type: t.ipv4,
message: {
id: 'macoolka.io.ipv4'
}
});
/**
* @since 0.2.0
*/
var ipv6 = withMessage({
type: t.ipv6,
message: {
id: 'macoolka.io.ipv6'
}
});
/**
* @since 0.2.0
*/
var email = withMessage({
type: t.email,
message: {
id: 'macoolka.io.email'
}
});
/**
* @since 0.2.0
*/
var url = withMessage({
type: t.url,
message: {
id: 'macoolka.io.url'
}
});
/**
* @since 0.2.0
*/
var nonEmptyString = withMessage({
type: t.nonEmptyString,
message: {
id: 'macoolka.io.nonEmptyString'
}
});
/**
* @since 0.2.0
*/
var nonEmptyArray = function (codec, name) {
return withMessage({
type: t.nonEmptyArray(codec, name),
message: function () {
var value = (0, pipeable_1.pipe)(codec, O.fromPredicate(isMessageEncode), O.map(function (type) { return M.formatMessage((0, macoolka_predicate_1.isFunction)(type.message) ? type.message() : type.message); }), O.getOrElse(function () { return codec.name; }));
return ({
id: 'macoolka.io.nonEmptyArray',
value: {
value: value
}
});
}
});
};
/**
* @since 0.2.0
*/
var array = function (codec, name) {
return withMessage({
type: t.array(codec, name),
message: function () {
var value = (0, pipeable_1.pipe)(codec, O.fromPredicate(isMessageEncode), O.map(function (type) { return M.formatMessage((0, macoolka_predicate_1.isFunction)(type.message) ? type.message() : type.message); }), O.getOrElse(function () { return codec.name; }));
return ({
id: 'macoolka.io.array',
value: {
value: value
}
});
}
});
};
var void1 = withMessage({
type: t.void,
message: {
id: 'macoolka.io.void'
}
});
/**
* @since 0.2.0
*/
var stringMatch = function (value) { return withMessage({
type: t.stringMatch(value),
message: {
id: 'macoolka.io.string.match',
/* value: {
value
} */
}
}); };
/**
* @since 0.2.0
*/
var stringMaxLength = function (value) { return withMessage({
type: t.stringMaxLength(value),
message: {
id: 'macoolka.io.string.maxLength',
value: {
value: value
}
}
}); };
/**
* @since 0.2.0
*/
var stringMinLength = function (value) { return withMessage({
type: t.stringMinLength(value),
message: {
id: 'macoolka.io.string.minLength',
value: {
value: value
}
}
}); };
/**
* @since 0.2.0
*/
var numberMaxValue = function (value) { return withMessage({
type: t.numberMaxValue(value),
message: {
id: 'macoolka.io.number.maxValue',
value: {
value: value
}
}
}); };
/**
* @since 0.2.0
*/
var numberMinValue = function (value) { return withMessage({
type: t.numberMinValue(value),
message: {
id: 'macoolka.io.number.minValue',
value: {
value: value
}
}
}); };
/**
* @since 0.2.0
*/
var literal = function (value, name) {
return withMessage({
type: t.literal(value, name),
message: {
id: 'macoolka.io.literal',
value: {
value: value
}
}
});
};
/**
* Parse Erros(contains message) to MonidI18N
* @desczh
* 解析包含Message的Erros到MonidI18N
* @since 0.2.0
*/
var show = function (as) {
return t.show(as, {
showDecoder: function (type) {
return (0, pipeable_1.pipe)(type, O.fromPredicate(isMessageEncode), O.map(function (a) { return M.formatMessage((0, macoolka_predicate_1.isFunction)(a.message) ? a.message() : a.message); }));
},
showValue: function (value) { return (0, pipeable_1.pipe)(value, O.fromPredicate(isMessageValue), O.map(function (v) { return (0, pipeable_1.pipe)(v.messages, function (a) {
var messages = !(0, macoolka_predicate_1.isArray)(a) ? [a] : a;
return (0, pipeable_1.pipe)(messages, A.map(function (message) { return M.formatMessage(message); }), function (as) { return as.join('\n'); });
}); }), function (result) {
return O.isNone(result)
? O.some(M.formatMessage({ id: 'macoolka.io.invalid', value: { value: t.formatValue(value) } }))
: result;
}); }
});
};
/**
* @since 0.2.0
*/
var failMessage = function (messages, context, message) {
return t.failure({ _kind: 'message', messages: messages }, context, message);
};
return __assign(__assign({}, t), { literal: literal, numberMinValue: numberMinValue, numberMaxValue: numberMaxValue, stringMinLength: stringMinLength, stringMaxLength: stringMaxLength, stringMatch: stringMatch, void: void1, array: array, string: string, number: number, int: Int, boolean: boolean, nonEmptyArray: nonEmptyArray, nonEmptyString: nonEmptyString, uuid: uuid, email: email, ipv4: ipv4, ipv6: ipv6, url: url, dateFromISOString: dateFromISOString, dateFromNumber: dateFromNumber, dateFromUnixTime: dateFromUnixTime, show: show, failMessage: failMessage });
};
exports.init = init;
var isMessageValue = function (a) {
return (0, macoolka_predicate_1.isObject)(a) && (0, macoolka_object_1.get)(a, '_kind') === 'message';
};
var isMessageEncode = function (a) { return (0, macoolka_predicate_1.isObject)(a) && (0, macoolka_object_1.get)(a, '_kind') === 'message'; };
//# sourceMappingURL=i18nIO.js.map