Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  data.js   Sprache: JAVA

 
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 default class 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() {
    return function 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) {
    return function 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) {
    return function decorator(target, name, descriptor) {
      let oldValue = descriptor.value;

      descriptor.value = function () {
        if (!DataDecorator._getProperty(this.data, properties)) {
          return Promise.reject(new Exception(`Property disabled [${properties}], skip execution... continuing...`));
        }
        return oldValue.apply(this, arguments);
      };

      return descriptor;
    };
  }

  /**
   * 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
C=85 H=91 G=87

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

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.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge