convertTo method

Dimension convertTo(
  1. dynamic conversions
)

Converts a number from one unit into another conversions == 'px', 's' , ... or { length: 'px', duration: 's', angle: 'rad' } String | Map<String, String>

Implementation

Dimension convertTo(dynamic conversions) {
  Map<String, String> conversionsMap;
  final derivedConversions = <String, String>{};
  Map<String, double> group;
  String targetUnit; //new unit
  final unit = this.unit.clone();
  var value = this.value;

  if (conversions is String) {
    for (final i in UnitConversions.groups.keys) {
      // groups = length, duration, angle
      if (UnitConversions.groups[i].containsKey(conversions)) {
        derivedConversions.clear();
        derivedConversions[i] = conversions;
      }
    }
    conversionsMap = derivedConversions; // {length: 'px', ..}
  } else {
    conversionsMap = conversions as Map<String, String>;
  }

  // maths on units
  // [atomicUnit] origina unit
  // ignore: avoid_positional_boolean_parameters
  String applyUnit(String atomicUnit, bool denominator) {
    if (group.containsKey(atomicUnit)) {
      if (denominator) {
        value = value / (group[atomicUnit] / group[targetUnit]);
      } else {
        value = value * (group[atomicUnit] / group[targetUnit]);
      }
      return targetUnit;
    }
    return atomicUnit;
  }

  for (final groupName in conversionsMap.keys) {
    if (conversionsMap.containsKey(groupName)) {
      targetUnit = conversionsMap[groupName];
      group = UnitConversions.groups[groupName];

      unit.map(applyUnit);
    }
  }

  unit.cancel();

  return Dimension(value, unit);

//2.3.1
//  Dimension.prototype.convertTo = function (conversions) {
//      var value = this.value, unit = this.unit.clone(),
//          i, groupName, group, targetUnit, derivedConversions = {}, applyUnit;
//
//      if (typeof conversions === 'string') {
//          for(i in unitConversions) {
//              if (unitConversions[i].hasOwnProperty(conversions)) {
//                  derivedConversions = {};
//                  derivedConversions[i] = conversions;
//              }
//          }
//          conversions = derivedConversions;
//      }
//      applyUnit = function (atomicUnit, denominator) {
//        /*jshint loopfunc:true */
//          if (group.hasOwnProperty(atomicUnit)) {
//              if (denominator) {
//                  value = value / (group[atomicUnit] / group[targetUnit]);
//              } else {
//                  value = value * (group[atomicUnit] / group[targetUnit]);
//              }
//
//              return targetUnit;
//          }
//
//          return atomicUnit;
//      };
//
//      for (groupName in conversions) {
//          if (conversions.hasOwnProperty(groupName)) {
//              targetUnit = conversions[groupName];
//              group = unitConversions[groupName];
//
//              unit.map(applyUnit);
//          }
//      }
//
//      unit.cancel();
//
//      return new Dimension(value, unit);
//  };
}