All files / datamodel/src/operator pure-operators.js

100% Statements 6/6
100% Branches 0/0
100% Functions 4/4
100% Lines 2/2

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                    1x                                                                                                                                                           1x  
/**
 * Creates a new variable calculated from existing variable. This method expects definition of the newly created
 * variable and a function which resolves value of the new variable from existing variables.
 *
 * This operator is not compose supported.
 *
 * Creates a new measure based on existing variables
 * @example
 *  //@preamble_start
 *  Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 *  const data = params[0];
 *  const schema = params[1];
 *  const DataModel = muze.DataModel;
 *  const dm = new DataModel(data, schema);
 *  //@preamble_end
 *  // DataModel instance is created from https://www.charts.com/static/cars.json data,
 *  // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted from
 *  // muze namespace and assigned to the DataModel variable.
 *  const creatorFn = calculateVariable({
 *      name: 'powerToWeight',
 *      type: 'measure' // Schema of variable
 *  }, ['horsepower', 'weight_in_lbs', (hp, weight) => hp / weight ]);
 *  const outputDM = creatorFn(dm);
 *  //@preamble_start
 *  printDM(outputDM);
 *  });
 *  //@preamble_end
 *
 * Creates a new dimension based on existing variables
 * @example
 *  //@preamble_start
 *  Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 *  const data = params[0];
 *  const schema = params[1];
 *  const DataModel = muze.DataModel;
 *  const dm = new muze.DataModel(data, schema);
 *  //@preamble_end
 *  // DataModel instance is created from https://www.charts.com/static/cars.json data,
 *  // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted from
 *  // muze namespace and assigned to the DataModel variable.
 *
 *  const creatorFn = calculateVariable(
 *     {
 *       name: 'Efficiency',
 *       type: 'dimension'
 *     }, ['horsepower', (hp) => {
 *      if (hp < 80) { return 'low'; },
 *      else if (hp < 120) { return 'moderate'; }
 *      else { return 'high' }
 *  }]);
 *   const outputDM = creatorFn(dm);
 *  //@preamble_start
 *  printDM(outputDM);
 *  });
 *  //@preamble_end
 *
 * @public
 * @namespace DataModel
 * @segment Operators
 *
 * @param {Schema} schema: Schema of newly defined variable
 * @param {VariableResolver} resolver: Resolver format to resolve the current variable
 *
 * @return {PreparatorFunction} Function which expects an instance of DataModel on which the operator needs to be
 *      applied.
 */
export const calculateVariable = (...args) => dm => dm.calculateVariable(...args);
 
/**
 * Performs sorting according to the specified sorting details. Like every other operator it doesn't mutate the current
 * DataModel instance on which it was called, instead returns a new DataModel instance containing the sorted data.
 *
 * This operator support multi level sorting by listing the variables using which sorting needs to be performed and
 * the type of sorting `ASC` or `DESC`.
 *
 * In the following example, data is sorted by `Origin` field in `DESC` order in first level followed by another level
 * of sorting by `Acceleration` in `ASC` order.
 *
 * @example
 *  //@preamble_start
 *  Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 *  const data = params[0];
 *  const schema = params[1];
 *  const DataModel = muze.DataModel;
 *  const dm = new DataModel(data, schema);
 *  //@preamble_end
 *  // DataModel instance is created from https://www.charts.com/static/cars.json data,
 *  // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted from
 *  // muze namespace and assigned to the DataModel variable.
 *
 *  const sort = DataModel.Operators.sort;
 *  const preparatorFn = sort([
 *      ['Origin', 'DESC']
 *      ['Acceleration'] // Default value is ASC
 *  ]);
 *  const outputDM = preparatorFn(dm);
 *  //@preamble_start
 *  printDM(outputDM);
 *  });
 *  //@preamble_end
 *
 * @text
 * This operator also provides another sorting mechanism out of the box where order is applied to a variable by
 * comparing values of another variable.
 * Assume an instance of DataModel created from {@link /static/cars.json | this} data. Now, the data in this
 * model can be sorted by *Origin* field according to the average value of all *Acceleration* for a
 * particular *Origin* value. We would expect an output where *Origin* with lowest average *Acceleration* would come
 * first, then the next lower average, all the way to Origin with the highest average *Acceleration* is the last
 * entry of the array.
 *
 * @example
 *  //@preamble_start
 *  Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 *  const data = params[0];
 *  const schema = params[1];
 *   const DataModel = muze.DataModel;
 *  const dm = new muze.DataModel(data, schema);
 *  //@preamble_end
 *  // DataModel instance is created from https://www.charts.com/static/cars.json data,
 *  // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted
 *  // from muze namespace and assigned to DataModel variable.
 *  const avg = DataModel.Stats.avg;
 *  const sort = DataModel.Operators.sort;
 *  const preparatorFn = sort([
 *      ['Origin', ['Acceleration', (a, b) => avg(...a.Acceleration) - avg(...b.Acceleration)]]
 *  ]);
 *  const outputDM = preparatorFn(dm);
 *  //@preamble_start
 *  printDM(outputDM);
 *  });
 *  //@preamble_end
 *
 * @text
 * If `groupBy` is applied post sorting, then sorting order is destroyed.
 *
 * @public
 * @namespace DataModel
 * @segment Operators
 *
 * @param {Array.<Array>} sortingDetails - Sorting details based on which the sorting will be performed.
 *
 * @return {PreparatorFunction} Function which expects an instance of DataModel on which the operator needs to be
 *      applied.
 */
export const sort = (...args) => dm => dm.sort(...args);