timeperiodjs
Version:
Time Period Library
51 lines (50 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Constants_1 = require("../Constants");
class PeriodTimeChanger {
constructor(start, end) {
// NOTE: Dates have to be bonded to its source. Do not lose this bonding by using 'new Date()'
this._start = start;
this._end = end;
}
extendPeriod(value, type = Constants_1.TimeType.MILLISECONDS, changeStartTime) {
const timeType = changeStartTime ? '_start' : '_end';
const multiplier = PeriodTimeChanger.getMsMultiplier(type, changeStartTime);
const ms = value * multiplier;
this[timeType].setMilliseconds(this[timeType].getMilliseconds() + ms);
}
shortenPeriod(value, type = Constants_1.TimeType.MILLISECONDS, changeStartTime) {
const timeType = changeStartTime ? '_start' : '_end';
const multiplier = PeriodTimeChanger.getMsMultiplier(type, changeStartTime);
const ms = value * multiplier;
this[timeType].setMilliseconds(this[timeType].getMilliseconds() - ms);
}
static getMsMultiplier(type = Constants_1.TimeType.MILLISECONDS, changeStartTime) {
let multiplier = 1;
let foundType = false;
// seconds
if (!foundType && type !== Constants_1.TimeType.MILLISECONDS)
multiplier *= 1000;
else
foundType = true;
// minutes
if (!foundType && type !== Constants_1.TimeType.SECONDS)
multiplier *= 60;
else
foundType = true;
// hours
if (!foundType && type !== Constants_1.TimeType.MINUTES)
multiplier *= 60;
else
foundType = true;
// days
if (!foundType && type !== Constants_1.TimeType.HOURS)
multiplier *= 24; // then it has to be days
else
foundType = true;
if (changeStartTime)
multiplier *= -1;
return multiplier;
}
}
exports.default = PeriodTimeChanger;