mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
modify: fix test and logic
This commit is contained in:
@@ -1,36 +1,18 @@
|
||||
/**
|
||||
* @fileoverview Formatting date string.
|
||||
*/
|
||||
|
||||
/** @var {Array} */
|
||||
const monthMapings = [
|
||||
'Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.',
|
||||
'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.',
|
||||
];
|
||||
import moment from 'moment';
|
||||
|
||||
/**
|
||||
* @description Return date string. For example, 'Sep.9, 2016 12:00'.
|
||||
* @param {Date}
|
||||
* @param {mixed}
|
||||
* @return {string}
|
||||
*/
|
||||
export function getLastUpdated(date) {
|
||||
if (!(date instanceof Date)) {
|
||||
throw Error('Invalid argument. Only instance of Date Object');
|
||||
const m = moment(date)
|
||||
if (!m.isValid()) {
|
||||
throw Error('Invalid argument.');
|
||||
}
|
||||
|
||||
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}`;
|
||||
return m.format('MMM D, gggg H:mm')
|
||||
}
|
||||
|
||||
@@ -4,42 +4,9 @@
|
||||
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'
|
||||
'Invalid argument.'
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user