rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
45 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IncrementTime = void 0;
const types_1 = require("../types");
/**
* @description Increments a given date by a specified amount of time based on the unit provided.
*
* @param date The starting date to increment.
* @param value The number of units to increment. If not a valid number, the original timestamp is returned.
* @param unit The unit of time to increment. Possible values are:
* - `'day'`: Increment by days.
* - `'hour'`: Increment by hours.
* - `'minute'`: Increment by minutes.
* - `'month'`: Increment by months.
* - `'week'`: Increment by weeks.
* - `'year'`: Increment by years.
*
* @returns The incremented timestamp as a number. If the input date is invalid, the current timestamp is returned.
*
* @example
* ```typescript
* const now = new Date();
* const incrementedTime = IncrementTime(now, 3, 'day'); // Adds 3 days to the current date.
* console.log(new Date(incrementedTime)); // Logs the incremented date.
* ```
*/
const IncrementTime = (date, value, unit) => {
if (!(date instanceof Date))
return new Date().getTime();
const time = date.getTime();
if (typeof value !== 'number')
return time;
switch (unit) {
case 'day': return (types_1.ONE_DAY * value) + time;
case 'hour': return (types_1.ONE_HOUR * value) + time;
case 'minute': return (types_1.ONE_MINUTE * value) + time;
case 'month': return (types_1.ONE_MONTH * value) + time;
case 'week': return (types_1.ONE_WEEK * value) + time;
case 'year': return (types_1.ONE_YEAR * value) + time;
default: return time;
}
};
exports.IncrementTime = IncrementTime;
exports.default = exports.IncrementTime;
//# sourceMappingURL=increment-time.util.js.map