D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
dslanger
Full window
Github gist
JS Bin // source https://jsbin.com/mecumom
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> 'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var compose = function compose() { for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) { fns[_key] = arguments[_key]; } return function (arg) { return fns.reduce(function (composed, f) { return f(composed); }, arg); }; }; var oneSecond = function oneSecond() { return 1000; }; var getCurrentTime = function getCurrentTime() { return new Date(); }; var clear = function clear() { return console.clear(); }; var log = function log(message) { return console.log(message); }; var serializeClockTime = function serializeClockTime(date) { return { hours: date.getHours(), minutes: date.getMinutes(), seconds: date.getSeconds() }; }; var civilianHours = function civilianHours(clockTime) { return _extends({}, clockTime, { hours: clockTime.hours > 12 ? clockTime.hours - 12 : clockTime.hours }); }; var appendAMPM = function appendAMPM(clockTime) { return _extends({}, clockTime, { ampm: clockTime.hours >= 12 ? 'PM' : 'AM' }); }; var display = function display(target) { return function (time) { return target(time); }; }; var formatClock = function formatClock(format) { return function (time) { return format.replace('hh', time.hours).replace('mm', time.minutes).replace('ss', time.seconds).replace('tt', time.ampm); }; }; var prependZero = function prependZero(key) { return function (clockTime) { return _extends({}, clockTime, _defineProperty({}, key, clockTime[key] < 10 ? '0' + clockTime[key] : clockTime[key])); }; }; var convertToCivilianTime = function convertToCivilianTime(clockTime) { return compose(appendAMPM, civilianHours)(clockTime); }; var doubleDigits = function doubleDigits(civilianTime) { return compose(prependZero('hours'), prependZero('minutes'), prependZero('seconds'))(civilianTime); }; var startTicking = function startTicking() { return setInterval(compose(clear, getCurrentTime, serializeClockTime, convertToCivilianTime, doubleDigits, formatClock('hh:mm:ss tt'), display(log)), oneSecond()); }; startTicking(); log(civilianHours(serializeClockTime(getCurrentTime()))); </script> <script id="jsbin-source-javascript" type="text/javascript">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 => ({ ...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, civilianHours )(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(); log(civilianHours(serializeClockTime(getCurrentTime())))</script></body> </html>