mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
add: util method for generating date
This commit is contained in:
36
browser/lib/date-formatter.js
Normal file
36
browser/lib/date-formatter.js
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Formatting date string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @var {Array} */
|
||||||
|
const monthMapings = [
|
||||||
|
'Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.',
|
||||||
|
'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Return date string. For example, 'Sep.9, 2016 12:00'.
|
||||||
|
* @param {Date}
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
export function getLastUpdated(date) {
|
||||||
|
if (!(date instanceof Date)) {
|
||||||
|
throw Error('Invalid argument. Only instance of Date Object');
|
||||||
|
}
|
||||||
|
|
||||||
|
const year = date.getFullYear();
|
||||||
|
const month = monthMapings[date.getMonth()];
|
||||||
|
const day = date.getDate();
|
||||||
|
let hour = date.getHours();
|
||||||
|
let minute = date.getMinutes();
|
||||||
|
|
||||||
|
if (hour < 10) {
|
||||||
|
hour = `0${hour}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minute < 10) {
|
||||||
|
minute = `0${minute}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${month}${day}, ${year} ${hour}:${minute}`;
|
||||||
|
}
|
||||||
45
tests/date-formatter-test.js
Normal file
45
tests/date-formatter-test.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Unit test for browser/lib/date-formatter.js
|
||||||
|
*/
|
||||||
|
const test = require('ava')
|
||||||
|
const { getLastUpdated } = require('browser/lib/date-formatter')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Get local Date insance.
|
||||||
|
* @param {numbr} year
|
||||||
|
* @param {number} month
|
||||||
|
* @param {number} day
|
||||||
|
* @param {number} hour
|
||||||
|
* @param {number} minute
|
||||||
|
* @param {number} second
|
||||||
|
* @return {Date}
|
||||||
|
*/
|
||||||
|
function generateLocalDate(year, month, day, hour, minute, second) {
|
||||||
|
const date = new Date()
|
||||||
|
date.setDate(day)
|
||||||
|
date.setFullYear(year)
|
||||||
|
date.setHours(hour)
|
||||||
|
date.setMinutes(minute)
|
||||||
|
date.setMonth(month - 1)
|
||||||
|
date.setSeconds(second)
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
|
||||||
|
test(t => {
|
||||||
|
const testCases = [
|
||||||
|
[generateLocalDate(2016, 9, 9, 12, 0, 0), 'Sep.9, 2016 12:00'],
|
||||||
|
[generateLocalDate(2016, 9, 9, 2, 0, 0), 'Sep.9, 2016 02:00'],
|
||||||
|
[generateLocalDate(2016, 5, 9, 2, 1, 0), 'May9, 2016 02:01'],
|
||||||
|
]
|
||||||
|
|
||||||
|
for (let testCase of testCases) {
|
||||||
|
t.is(testCase[1], getLastUpdated(testCase[0]))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test(t => {
|
||||||
|
t.throws(
|
||||||
|
() => getLastUpdated('invalid argument'),
|
||||||
|
'Invalid argument. Only instance of Date Object'
|
||||||
|
)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user