UNPKG

kits-core

Version:
346 lines (307 loc) 9.04 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDurationSummary = exports.getDurationDescription = exports.getDurationTime = exports.formatDuration = exports.formatDurationHMS = exports.formatTimeHMS = exports.formatTime = exports.parseDuration = exports.timeConversion = undefined; var _moment = require('moment'); var _moment2 = _interopRequireDefault(_moment); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * * @param milliseconds * @returns {string} * @inheritDoc https://www.convertunits.com/from/milliseconds/to/year */ var timeConversion = exports.timeConversion = function timeConversion(milliseconds) { var seconds = (milliseconds / 1000).toFixed(1); var minutes = (milliseconds / (1000 * 60)).toFixed(1); var hours = (milliseconds / (1000 * 60 * 60)).toFixed(1); var days = (milliseconds / (1000 * 60 * 60 * 24)).toFixed(1); var months = (milliseconds / 2629746000).toFixed(1); var years = (milliseconds / 31556952000).toFixed(1); // let months = (milliseconds / (1000 * 60 * 60 * 24 * 31)).toFixed(1); // let years = (milliseconds / (1000 * 60 * 60 * 24 * 31 * 12)).toFixed(1); if (seconds < 60) { return seconds + ' Sec'; } if (minutes < 60) { return minutes + ' Min'; } if (hours < 24) { return hours + ' Hrs'; } if (days < 31) { return days + ' Days'; } if (months < 12) { return months + ' Months'; } return years + ' Years'; }; /** * * @param duration * @returns {{milliseconds: *, hours: number, seconds: number, minutes: number, days: number}} */ var parseDuration = exports.parseDuration = function parseDuration(duration) { var remain = duration; var years = Math.floor(remain / 31556952000); remain %= 31556952000; var months = Math.floor(remain / 2629746000); remain %= 2629746000; var days = Math.floor(remain / (1000 * 60 * 60 * 24)); remain %= 1000 * 60 * 60 * 24; var hours = Math.floor(remain / (1000 * 60 * 60)); remain %= 1000 * 60 * 60; var minutes = Math.floor(remain / (1000 * 60)); remain %= 1000 * 60; var seconds = Math.floor(remain / 1000); remain %= 1000; var milliseconds = remain; return { years: years, months: months, days: days, hours: hours, minutes: minutes, seconds: seconds, milliseconds: milliseconds }; }; /** * * @param o * @param useMilli * @returns {string} */ var formatTime = exports.formatTime = function formatTime(o) { var useMilli = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var parts = []; if (o.years) { var ret = o.years + ' year'; if (o.years !== 1) { ret += 's'; } parts.push(ret); } if (o.months) { var _ret = o.months + ' month'; if (o.months !== 1) { _ret += 's'; } parts.push(_ret); } if (o.days) { var _ret2 = o.days + ' day'; if (o.days !== 1) { _ret2 += 's'; } parts.push(_ret2); } if (o.hours) { var _ret3 = o.hours + ' hour'; if (o.hours !== 1) { _ret3 += 's'; } parts.push(_ret3); } if (o.minutes) { var _ret4 = o.minutes + ' minute'; if (o.minutes !== 1) { _ret4 += 's'; } parts.push(_ret4); } if (o.seconds) { var _ret5 = o.seconds + ' second'; if (o.seconds !== 1) { _ret5 += 's'; } parts.push(_ret5); } if (useMilli && o.milliseconds) { var _ret6 = o.milliseconds + ' millisecond'; if (o.milliseconds !== 1) { _ret6 += 's'; } parts.push(_ret6); } if (parts.length === 0) { return 'instantly'; } return parts.join(' '); }; var formatTimeHMS = exports.formatTimeHMS = function formatTimeHMS(o) { var hours = o.hours.toString(); if (hours.length === 1) hours = '0' + hours; var minutes = o.minutes.toString(); if (minutes.length === 1) minutes = '0' + minutes; var seconds = o.seconds.toString(); if (seconds.length === 1) seconds = '0' + seconds; return hours + ':' + minutes + ':' + seconds; }; /** * * @param duration * @returns {string} */ var formatDurationHMS = exports.formatDurationHMS = function formatDurationHMS(duration) { var time = parseDuration(duration); return formatTimeHMS(time); }; /** * * @param duration * @param useMilli * @returns {string} */ var formatDuration = exports.formatDuration = function formatDuration(duration) { var useMilli = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var time = parseDuration(duration); return formatTime(time, useMilli); }; /** * * @param time: number - is time * @param unit: string = (milliseconds|minutes|months| ... unit moment) * @param useFixed: boolean = (true|false) * @returns {{mins: string, hours: string, weeks: string, months: string, secs: string, days: string, years: string, info: {unit: string, time: number}}} */ var getDurationTime = exports.getDurationTime = function getDurationTime() { var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'milliseconds'; var useFixed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var secs = _moment2.default.duration(time, unit).asSeconds(); var mins = _moment2.default.duration(time, unit).asMinutes(); var hours = _moment2.default.duration(time, unit).asHours(); var days = _moment2.default.duration(time, unit).asDays(); var weeks = _moment2.default.duration(time, unit).asWeeks(); var months = _moment2.default.duration(time, unit).asMonths(); var years = _moment2.default.duration(time, unit).asYears(); if (useFixed) { return { info: { time: time, unit: unit }, secs: secs.toFixed(1), mins: mins.toFixed(1), hours: hours.toFixed(1), days: days.toFixed(1), weeks: weeks.toFixed(1), months: months.toFixed(1), years: years.toFixed(1) }; } return { info: { time: time, unit: unit }, secs: secs, mins: mins, hours: hours, days: days, weeks: weeks, months: months, years: years }; }; /** * * @param time * @param unit * @param useFixed * @returns {string} */ var getDurationDescription = exports.getDurationDescription = function getDurationDescription() { var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'milliseconds'; var useFixed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var duration = getDurationTime(time, unit, useFixed); var res = ''; if (duration.years > 0) { res = res.trim() + ' ' + duration.years + ' year'; } if (duration.months > 0) { res = res.trim() + ' ' + duration.months + ' months'; } if (duration.weeks > 0) { res = res.trim() + ' ' + duration.weeks + ' weeks'; } if (duration.days > 0) { res = res.trim() + ' ' + duration.days + ' days'; } if (duration.hours > 0) { res = res.trim() + ' ' + duration.hours + ' hours'; } if (duration.mins > 0) { res = res.trim() + ' ' + duration.mins + ' minutes'; } if (duration.secs > 0) { res = res.trim() + ' ' + duration.secs + ' seconds'; } return res; }; /** * * @param time: number - is time * @param unit: string = (milliseconds|minutes|months| ... unit moment) * @param useFixed: boolean = (true|false) * @returns {{unit: string, time: string}} */ var getDurationSummary = exports.getDurationSummary = function getDurationSummary() { var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'milliseconds'; var useFixed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var duration = getDurationTime(time, unit, useFixed); if (duration.secs < 60) { return { timeOrigin: time, time: duration.secs, unit: 'Sec' }; } if (duration.mins < 60) { return { timeOrigin: time, time: duration.mins, unit: 'Min' }; } if (duration.hours < 24) { return { timeOrigin: time, time: duration.hours, unit: 'Hrs' }; } if (duration.days < 31) { return { timeOrigin: time, time: duration.days, unit: 'Days' }; } if (duration.weeks < 4) { return { timeOrigin: time, time: duration.weeks, unit: 'Weeks' }; } if (duration.months < 12) { return { timeOrigin: time, time: duration.months, unit: 'Months' }; } return { timeOrigin: time, time: duration.years, unit: 'Years' }; }; exports.default = { timeConversion: timeConversion, formatDuration: formatDuration, getDurationTime: getDurationTime, getDurationSummary: getDurationSummary, getDurationDescription: getDurationDescription, formatDurationHMS: formatDurationHMS };