-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix other places that use the setting
- Loading branch information
1 parent
6d13c8f
commit d24ba1b
Showing
6 changed files
with
97 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { TIMEUNIT, timeUnitToMs, msToTimeUnit } from './convertTimeUnit'; | ||
|
||
describe('timeUnitToMs function', () => { | ||
it('should correctly convert days to milliseconds', () => { | ||
expect(timeUnitToMs(TIMEUNIT.days, 1)).toBe(86400000); | ||
expect(timeUnitToMs(TIMEUNIT.days, 2)).toBe(172800000); | ||
expect(timeUnitToMs(TIMEUNIT.days, 0.5)).toBe(43200000); | ||
}); | ||
|
||
it('should correctly convert hours to milliseconds', () => { | ||
expect(timeUnitToMs(TIMEUNIT.hours, 1)).toBe(3600000); | ||
expect(timeUnitToMs(TIMEUNIT.hours, 2)).toBe(7200000); | ||
expect(timeUnitToMs(TIMEUNIT.hours, 0.5)).toBe(1800000); | ||
}); | ||
|
||
it('should correctly convert minutes to milliseconds', () => { | ||
expect(timeUnitToMs(TIMEUNIT.minutes, 1)).toBe(60000); | ||
expect(timeUnitToMs(TIMEUNIT.minutes, 2)).toBe(120000); | ||
expect(timeUnitToMs(TIMEUNIT.minutes, 0.5)).toBe(30000); | ||
}); | ||
|
||
it('should throw an error for invalid time units', () => { | ||
expect(() => timeUnitToMs('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - timeUnitToMs - invalid time unit'); | ||
}); | ||
}); | ||
|
||
describe('msToTimeUnit function', () => { | ||
it('should correctly convert milliseconds to days', () => { | ||
expect(msToTimeUnit(TIMEUNIT.days, 86400000)).toBe(1); // 1 day | ||
expect(msToTimeUnit(TIMEUNIT.days, 172800000)).toBe(2); // 2 days | ||
expect(msToTimeUnit(TIMEUNIT.days, 43200000)).toBe(0.5); // .5 days | ||
}); | ||
|
||
it('should correctly convert milliseconds to hours', () => { | ||
expect(msToTimeUnit(TIMEUNIT.hours, 3600000)).toBe(1); // 1 hour | ||
expect(msToTimeUnit(TIMEUNIT.hours, 7200000)).toBe(2); // 2 hours | ||
expect(msToTimeUnit(TIMEUNIT.hours, 1800000)).toBe(0.5); // .5 hours | ||
}); | ||
|
||
it('should correctly convert milliseconds to minutes', () => { | ||
expect(msToTimeUnit(TIMEUNIT.minutes, 60000)).toBe(1); // 1 min | ||
expect(msToTimeUnit(TIMEUNIT.minutes, 120000)).toBe(2); // 2 min | ||
expect(msToTimeUnit(TIMEUNIT.minutes, 30000)).toBe(0.5); // .5 min | ||
}); | ||
|
||
it('should throw an error for invalid time units', () => { | ||
expect(() => msToTimeUnit('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - msToTimeUnit - invalid time unit'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export enum TIMEUNIT { | ||
days = 'days', | ||
hours = 'hours', | ||
minutes = 'minutes', | ||
} | ||
|
||
export const timeUnitToMs = (unit: TIMEUNIT, timespan: number) => { | ||
switch (unit) { | ||
case TIMEUNIT.days: | ||
return timespan * 24 * 60 * 60 * 1000; | ||
|
||
case TIMEUNIT.hours: | ||
return timespan * 60 * 60 * 1000; | ||
|
||
case TIMEUNIT.minutes: | ||
return timespan * 60 * 1000; | ||
|
||
default: | ||
throw new Error('TimespanSettingInput - timeUnitToMs - invalid time unit'); | ||
} | ||
}; | ||
|
||
export const msToTimeUnit = (unit: TIMEUNIT, timespan: number) => { | ||
switch (unit) { | ||
case TIMEUNIT.days: | ||
return timespan / 24 / 60 / 60 / 1000; | ||
case TIMEUNIT.hours: | ||
return timespan / 60 / 60 / 1000; | ||
case TIMEUNIT.minutes: | ||
return timespan / 60 / 1000; | ||
default: | ||
throw new Error('TimespanSettingInput - msToTimeUnit - invalid time unit'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters