76 lines
11 KiB
JavaScript
76 lines
11 KiB
JavaScript
|
"use strict";
|
||
|
// -------------------------------------------------------------
|
||
|
// WARNING: this file is used by both the client and the server.
|
||
|
// Do not use any browser or node-specific API!
|
||
|
// -------------------------------------------------------------
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const errors_1 = require("../shared/errors");
|
||
|
const string_ends_with_1 = __importDefault(require("./string-ends-with"));
|
||
|
const arrayIndexOf = Array.prototype.indexOf;
|
||
|
const arrayMap = Array.prototype.map;
|
||
|
const arraySort = Array.prototype.sort;
|
||
|
const arrayFilter = Array.prototype.filter;
|
||
|
const arrayConcat = Array.prototype.concat;
|
||
|
const COMMAND_NAME_SUFFIX = 'Command';
|
||
|
function validateObjectProps(obj, dest) {
|
||
|
const objectName = dest.constructor.name;
|
||
|
const validKeys = arrayMap.call(dest.getAllAssignableProperties(), p => p.name);
|
||
|
const reportedProperties = arraySort.call(dest.getReportedProperties());
|
||
|
for (const key in obj) {
|
||
|
if (!(arrayIndexOf.call(validKeys, key) > -1 || key in dest))
|
||
|
throw new errors_1.ActionInvalidObjectPropertyError(objectName, key, reportedProperties);
|
||
|
}
|
||
|
}
|
||
|
function getDisplayTypeName(constructorName, propName) {
|
||
|
if (string_ends_with_1.default.call(constructorName, COMMAND_NAME_SUFFIX))
|
||
|
return propName;
|
||
|
return `${constructorName}.${propName}`;
|
||
|
}
|
||
|
class Assignable {
|
||
|
getAssignableProperties() {
|
||
|
return [];
|
||
|
}
|
||
|
getAllAssignableProperties() {
|
||
|
let parent = Object.getPrototypeOf(this);
|
||
|
let result = [];
|
||
|
while (parent && parent.getAssignableProperties) {
|
||
|
result = arrayConcat.call(result, parent.getAssignableProperties());
|
||
|
parent = Object.getPrototypeOf(parent);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
getNonReportedProperties() {
|
||
|
return [];
|
||
|
}
|
||
|
getReportedProperties() {
|
||
|
const props = arrayMap.call(this.getAllAssignableProperties(), prop => prop.name);
|
||
|
const nonReportedProps = this.getNonReportedProperties();
|
||
|
return arrayFilter.call(props, name => !(arrayIndexOf.call(nonReportedProps, name) > -1));
|
||
|
}
|
||
|
_assignFrom(obj, validate, initOptions = {}) {
|
||
|
if (!obj)
|
||
|
return;
|
||
|
if (validate)
|
||
|
validateObjectProps(obj, this);
|
||
|
const props = this.getAllAssignableProperties();
|
||
|
for (let i = 0; i < props.length; i++) {
|
||
|
const { name, type, required, init, defaultValue } = props[i];
|
||
|
if (defaultValue !== void 0)
|
||
|
this[name] = defaultValue;
|
||
|
const srcVal = obj[name];
|
||
|
if (srcVal === void 0 && !required)
|
||
|
continue;
|
||
|
if (validate && type) {
|
||
|
const typeName = getDisplayTypeName(this.constructor.name, name);
|
||
|
type(typeName, srcVal);
|
||
|
}
|
||
|
this[name] = init ? init(name, srcVal, initOptions, validate) : srcVal;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
exports.default = Assignable;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXNzaWduYWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9hc3NpZ25hYmxlLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxnRUFBZ0U7QUFDaEUsZ0VBQWdFO0FBQ2hFLCtDQUErQztBQUMvQyxnRUFBZ0U7Ozs7O0FBRWhFLDZDQUFvRTtBQUNwRSwwRUFBZ0Q7QUFFaEQsTUFBTSxZQUFZLEdBQUssS0FBSyxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUM7QUFDL0MsTUFBTSxRQUFRLEdBQVMsS0FBSyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUM7QUFDM0MsTUFBTSxTQUFTLEdBQV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