import {Exception} from '../util/exception'; import {Utilities} from '../util/utilities';
/** * This {Decorator} class is used to check whether a property is present in the data before executing a method.
*/
export defaultclass DataDecorator {
/** * This function can be used as a decorator to intercept a method and, based on {this.data}, * whether to execute it or not. * * @example * // @Decorators.Data.notEmpty() * @public
*/ static notEmpty() { returnfunction decorator(target, name, descriptor) {
let oldValue = descriptor.value;
descriptor.value = function () { if (Utilities.isObjectEmpty(this.data)) { return Promise.reject(new Exception('No data here, nothing to render!'));
} return oldValue.apply(this, arguments);
};
return descriptor;
};
}
/** * This function can be used as a decorator to intercept a method and, based on {this.data}, * whether to execute it or not. * * @example * // @Decorators.Data.requires('canvas.graph') * @param {string} properties - the properties separated by a dot, e.g. 'data.property' * @public
*/ static requires(properties) { returnfunction decorator(target, name, descriptor) {
let oldValue = descriptor.value;
descriptor.value = function () { if (!DataDecorator._hasData(DataDecorator._getProperty(this.data, properties))) { return Promise.reject(new Exception(`No data here [${properties}], nothing to render... continuing...`));
} return oldValue.apply(this, arguments);
};
return descriptor;
};
}
/** * This function can be used as a decorator to intercept a method and, based on {this.data}, * execute it if the property is set to true otherwise rejects the promise. * * @example * // @Decorators.Data.enabled('canvas.texTypesetting') * @param {string} properties - the properties separated by a dot, e.g. 'data.property' * @public
*/ static enabled(properties) { returnfunction decorator(target, name, descriptor) {
let oldValue = descriptor.value;
/** * Helper method to iterate over an object * @param {Object} obj - the object to search the property for * @param {string} propertyPath - property separated by dot * @private
*/ static _getProperty(obj, propertyPath) {
let tmp = obj;
if (tmp && propertyPath) {
let properties = propertyPath.split('.');
for (let property of properties) { if (!Object.prototype.hasOwnProperty.call(tmp, property)) {
tmp = undefined; break;
} else {
tmp = tmp[property];
}
}
}
return tmp;
}
/** * Helper method to check whether a property has data * @param {Object} obj - the object to check * @private
*/ static _hasData(obj) { return obj && ((typeof obj === 'string' && obj) || (obj instanceof Array && obj.length) || (obj instanceof Object && Object.values(obj).length));
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.