// get one second
const oneSecond = () => 1000;
// clear console
const clear = () => console.clear();
// print message
const log = message => console.log(message);
// get current system time
const getCurrentTime = () => new Date();
// serialize time
const serializeClockTime = clockTime => ({
year: clockTime.getFullYear(),
month: clockTime.getMonth() + 1,
day: clockTime.getDate(),
hours: clockTime.getHours(),
minutes: clockTime.getMinutes(),
seconds: clockTime.getSeconds()
});
// append AM/PM to time
const appendAMPM = clockTime => ({...clockTime,
ampm: clockTime.hours > 11 ? 'PM': 'AM'
});
// civilian hour
const civilianHour = clockTime => ({...clockTime,
hours: clockTime.hours > 12 ? clockTime.hours - 12 : clockTime.hours
});
// format time
const formatClockTime = (formatter = 'yyyy-MM-dd hh:mm:ss') => ({
year,
month,
day,
hours,
minutes,
seconds,
ampm
}) => formatter.replace('yyyy', year)
.replace('MM', month)
.replace('dd', day)
.replace('hh', hours)
.replace('mm', minutes)
.replace('ss', seconds)
.concat(' ').concat(ampm);
// prepend zero when number less then 10
const prependZero = clockTime => Object.keys(clockTime).reduce((previousClockTime, key) => ({...previousClockTime,
[key] : clockTime[key] < 10 ? '0' + clockTime[key] : clockTime[key]
}), {});
// pipe
const compose = (...fns) => arg => fns.reduce((composed, f) => f(composed), arg);
// display message
const display = target => time => target(time);
// ticking method
const startTicking = () => setInterval(compose(clear, getCurrentTime, serializeClockTime, appendAMPM, civilianHour, prependZero, formatClockTime(), display(log)), oneSecond());
// start ticking
startTicking();
console