corporate-frontend-mithril
Version:
Corporate frontend MithrilJS modules
123 lines (107 loc) • 3.32 kB
JavaScript
const moment = require('moment');
const _ = require('lodash');
/**
*
* The main purpose for this Dates class is to make sure the corporate website has the standard date format across the whole site.
*
* TODO mocha Test
*/
let getDateUTCFormat = function (candidate) {
return moment.utc(candidate)._f;
};
exports.getDateUTCFormat = getDateUTCFormat;
/**
* Get current year
* return {number} - current year, e.g. 2017
*/
let getCurrentYear = function () {
return new Date().getFullYear();
};
exports.getCurrentYear = getCurrentYear;
/**
* Get in a day, e.g. 10am, 5pm
* @param candidate {Date} - date
* @returns {string} hour in a day, e.g. 10am, 5pm
*/
let getHour = function (candidate) {
if(_.includes(this.getDateUTCFormat(candidate), 'HH') ) {
return moment(candidate).format('ha');
} else {
return '';
}
};
exports.getHour = getHour;
/**
* Get in a day, e.g. 10:45am, 5:32pm
* Note: 9:00am will be 9am
* @param candidate {Date} - date
* @returns {string} minute in a day, e.g. 10am, 5pm
*/
let getHourMinute = function (candidate) {
if(_.includes(this.getDateUTCFormat(candidate), 'HH:mm')) {
return moment(candidate).format('h.mma').replace('.00', '');
} else {
return '';
}
};
exports.getHourMinute = getHourMinute;
/**
* Get corporate website standard date, e.g. 14 November 2016, time format will be YYYY-MM-DDTHH:mm:ss
* @param candidate {Date} - date
* @returns {string} - date string e.g. 12 November 2016
*/
let getDate = function (candidate) {
return `${moment(candidate).format('D')} ${moment(candidate).format('MMMM')} ${moment(candidate).format('YYYY')}` ;
};
exports.getDate = getDate;
let getDateTime = function (candidate) {
let date = getDate(candidate);
let hours = getHourMinute(candidate);
return `${hours}, ${date}`;
};
exports.getDateTime = getDateTime;
/**
* Get hour and date
* @param candidate {Date} - date
* @returns {string} - date string e.g. 10am, 14 November 2016
*/
let getHourAndDate = function (candidate) {
if( _.isEmpty(this.getHour(candidate)) ){
return this.getDate(candidate);
}
return `${this.getHour(candidate)}, ${this.getDate(candidate)}`;
};
exports.getHourAndDate = getHourAndDate;
/**
* Get hour, minutes and date
* @param candidate {Date} - date
* @returns {string} - date string e.g. 10:45am, 14 November 2016
*/
let getHourMinuteAndDate = function (candidate) {
if( _.isEmpty(this.getHour(candidate)) ){
return this.getDate(candidate);
}
return `${this.getHourMinute(candidate)}, ${this.getDate(candidate)}`;
};
exports.getHourMinuteAndDate = getHourMinuteAndDate;
/**
* Convert seconds into mm:ss or hh:mm:ss
*/
let convertSeconds = function (data, format='mm:ss') {
let minutes = Math.floor(data / 60);
let seconds = data - minutes * 60;
let hours = Math.floor(data / 3600);
if(Object.is(format, 'mm:ss')) {
return `${pad(minutes,'0',2)} : ${pad(seconds,'0',2)}`;
}
return `${pad(hours,'0',2)} : ${pad(minutes,'0',2)} : ${pad(seconds,'0',2)}`;
};
exports.convertSeconds = convertSeconds;
/**
* Pad of number
* @param {number} num
*/
let pad = function (string,pad,length) {
return (new Array(length+1).join(pad)+string).slice(-length);
};
exports.pad = pad;