本文整理匯總了TypeScript中src/monet.Validation類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Validation類的具體用法?TypeScript Validation怎麽用?TypeScript Validation使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Validation類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: log3
function log3(message: Validation<string, string>): void {
if (message.isSuccess()) {
console.log(message.success());
} else if (message.isFail()) {
console.warn(message.fail());
}
}
示例2: log
function log(message: Validation<string, string>) {
return message.cata(err => IO(() => {
console.error(err);
}), msg => IO(() => {
console.log(msg);
}));
}
示例3: log2
function log2(message: Validation<string, string>): IO<void> {
return message.bimap(err => IO(() => {
console.error(err);
}), msg => IO(() => {
console.log(msg);
})).cata(e => e, m => m);
}
示例4:
const wrappedGreeting: Validation<string, string> = wrapped.bind(v => v.payload ?
Validation.success<string, string>(v.payload) :
Validation.fail<string, string>('NonePayload'));
示例5: getMessage
payload: string;
}
function getMessage(msg: IMessage) {
if (msg && msg.hasOwnProperty('type')) {
return Success<string, IMessage>(msg);
}
return Fail<string, IMessage>('BadMessageFormat.');
}
const wrapped = getMessage({type: 'MESSAGE', payload: 'Hello World'});
console.assert(!(wrapped.isSuccess() === wrapped.isFail()));
const wrappedGreeting: Validation<string, string> = wrapped.bind(v => v.payload ?
Validation.success<string, string>(v.payload) :
Validation.fail<string, string>('NonePayload'));
const wrappedType = wrapped.flatMap(getType);
const unpacked = wrappedGreeting.chain(g => wrappedType.map(t => `Type: ${t} - Body: ${g}`));
log(unpacked).run();
log2(getMessage(null).flatMap(getType)).performUnsafeIO();
log3(getMessage({type: 'x', payload: null}).chain(getType));
const nameError: string = Fail('-- Adamovisch').failMap(n => n.split(' ').shift()).fail();
function getHttpError(code: number) {
return Fail<number, string>(code);
}