All files / latest map.js

100% Statements 119/119
100% Branches 9/9
100% Functions 9/9
100% Lines 119/119

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x  
const Json = require('./json.js');
const makeObject = require('./src/handlers/make/makeObject.js');
const mergeFunctions = require('./src/helpers/mergeFunctions');
const loadDefaultSettings = require('./src/helpers/loadDefaultSettings');
const validateReponseAndPassDefault = require('./src/helpers/validateReponseAndPassDefault');
 
const isArrayNotEmpty = (value) => (Array.isArray(value) && value.length > 0);
const isNoArray = (value) => (value !== undefined && !Array.isArray(value));
const validateValue = (value) => (isNoArray(value) || isArrayNotEmpty(value));
 
class Map {
  /**
   * Construct a map containing origin object, destination object and settings
   * @param {Object} origin - origin object, from where data should be obtained
   * @param {Object} destination - destination object, to where data should be mapped
   * @param {Object} settings object, currently only support for fatalErrorOnCreate,
   * if true, an arror will be thrown on translation if query is not met.
   * @param {Object} functions object of functions that can be called within query.
 
   */
  constructor(origin, destination, settings, functions) {
    this.settings = loadDefaultSettings(makeObject(settings));
    this.originObject = new Json(origin, settings);
    this.destinationObject = new Json(destination, settings);
    this.functions = makeObject(functions);
  }
 
  /**
   * Translate a single value into destination object. Query will stop after first match.
   * @param {String} originPath - path from where data should be obtained from origin object
   * @param {originPath} destinationPath - path to where data should be mapped into destination
   * object
   * @param {Object} functions object of functions that can be called within query.
   */
  translate(originPath, destinationPath, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    const value = this.originObject.get(originPath, funcs);
    if (this.settings.mapIfNotFound || value !== undefined) {
      this.destinationObject.set(
        destinationPath,
        validateReponseAndPassDefault(value, undefined, this.settings.defaultGetResponse),
        this.settings.fatalErrorOnCreate, funcs,
      );
    }
  }
 
  /**
   * Translate all values into destination object. Destination will be an array with all results
   * from origin query.
   * @param {String} originPath - path from where data should be obtained from origin object
   * @param {originPath} destinationPath - path to where data should be mapped into destination
   * object
   * @param {Object} functions object of functions that can be called within query.
   */
  translateAll(originPath, destinationPath, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    const values = this.originObject.getAll(originPath, funcs);
    if (this.settings.mapIfNotFound || validateValue(values)) {
      this.destinationObject.setAll(
        destinationPath,
        validateReponseAndPassDefault(values, [], this.settings.defaultGetAllResponse),
        this.settings.fatalErrorOnCreate,
        funcs,
      );
    }
  }
 
  /**
   * Translate single value into destination object. Destination will be the first matching value
   * from origin path, mapped into all qualified destinations.
   * @param {String} originPath - path from where data should be obtained from origin object
   * @param {originPath} destinationPath - path to where data should be mapped into destination
   * object
   * @param {Object} functions object of functions that can be called within query.
   */
  translateOneToAll(originPath, destinationPath, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    const value = this.originObject.get(originPath, funcs);
    if (this.settings.mapIfNotFound || value !== undefined) {
      this.destinationObject.setAll(
        destinationPath,
        validateReponseAndPassDefault(value, undefined, this.settings.defaultGetResponse),
        this.settings.fatalErrorOnCreate,
        funcs,
      );
    }
  }
 
  /**
   * Translate all values into destination object. The single destination will be filled with an
   * array filled with all results from the originPath query.
   * @param {String} originPath - path from where data should be obtained from origin object
   * @param {originPath} destinationPath - path to where data should be mapped into destination
   * object
   * @param {Object} functions object of functions that can be called within query.
   */
  translateAllToOne(originPath, destinationPath, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    const values = this.originObject.getAll(originPath, funcs);
    if (this.settings.mapIfNotFound || validateValue(values)) {
      this.destinationObject.set(
        destinationPath,
        validateReponseAndPassDefault(values, [], this.settings.defaultGetAllResponse),
        this.settings.fatalErrorOnCreate,
        funcs,
      );
    }
  }
 
  /**
   * Exports the destination object
   * @returns {Object} destination object
   */
  export() {
    return this.destinationObject.object;
  }
}
 
module.exports = Map;