Skip to content

Instantly share code, notes, and snippets.

@egroj97
Last active July 23, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egroj97/59ea1cf99bfa5b3b199cc53cb21e41fa to your computer and use it in GitHub Desktop.
Save egroj97/59ea1cf99bfa5b3b199cc53cb21e41fa to your computer and use it in GitHub Desktop.
const oneSecond = () => 1000
const getCurrentTime = () => new Date()
const clear = () => console.clear()
const log = message => console.log(message)
const compose = (...fns) =>
(arg) =>
fns.reduce(
(composed, f) => f(composed),
arg
)
const serializeClockTime = date => ({
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
})
const civilianTime = clockTime => ({
...clockTime,
hours: (clockTime.hours > 12) ? clockTime.hours - 12 : clockTime.hours
})
const appendAMPM = clockTime => ({
...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 prependZero = key =>
clockTime => ({
...clockTime,
[key]: (clockTime[key] < 10) ? "0" + clockTime[key] : clockTime[key]
})
const convertToCiviliantime = clockTime =>
compose(
appendAMPM,
civilianTime
)(clockTime)
const doubleDigits = civilianTime =>
compose(
prependZero("hours"),
prependZero("minutes"),
prependZero("seconds")
)(civilianTime)
const startTicking = () => setInterval(
compose(
clear,
getCurrentTime,
serializeClockTime,
convertToCiviliantime,
doubleDigits,
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