All files / lib/model/nw/extensions/sap ExtenderBase.js

100% Statements 22/22
100% Branches 6/6
100% Functions 12/12
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88    1x                       11x                       62x                             75x 75x 70x                       75x 75x 774x     774x 774x 249x       75x 9x 214x 214x 214x       75x       68x       169x       1x  
"use strict";
 
const _ = require("lodash");
 
/**
 * Helper for vendor specific extensions.
 *
 * https://wiki.scn.sap.com/wiki/display/EmTech/SAP+Annotations+for+OData+Version+2.0
 * https://github.com/SAP/odata-vocabularies
 *
 * @class ExtenderBase
 */
class ExtenderBase {
  static get ATTRIBUTES_SCHEMA() {
    return [["schema-version", (value) => (value ? value : "0000")]];
  }
 
  /**
   * Creates and applies sap extension for schema element. It contains defined attributes.
   *
   * @static
   * @param {Object} item schema element to be extended, should contain .raw.$ object
   * @param {[Array]} attributes array of arrays defining attributes by name (kebap case) in first element and optional format function in second element
   * @memberof ExtenderBase
   */
  static applyAttributeExtension(item, attributes) {
    ExtenderBase.applyExtension(
      item,
      ExtenderBase.createAttributeExtension(item, attributes)
    );
  }
 
  /**
   * Applies extension object to schema element.
   *
   * @static
   * @param {Object} item schema element to be extended
   * @param {Object} extension extension object
   * @memberof ExtenderBase
   */
  static applyExtension(item, extension) {
    item.extensions.push(extension);
    Object.defineProperty(item, "sap", {
      get: () => extension,
    });
  }
 
  /**
   * Creates sap extension for accessing attribute values.
   *
   * @param {Object} [source] object with .raw.$ object
   * @param {[Array]} [attributes] array of arrays defining attributes by name (kebap case) in first element and optional format function in second element
   * @returns {Object} extension object
   */
  static createAttributeExtension(source, attributes) {
    let extension = {};
    attributes.forEach((attribute) => {
      let rawValue = source.raw.$
        ? source.raw.$[`sap:${attribute[0]}`]
        : undefined;
      let value = attribute[1] ? attribute[1](rawValue) : rawValue;
      Object.defineProperty(extension, _.camelCase(attribute[0]), {
        get: () => value,
      });
    });
 
    extension.extendLegacyApiObject = (api) => {
      attributes.forEach((attribute) => {
        let internalName = _.camelCase(attribute[0]);
        let apiName = _.upperFirst(internalName);
        api[apiName] = extension[internalName];
      });
    };
 
    return extension;
  }
 
  static defaultFalseBool(value) {
    return value === "true";
  }
 
  static defaultTrueBool(value) {
    return value !== "false";
  }
}
 
module.exports = ExtenderBase;