-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypioType.js
37 lines (37 loc) · 1.12 KB
/
TypioType.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypioType = void 0;
class TypioType {
// tslint:disable-next-line:variable-name
constructor(operators, castFn,
// tslint:disable-next-line:variable-name
name = "") {
this.operators = operators;
this.castFn = castFn;
this.name = name;
this._____$TYPIO$ = true;
}
cast(obj) {
const initValue = this.castFn(obj);
if (initValue.type === "error") {
return initValue;
}
for (const operator of this.operators) {
const operatorResult = operator(initValue.value);
if (operatorResult !== true) {
return {
type: "error",
operator: operator.label,
value: initValue.value,
error: typeof operatorResult === "string" ? operatorResult : undefined,
};
}
}
return {
type: "success",
value: initValue.value,
};
}
}
exports.TypioType = TypioType;
exports.default = TypioType;