Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const oneSecond = () => 1000;
- const getCurrentTime = () => new Date();
- const clear = () => console.clear();
- const log = (message) => console.log(message);
- //
- const compose =
- (...fns) =>
- (args) =>
- fns.reduce((composed, f) => f(composed), args);
- // Take a date object and return an object for clock time that contains hours, minutes, and seconds.
- const serializeClockTime = (date) => ({
- hours: date.getHours(),
- minutes: date.getMinutes(),
- seconds: date.getSeconds(),
- });
- //
- const civilianHours = (clockTime) => ({
- ...clockTime,
- hours: clockTime.hours > 12 ? clockTime.hours - 12 : clockTime.hours,
- });
- const appendAMPM = (clockTime) => ({
- ...clockTime,
- ampm: clockTime.hours > 12 ? "PM" : "AM",
- });
- // Take a target function and returns a function that will send a time to the target.
- const display = (target) => (time) => target(time);
- // Take a template string and uses it to return clock time formatted based on the
- // criteria from the string.
- const formatClock = (format) => (time) =>
- format
- .replace("hh", time.hours)
- .replace("mm", time.minutes)
- .replace("ss", time.seconds)
- .replace("tt", time.ampm);
- // Take an object's key as an argument and prepends a zero to the value stored
- // under that object's key.
- const prependZero = (key) => (clockTime) => ({
- ...clockTime,
- key: clockTime[key] < 10 ? "0" + clockTime[key] : clockTime[key],
- });
- // Takes clock time as an argument and transform it into civilian time
- const convertToCivilianTime = (clockTime) =>
- compose(appendAMPM, civilianHours)(clockTime);
- // Takes civilian clock time and make sure the hours, minutes, and seconds
- // display double digits by prepending zeros where needed.
- const doubleDigits = (civilianTime) =>
- compose(
- prependZero("hours"),
- prependZero("minutes"),
- prependZero("seconds")
- )(civilianTime);
- // Starts the clock by setting an interval that invokes a callback every second
- const startTicking = () =>
- setInterval(
- compose(
- clear,
- getCurrentTime,
- serializeClockTime,
- convertToCivilianTime,
- doubleDigits,
- formatClock("hh:mm:ss tt"),
- display(log)
- ),
- oneSecond()
- );
- startTicking();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement