container.ts
Version:
Modular application framework
282 lines • 10.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
var error_1 = require("../error");
// TODO(M): Improve operator field documentation/testing/code readability.
/** Field error codes. */
var EFieldError;
(function (EFieldError) {
EFieldError[EFieldError["AndFieldError"] = 0] = "AndFieldError";
EFieldError[EFieldError["OrFieldError"] = 1] = "OrFieldError";
EFieldError[EFieldError["NotFieldError"] = 2] = "NotFieldError";
EFieldError[EFieldError["OptionalFieldError"] = 3] = "OptionalFieldError";
})(EFieldError = exports.EFieldError || (exports.EFieldError = {}));
/** Field error chain class. */
var FieldError = /** @class */ (function (_super) {
__extends(FieldError, _super);
function FieldError(code, value, cause) {
return _super.call(this, { name: EFieldError[code], value: value }, cause) || this;
}
return FieldError;
}(error_1.ErrorChain));
exports.FieldError = FieldError;
/** Field abstract base class. */
var Field = /** @class */ (function () {
function Field() {
}
/** Format method takes typed input and returns string output. */
Field.prototype.format = function (value, context) {
return lodash_1.toString(value);
};
/** Return an optional instance of this field. */
Field.prototype.optional = function (defaultValue) {
return new OptionalField(this, defaultValue);
};
/** And operator chaining. */
Field.prototype.and = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return new (AndField.bind.apply(AndField, __spreadArrays([void 0, this], fields)))();
};
/** Or operator chaining. */
Field.prototype.or = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return new (OrField.bind.apply(OrField, __spreadArrays([void 0, this], fields)))();
};
/** Not operator chaining. */
Field.prototype.not = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return this.and(this, new (NotField.bind.apply(NotField, __spreadArrays([void 0], fields)))());
};
return Field;
}());
exports.Field = Field;
/** Field operator abstract base class. */
var OperatorField = /** @class */ (function (_super) {
__extends(OperatorField, _super);
function OperatorField() {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
var _this = _super.call(this) || this;
_this.fields = fields;
return _this;
}
return OperatorField;
}(Field));
exports.OperatorField = OperatorField;
/**
* Optional field wrapper, if value is defined uses field in validation/formatting.
* If value is undefined default or null value is returned.
*/
var OptionalField = /** @class */ (function (_super) {
__extends(OptionalField, _super);
function OptionalField(field, defaultValue, context) {
var _this = _super.call(this) || this;
_this.field = field;
_this.defaultValue = defaultValue;
_this.formatDefault = _this.format(defaultValue, context);
return _this;
}
OptionalField.prototype.validate = function (value, context) {
try {
if (value == null) {
if (this.formatDefault == null) {
return null;
}
return this.field.validate(this.formatDefault, context);
}
return this.field.validate(value, context);
}
catch (error) {
throw new FieldError(EFieldError.OptionalFieldError, value, error);
}
};
OptionalField.prototype.format = function (value, context) {
try {
if (value == null) {
if (this.defaultValue == null) {
return null;
}
return this.field.format(this.defaultValue, context);
}
return this.field.format(value, context);
}
catch (error) {
throw new FieldError(EFieldError.OptionalFieldError, value, error);
}
};
return OptionalField;
}(Field));
exports.OptionalField = OptionalField;
/** And field operator, all input fields used in validation/formatting. */
var AndField = /** @class */ (function (_super) {
__extends(AndField, _super);
function AndField() {
return _super !== null && _super.apply(this, arguments) || this;
}
AndField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields.map(function (f) { return f.validate(value, context); }).reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.AndFieldError, value, error);
}
if (validated == null) {
throw new FieldError(EFieldError.AndFieldError, value);
}
return validated;
};
AndField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields.map(function (f) { return f.format(value, context); }).reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.AndFieldError, value, error);
}
if (formatted == null) {
throw new FieldError(EFieldError.AndFieldError, value);
}
return formatted;
};
return AndField;
}(OperatorField));
exports.AndField = AndField;
/** Or field operator, at least one input field used in validation/formatting. */
var OrField = /** @class */ (function (_super) {
__extends(OrField, _super);
function OrField() {
return _super !== null && _super.apply(this, arguments) || this;
}
OrField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields
.map(function (f) {
try {
return f.validate(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.OrFieldError, value, error);
}
if (validated == null) {
throw new FieldError(EFieldError.OrFieldError);
}
return validated;
};
OrField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields
.map(function (f) {
try {
return f.format(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.OrFieldError, value, error);
}
if (formatted == null) {
throw new FieldError(EFieldError.OrFieldError);
}
return formatted;
};
return OrField;
}(OperatorField));
exports.OrField = OrField;
/** Not field operator, all input fields expected to throw error/fail in validation/formatting. */
var NotField = /** @class */ (function (_super) {
__extends(NotField, _super);
function NotField() {
return _super !== null && _super.apply(this, arguments) || this;
}
NotField.prototype.validate = function (value, context) {
var validated;
try {
validated = this.fields
.map(function (f) {
try {
return f.validate(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.NotFieldError, value, error);
}
if (validated != null) {
throw new FieldError(EFieldError.NotFieldError, validated);
}
return validated;
};
NotField.prototype.format = function (value, context) {
var formatted;
try {
formatted = this.fields
.map(function (f) {
try {
return f.format(value, context);
}
catch (error) {
return null;
}
})
.reduce(function (p, c) { return (p != null ? p : c); }, null);
}
catch (error) {
throw new FieldError(EFieldError.NotFieldError, value, error);
}
if (formatted != null) {
throw new FieldError(EFieldError.NotFieldError, formatted);
}
return formatted;
};
return NotField;
}(OperatorField));
exports.NotField = NotField;
//# sourceMappingURL=field.js.map