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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXNzaWduYWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9hc3NpZ25hYmxlLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxnRUFBZ0U7QUFDaEUsZ0VBQWdFO0FBQ2hFLCtDQUErQztBQUMvQyxnRUFBZ0U7Ozs7O0FBRWhFLDZDQUFvRTtBQUNwRSwwRUFBZ0Q7QUFFaEQsTUFBTSxZQUFZLEdBQUssS0FBSyxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUM7QUFDL0MsTUFBTSxRQUFRLEdBQVMsS0FBSyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUM7QUFDM0MsTUFBTSxTQUFTLEdBQVEsS0FBSyxDQUFDLFNBQVMsQ0FBQyxJQUFJLENBQUM7QUFDNUMsTUFBTSxXQUFXLEdBQU0sS0FBSyxDQUFDLFNBQVMsQ0FBQyxNQUFNLENBQUM7QUFDOUMsTUFBTSxXQUFXLEdBQU0sS0FBSyxDQUFDLF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WUgfSA9IHByb3BzW2ldO1xuXG4gICAgICAgICAgICBpZiAoZGVmYXVsdFZhbHVlICE9PSB2b2lkIDApXG4gICAgICAgICAgICAgICAgdGhpc1tuYW1lXSA9IGRlZmF1bHRWYWx1ZTtcblxuICAgICAgICAgICAgY29uc3Qgc3JjVmFsID0gb2JqW25hbWVdO1xuXG4gICAgICAgICAgICBpZiAoc3JjVmFsID09PSB2b2lkIDAgJiYgIXJlcXVpcmVkKVxuICAgICAgICAgICAgICAgIGNvbnRpbnVlO1xuXG4gICAgICAgICAgICBpZiAodmFsaWRhdGUgJiYgdHlwZSkge1xuICAgICAgICAgICAgICAgIGNvbnN0IHR5cGVOYW1lID0gZ2V0RGlzcGxheVR5cGVOYW1lKHRoaXMuY29uc3RydWN0b3IubmFtZSwgbmFtZSk7XG5cbiAgICAgICAgICAgICAgICB0eXBlKHR5cGVOYW1lLCBzcmNWYWwpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICB0aGlzW25hbWVdID0gaW5pdCA/IGluaXQobmFtZSwgc3JjVmFsLCBpbml0T3B0aW9ucywgdmFsaWRhdGUpIDogc3JjVmFsO1xuICAgICAgICB9XG4gICAgfVxufVxuIl19
|