All files / src/services/converters DurationConverter.js

90.91% Statements 10/11
66.67% Branches 2/3
100% Functions 0/0
90.91% Lines 10/11
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    1x   1x 1x       2x 2x 2x 2x             6x   4x       2x                  
'use es6';
 
import convert from 'convert-units';
 
import Duration from '../../data/Duration';
import TimeUnit from '../../data/TimeUnit';
 
export default class DurationConverter {
  static convert(duration, toUnit) {
    const fromUnitIdentifier = DurationConverter.getUnitConversionIdentifier(duration.unit);
    const toUnitIdentifier = DurationConverter.getUnitConversionIdentifier(toUnit);
    const convertedLength = convert(duration.length).from(fromUnitIdentifier).to(toUnitIdentifier);
    return new Duration({
      length: convertedLength,
      unit: toUnit
    });
  }
 
  static getUnitConversionIdentifier(unit) {
    switch (unit) {
      case TimeUnit.SECOND: {
        return 's';
      }
 
      case TimeUnit.MINUTE: {
        return 'min';
      }
 
      default: {
        throw new TypeError('Unknown unit');
      }
    }
  }
}