mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
modify: fix test and logic
This commit is contained in:
@@ -1,36 +1,18 @@
|
|||||||
/**
|
/**
|
||||||
* @fileoverview Formatting date string.
|
* @fileoverview Formatting date string.
|
||||||
*/
|
*/
|
||||||
|
import moment from 'moment';
|
||||||
/** @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'.
|
* @description Return date string. For example, 'Sep.9, 2016 12:00'.
|
||||||
* @param {Date}
|
* @param {mixed}
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
export function getLastUpdated(date) {
|
export function getLastUpdated(date) {
|
||||||
if (!(date instanceof Date)) {
|
const m = moment(date)
|
||||||
throw Error('Invalid argument. Only instance of Date Object');
|
if (!m.isValid()) {
|
||||||
|
throw Error('Invalid argument.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const year = date.getFullYear();
|
return m.format('MMM D, gggg H:mm')
|
||||||
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}`;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,42 +4,9 @@
|
|||||||
const test = require('ava')
|
const test = require('ava')
|
||||||
const { getLastUpdated } = require('browser/lib/date-formatter')
|
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 => {
|
test(t => {
|
||||||
t.throws(
|
t.throws(
|
||||||
() => getLastUpdated('invalid argument'),
|
() => getLastUpdated('invalid argument'),
|
||||||
'Invalid argument. Only instance of Date Object'
|
'Invalid argument.'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user