src/app/shared/utils/time-duration.ts
Properties |
Methods |
Static toNumber | ||||||||
toNumber(str: string)
|
||||||||
Defined in src/app/shared/utils/time-duration.ts:23
|
||||||||
Parameters :
Returns :
number
|
Static toString | ||||||||
toString(time: number)
|
||||||||
Defined in src/app/shared/utils/time-duration.ts:41
|
||||||||
Parameters :
Returns :
string
|
Static validator | ||||||||
validator(control: FormControl)
|
||||||||
Defined in src/app/shared/utils/time-duration.ts:15
|
||||||||
Parameters :
Returns :
{ invalidDuration: boolean; }
|
Readonly Static Private D |
D:
|
Default value : 'd'
|
Defined in src/app/shared/utils/time-duration.ts:7
|
Readonly Static Private Day |
Day:
|
Default value : 28800000
|
Defined in src/app/shared/utils/time-duration.ts:12
|
Readonly Static Private H |
H:
|
Default value : 'h'
|
Defined in src/app/shared/utils/time-duration.ts:6
|
Readonly Static Private Hour |
Hour:
|
Default value : 3600000
|
Defined in src/app/shared/utils/time-duration.ts:11
|
Readonly Static Private M |
M:
|
Default value : 'm'
|
Defined in src/app/shared/utils/time-duration.ts:5
|
Readonly Static Private Minute |
Minute:
|
Default value : 60000
|
Defined in src/app/shared/utils/time-duration.ts:10
|
Readonly Static Private W |
W:
|
Default value : 'w'
|
Defined in src/app/shared/utils/time-duration.ts:8
|
Readonly Static Private Week |
Week:
|
Default value : 144000000
|
Defined in src/app/shared/utils/time-duration.ts:13
|
import {FormControl} from "@angular/forms";
export class TimeDuration {
private static readonly M = 'm';
private static readonly H = 'h';
private static readonly D = 'd';
private static readonly W = 'w';
private static readonly Minute = 60000;
private static readonly Hour = 3600000;
private static readonly Day = 28800000;
private static readonly Week = 144000000;
static validator(control: FormControl) {
const regexp = /(^\d{1,2}[w]\s\d{1,2}[d]\s\d{1,2}[h]\s\d{1,2}[m]$)|(^\d{1,2}[w]\s\d{1,2}[d]\s\d{1,2}[h]$)|(^\d{1,2}[w]\s\d{1,2}[d]\s\d{1,2}[m]$)|(^\d{1,2}[w]\s\d{1,2}[h]\s\d{1,2}[m]$)|(^\d{1,2}[d]\s\d{1,2}[h]\s\d{1,2}[m]$)|(^\d{1,2}[w]\s\d{1,2}[d]$)|(^\d{1,2}[w]\s\d{1,2}[h]$)|(^\d{1,2}[w]\s\d{1,2}[m]$)|(^\d{1,2}[d]\s\d{1,2}[h]$)|(^\d{1,2}[d]\s\d{1,2}[m]$)|(^\d{1,2}[h]\s\d{1,2}[m]$)|(^\d{1,2}[w]$)|(^\d{1,2}[d]$)|(^\d{1,2}[h]$)|(^\d{1,2}[m]$)/;
if (control.value && control.value.length > 0 && !regexp.test(control.value.trim())) {
return {invalidDuration: true}
}
return null;
}
static toNumber(str: string): number {
let durations = str.split(/\s/).map(e => e.trim()).map(e => {
let type = e.slice(-1);
let num = parseInt(e.slice(0, -1));
switch (type) {
case TimeDuration.M:
return num * TimeDuration.Minute;
case TimeDuration.H:
return num * TimeDuration.Hour;
case TimeDuration.D:
return num * TimeDuration.Day;
case TimeDuration.W:
return num * TimeDuration.Week;
}
});
return durations.reduce((a, b) => a + b, 0)
}
static toString(time: number): string {
let str = "";
let weeks = Math.floor(time / TimeDuration.Week);
if (weeks) {
str += weeks + TimeDuration.W + " ";
}
time %= TimeDuration.Week;
let days = Math.floor(time / TimeDuration.Day);
if (days) {
str += days + TimeDuration.D + " ";
}
time %= TimeDuration.Day;
let hours = Math.floor(time / TimeDuration.Hour);
if (hours) {
str += hours + TimeDuration.H + " ";
}
time %= TimeDuration.Hour;
let minutes = Math.floor(time / TimeDuration.Minute);
if (minutes) {
str += minutes + TimeDuration.M
}
return str.trim();
}
}