Last active
August 29, 2015 14:14
-
-
Save jackjennings/3dac77eb113d51c7fed2 to your computer and use it in GitHub Desktop.
Bacon.Clock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Bacon = require('baconjs'); | |
| var pad = require('./pad'); | |
| module.exports = function(interval) { | |
| interval = interval || 500; | |
| var get = { | |
| date: function(event) { return new Date(); }, | |
| hour: function(date) { return date.getHours(); }, | |
| minute: function(date) { return date.getMinutes(); }, | |
| second: function(date) { return date.getSeconds(); }, | |
| day: function(date) { return date.getDate(); }, | |
| month: function(date) { return date.getMonth() + 1; }, | |
| year: function(date) { return date.getFullYear(); } | |
| } | |
| var format = { | |
| date: function(day, month, year, delimiter) { | |
| return month + delimiter + day + delimiter + year; // 9/22/2014 | |
| }, | |
| time: function(hours, minutes, seconds, delimiter) { | |
| return hours + delimiter + pad.zero(minutes) + delimiter + pad.zero(seconds); // 14:25:00 | |
| } | |
| }; | |
| var delimiter = { | |
| time: Bacon.interval(1000).scan(":", function(val) { return (val === ":" ? " " : ":"); }), | |
| date: Bacon.constant('/') | |
| }; | |
| var clock = Bacon.fromPoll(interval, get.date).toProperty(get.date()); | |
| var hours = clock.map(get.hour).toProperty().skipDuplicates(); | |
| var minutes = clock.map(get.minute).toProperty().skipDuplicates(); | |
| var seconds = clock.map(get.second).toProperty().skipDuplicates(); | |
| var day = clock.map(get.day).toProperty().skipDuplicates(); | |
| var month = clock.map(get.month).toProperty().skipDuplicates(); | |
| var year = clock.map(get.year).toProperty().skipDuplicates(); | |
| var date = Bacon.combineWith(format.date, day, month, year, delimiter.date); | |
| var time = Bacon.combineWith(format.time, hours, minutes, seconds, delimiter.time); | |
| return { | |
| clock: clock, | |
| hours: hours, | |
| minutes: minutes, | |
| seconds: seconds, | |
| day: day, | |
| month: month, | |
| year: year, | |
| date: date, | |
| time: time | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var clock = require('./clock')(500); | |
| $(function(){ | |
| clock.date.assign($('.clock .date'), 'text'); | |
| clock.time.assign($('.clock .time'), 'text'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pad = function(n, p) { | |
| return n < 10 ? p.toString() + n : n; | |
| }; | |
| pad.zero = function(n) { | |
| return pad(n, 0); | |
| }; | |
| module.export = pad; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment