All files / src/services/tables/builders RideEstimatesTableBuilder.js

92.5% Statements 37/40
78.57% Branches 11/14
100% Functions 3/3
92.5% Lines 37/40
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    1x 1x       1x   1x         1x   1x 3x     1x   1x 3x     1x 1x   1x       1x                   1x 1x 5x 1x 1x         3x                   3x   3x 1x 2x 1x 1x   1x     1x     3x             2x     2x                           3x 3x   3x 1x   2x 2x            
'use es6';
 
import emoji from 'node-emoji';
import {
  List,
  Map
} from 'immutable';
import Table from 'cli-table2';
 
import CostEstimateFormatter from '../formatters/CostEstimateFormatter';
// import Utilities from '../../../Utilities';
 
export default class RideEstimatesTableBuilder {
  static build(rideEstimates) {
    let table = RideEstimatesTableBuilder.buildInitialTable();
 
    let costEstimateRows = rideEstimates.costEstimates.map(costEstimate => {
      return RideEstimatesTableBuilder.buildCostEstimateRow(costEstimate);
    });
 
    costEstimateRows = costEstimateRows.sort(RideEstimatesTableBuilder.sortByDisplayName);
 
    costEstimateRows.forEach((costEstimateRow) => {
      table.push(costEstimateRow);
    });
 
    table.push(RideEstimatesTableBuilder.buildLocationRow(rideEstimates.start.name, false));
    table.push(RideEstimatesTableBuilder.buildLocationRow(rideEstimates.end.name, true));
 
    return table.toString();
  }
 
  static getTableHeaders() {
    return List.of(
      emoji.get('red_car'),
      emoji.get('money_with_wings'),
      emoji.get('arrows_clockwise'),
      emoji.get('hourglass_flowing_sand'),
      `${emoji.get('boom')} Primetime${emoji.get('boom')}`
    );
  }
 
  static buildInitialTable() {
    let table = new Table();
    let formattedHeaders = List(RideEstimatesTableBuilder.getTableHeaders()
                              .map(header => Map({ content: header, hAlign: 'center' })));
    table.push(formattedHeaders.toJS());
    return table;
  }
 
  static buildCostEstimateRow(costEstimate) {
    // TODO: Check for costEstimate.isValidEstimate
    return [
      costEstimate.displayName,
      CostEstimateFormatter.formatRange(costEstimate.priceRange),
      CostEstimateFormatter.formatDistance(costEstimate.estimatedDistance),
      CostEstimateFormatter.formatDuration(costEstimate.estimatedDuration),
      RideEstimatesTableBuilder.buildPrimetimePercentageSymbol(costEstimate.primetimePercentage).toJS(),
    ];
  }
 
  static buildPrimetimePercentageSymbol(primetimePercentage) {
    let primetime = `${primetimePercentage}%`;
 
    if (primetimePercentage === 0) {
      primetime = `${primetime} ${emoji.get('slightly_smiling_face')}`;
    } else if (primetimePercentage <= 25) {
      primetime = `${primetime} ${emoji.get('confused')}`;
    } else Iif (primetimePercentage <= 50) {
      primetime = `${primetime} ${emoji.get('slightly_frowning_face')}`;
    } else Iif (primetimePercentage <= 75) {
      primetime = `${primetime} ${emoji.get('cry')}`;
    } else {
      primetime = `${primetime} ${emoji.get('weary')}`;
    }
 
    return Map({
      content: primetime,
      hAlign: 'center',
    });
   }
 
  static buildLocationRow(name, isEnd) {
    let symbol = isEnd
      ? emoji.get('end')
      : emoji.get('round_pushpin');
    return [
      {
        colSpan: 1,
        content: symbol,
        hAlign: 'center'
      },
      {
        colSpan: 4,
        content: name
      },
    ]
  }
 
  static sortByDisplayName(costEstimate1, costEstimate2) {
    const displayName1 = costEstimate1[0].toLowerCase();
    const displayName2 = costEstimate2[0].toLowerCase();
 
    if (displayName1 < displayName2) {
      return -1;
    }
    Eif (displayName1 > displayName2) {
      return 1;
    }
 
    return 0;
  }
}