@foxy.io/sdk
Version:
Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.
46 lines (45 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTimeFromFrequency = exports.InvalidFrequencyError = void 0;
/**
* Instances of this error are thrown by `getTimeFromFrequency` when
* provided with unsupported frequency.
*/
class InvalidFrequencyError extends Error {
constructor(frequency) {
super(`Invalid frequency "${frequency}".`);
}
}
exports.InvalidFrequencyError = InvalidFrequencyError;
/**
* Gives a rough estimate for a number of milliseconds in the given frequency.
*
* IMPORTANT: this function SHOULD NOT be used to calculate precise dates
* as it doesn't account for leap years, months of variable length, etc. It works
* best for broad estimates and comparing frequencies.
*
* @param frequency Frequency as positive integer + units (y, m, w, d for years, months, weeks and days respectively). You can use .5m for twice a month. Example: 1m (1 month), 4y (4 years).
* @throws InvalidFrequencyError when provided with unsupported frequency.
* @returns Estimated number of milliseconds in the given frequency.
*/
function getTimeFromFrequency(frequency) {
const DAY = 86400000;
const MONTH = 31 * DAY;
if (frequency === '.5m')
return MONTH / 2;
const YEAR = 365 * DAY;
const WEEK = 7 * DAY;
const count = parseInt(frequency.substring(0, frequency.length - 1));
if (isNaN(count))
throw new InvalidFrequencyError(frequency);
if (frequency.endsWith('y'))
return count * YEAR;
if (frequency.endsWith('m'))
return count * MONTH;
if (frequency.endsWith('w'))
return count * WEEK;
if (frequency.endsWith('d'))
return count * DAY;
throw new InvalidFrequencyError(frequency);
}
exports.getTimeFromFrequency = getTimeFromFrequency;