Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Last active October 3, 2017 06:11
Show Gist options
  • Select an option

  • Save sharmaabhinav/392f4016d27dde362e88c6ce1c5ca3db to your computer and use it in GitHub Desktop.

Select an option

Save sharmaabhinav/392f4016d27dde362e88c6ce1c5ca3db to your computer and use it in GitHub Desktop.
const compose = (...fns) => (arg) => fns.reduce((composed, f) => f(composed), arg)
const oneSecond = () => 1000
const getCurrentTime = () => new Date()
const clear = () => { console.clear() }
const log = (message) => { console.log(message) }
const serializeClockTime = date => ({
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
})
const civilianHours = clockTime => Object.assign({}, clockTime, { hours: clockTime.hours > 12 ? clockTime.hours - 12 : clockTime.hours })
const appendAMPM = clockTime => Object.assign({}, clockTime, {ampm: clockTime.hours >= 12? 'PM': 'AM'})
const display = target => time => { target(time) }
const formatClock = format => time => format.replace('hh',time.hours).replace('mm', time.minutes).replace('ss', time.seconds).replace('tt', time.ampm)
const convertToCivilianTime = clockTime => compose(
appendAMPM,
civilianHours
)(clockTime)
const startTicking = () => setInterval( compose( clear,
getCurrentTime,
serializeClockTime,
convertToCivilianTime,
formatClock('hh:mm:ss:tt'),
display(log)
),
oneSecond()
)
startTicking()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment