constants-to-ts
parent
506b55bfe2
commit
f8cd997b13
@ -0,0 +1,13 @@
|
||||
import { NumberUtils } from './utils';
|
||||
|
||||
// Default TTL
|
||||
export const TTL_DEFAULT = {
|
||||
PAIRING_REQUEST: NumberUtils.timeAsMs(2, 'minutes'),
|
||||
DEVICE_UNPAIRING: NumberUtils.timeAsMs(4, 'days'),
|
||||
SESSION_REQUEST: NumberUtils.timeAsMs(4, 'days'),
|
||||
END_SESSION_MESSAGE: NumberUtils.timeAsMs(4, 'days'),
|
||||
TYPING_MESSAGE: NumberUtils.timeAsMs(1, 'minute'),
|
||||
ONLINE_BROADCAST: NumberUtils.timeAsMs(1, 'minute'),
|
||||
SESSION_ESTABLISHED: NumberUtils.timeAsMs(5, 'minutes'),
|
||||
REGULAR_MESSAGE: NumberUtils.timeAsMs(2, 'days'),
|
||||
};
|
@ -0,0 +1,47 @@
|
||||
type TimeUnit =
|
||||
| 'second'
|
||||
| 'seconds'
|
||||
| 'minute'
|
||||
| 'minutes'
|
||||
| 'hour'
|
||||
| 'hours'
|
||||
| 'day'
|
||||
| 'days';
|
||||
|
||||
export const timeAsMs = (value: number, unit: TimeUnit) => {
|
||||
// Converts a time to milliseconds
|
||||
// Valid units: second, minute, hour, day
|
||||
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
|
||||
|
||||
switch (unitAsSingular) {
|
||||
case 'second':
|
||||
return value * 1000;
|
||||
case 'minute':
|
||||
return value * 60 * 1000;
|
||||
case 'hour':
|
||||
return value * 60 * 60 * 1000;
|
||||
case 'day':
|
||||
return value * 24 * 60 * 60 * 1000;
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
export const msAsUnit = (value: number, unit: TimeUnit) => {
|
||||
// Converts milliseconds to your preferred unit
|
||||
// Valid units: second(s), minute(s), hour(s), day(s)
|
||||
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
|
||||
|
||||
switch (unitAsSingular) {
|
||||
case 'second':
|
||||
return value / 1000;
|
||||
case 'minute':
|
||||
return value / 60 / 1000;
|
||||
case 'hour':
|
||||
return value / 60 / 60 / 1000;
|
||||
case 'day':
|
||||
return value / 24 / 60 / 60 / 1000;
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue