All files duration.ts

100% Statements 16/16
50% Branches 1/2
100% Functions 5/5
100% Lines 14/14

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 451x   1x 1x   1x                                         12x 12x       15x     1x 60x 12x     1x 12x 12x   1x  
import Formater from './formater';
 
export default class Duration {
  static readonly FORMAT_TOKENS: RegExp = /\*?[Hh]|\*?m+|\*?s+|./g;
 
  static readonly INPUT_TYPES = [
    {
      type: 'S',
      millisecondValue: 1,
    },
    {
      type: 's',
      millisecondValue: 1000,
    },
    {
      type: 'm',
      millisecondValue: 60000,
    },
    {
      type: 'h',
      millisecondValue: 3600000,
    },
  ];
 
  private _millisecond: number;
 
  constructor(private duration: number, private Iunit: string = 'S') {
    this._millisecond = Duration.convertToMillisecond(duration, unit);
  }
 
  get millisecond(): number {
    return this._millisecond;
  }
 
  private static convertToMillisecond(value: number, type: string): number {
    const { millisecondValue } = Duration.INPUT_TYPES.filter(v => v.type === type)[0];
    return value * millisecondValue;
  }
 
  public format(token: string): string {
    const formatter = new Formater(this, token);
    return formatter.format();
  }
}