Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #68 from sulmanen/master

Add bower.json for better frontend package management support
  • Loading branch information...
commit 1fb7ba0e00ed76fd6642dc2ffdc3b080f52b38ef 2 parents d6de4ae + b222790
Nick Campbell authored
Showing with 128 additions and 73 deletions.
  1. +29 −0 bower.json
  2. +99 −73 lib/cron.js
29 bower.json
View
@@ -0,0 +1,29 @@
+{
+ "name": "cronjs",
+ "version": "1.0.1",
+ "homepage": "https://github.com/ncb000gt/node-cron",
+ "authors": [
+ "Romain Beauxis<toots@rastageeks.org>",
+ "James Padoulsey<@padolsey>",
+ "Craig Condon<craig@browsertap.com>",
+ "Finn Herpich",
+ "cliftonc",
+ "neyric",
+ "humanchimp",
+ "danhbear"
+ ],
+ "description": "js cron handler",
+ "main": ["lib/cron.js"],
+ "keywords": [
+ "cron",
+ "js"
+ ],
+ "license": "MIT",
+ "ignore": [
+ "**/.*",
+ "node_modules",
+ "bower_components",
+ "test",
+ "tests"
+ ]
+}
172 lib/cron.js
View
@@ -1,21 +1,24 @@
-var CronDate = Date;
+var CronDate;
+var exports;
+
try {
CronDate = require("time").Date;
-} catch(e) {
- //no time module...leave CronDate alone. :)
+ exports = module.exports;
+} catch (e) {
+ CronDate = Date;
+ exports = null;
Y.
Y-- added a note

Isn't it a mistake to set exports to null ?
Because nothing will be exported if there is no time module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
-
function CronTime(source, zone) {
this.source = source;
- this.zone = zone;
+ this.zone = zone;
- this.second = {};
- this.minute = {};
- this.hour = {};
- this.dayOfWeek = {};
+ this.second = {};
+ this.minute = {};
+ this.hour = {};
+ this.dayOfWeek = {};
this.dayOfMonth = {};
- this.month = {};
+ this.month = {};
if ((this.source instanceof Date) || (this.source instanceof CronDate)) {
this.source = new CronDate(this.source);
@@ -26,11 +29,35 @@ function CronTime(source, zone) {
}
CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
-CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [0, 6] ];
-CronTime.parseDefaults = [ '0', '*', '*', '*', '*', '*' ];
+CronTime.constraints = [
+ [0, 59],
+ [0, 59],
+ [0, 23],
+ [1, 31],
+ [0, 11],
+ [0, 6]
+];
+CronTime.parseDefaults = ['0', '*', '*', '*', '*', '*'];
CronTime.aliases = {
- jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11,
- sun:0, mon:1, tue:2, wed:3, thu:4, fri:5, sat:6
+ jan: 0,
+ feb: 1,
+ mar: 2,
+ apr: 3,
+ may: 4,
+ jun: 5,
+ jul: 6,
+ aug: 7,
+ sep: 8,
+ oct: 9,
+ nov: 10,
+ dec: 11,
+ sun: 0,
+ mon: 1,
+ tue: 2,
+ wed: 3,
+ thu: 4,
+ fri: 5,
+ sat: 6
};
@@ -85,21 +112,21 @@ CronTime.prototype = {
*/
_getNextDateFrom: function(start) {
var date = new CronDate(start);
- //console.log("d: " + date);
+ //console.log("d: " + date);
if (this.zone && date.setTimezone)
date.setTimezone(start.getTimezone());
- if (this.realDate && start < new Date())
- console.log("WARNING: Date in past. Will never be fired.");
- if (this.realDate) return date;
+ if (this.realDate && start < new Date())
+ console.log("WARNING: Date in past. Will never be fired.");
+ if (this.realDate) return date;
//sanity check
//var i = 1000;
- while(1) {
+ while (1) {
var diff = date - start;
if (!(date.getMonth() in this.month)) {
- date.setMonth(date.getMonth()+1);
+ date.setMonth(date.getMonth() + 1);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
@@ -107,33 +134,33 @@ CronTime.prototype = {
}
if (!(date.getDate() in this.dayOfMonth)) {
- date.setDate(date.getDate()+1);
+ date.setDate(date.getDate() + 1);
date.setHours(0);
date.setMinutes(0);
continue;
}
if (!(date.getDay() in this.dayOfWeek)) {
- date.setDate(date.getDate()+1);
+ date.setDate(date.getDate() + 1);
date.setHours(0);
date.setMinutes(0);
continue;
}
if (!(date.getHours() in this.hour)) {
- date.setHours(date.getHours() == 23 && diff > 24*60*60*1000 ? 0 : date.getHours()+1);
+ date.setHours(date.getHours() == 23 && diff > 24 * 60 * 60 * 1000 ? 0 : date.getHours() + 1);
date.setMinutes(0);
continue;
}
if (!(date.getMinutes() in this.minute)) {
- date.setMinutes(date.getMinutes() == 59 && diff > 60*60*1000 ? 0 : date.getMinutes()+1);
+ date.setMinutes(date.getMinutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.getMinutes() + 1);
date.setSeconds(0);
continue;
}
if (!(date.getSeconds() in this.second)) {
- date.setSeconds(date.getSeconds() == 59 && diff > 60*1000 ? 0 : date.getSeconds()+1);
+ date.setSeconds(date.getSeconds() == 59 && diff > 60 * 1000 ? 0 : date.getSeconds() + 1);
continue;
}
@@ -147,10 +174,10 @@ CronTime.prototype = {
* wildcard, or all params in array (for to string)
*/
_wcOrAll: function(type) {
- if(this._hasAll(type)) return '*';
+ if (this._hasAll(type)) return '*';
var all = [];
- for(var time in this[type]) {
+ for (var time in this[type]) {
all.push(time);
}
@@ -162,8 +189,8 @@ CronTime.prototype = {
_hasAll: function(type) {
var constrain = CronTime.constraints[CronTime.map.indexOf(type)];
- for(var i = constrain[0], n = constrain[1]; i < n; i++) {
- if(!(i in this[type])) return false;
+ for (var i = constrain[0], n = constrain[1]; i < n; i++) {
+ if (!(i in this[type])) return false;
}
return true;
@@ -175,17 +202,18 @@ CronTime.prototype = {
*/
_parse: function() {
var aliases = CronTime.aliases,
- source = this.source.replace(/[a-z]{1,3}/ig, function(alias){
- alias = alias.toLowerCase();
+ source = this.source.replace(/[a-z]{1,3}/ig, function(alias) {
+ alias = alias.toLowerCase();
- if (alias in aliases) {
- return aliases[alias];
- }
+ if (alias in aliases) {
+ return aliases[alias];
+ }
- throw new Error('Unknown alias: ' + alias);
- }),
- split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
- cur, i = 0, len = CronTime.map.length;
+ throw new Error('Unknown alias: ' + alias);
+ }),
+ split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
+ cur, i = 0,
+ len = CronTime.map.length;
for (; i < CronTime.map.length; i++) {
// If the split source string doesn't contain all digits,
@@ -199,22 +227,22 @@ CronTime.prototype = {
/**
* Parse a field from the cron syntax.
*/
- _parseField: function(field, type, constraints){
+ _parseField: function(field, type, constraints) {
//var rangePattern = /^(\*)(?:\/(\d+))?$|(\d+)(?:-(\d+))?(?:\/(\d+))?(?:,|$)/g
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,
- typeObj = this[type],
- diff, pointer,
- low = constraints[0],
- high = constraints[1];
+ typeObj = this[type],
+ diff, pointer,
+ low = constraints[0],
+ high = constraints[1];
// * is a shortcut to [lower-upper] range
- field = field.replace(/\*/g, low + '-' + high);
+ field = field.replace(/\*/g, low + '-' + high);
//commas separate information, so split based on those
var allRanges = field.split(',');
- for(var i = 0; i < allRanges.length; i++){
+ for (var i = 0; i < allRanges.length; i++) {
if (allRanges[i].match(rangePattern)) {
allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
step = parseInt(step) || 1;
@@ -227,15 +255,15 @@ CronTime.prototype = {
// Count from the lower barrier to the upper
pointer = lower;
- do {
- typeObj[pointer] = true
- pointer += step;
- }while(pointer <= upper);
+ do {
+ typeObj[pointer] = true
+ pointer += step;
+ } while (pointer <= upper);
- });
- } else {
+ });
+ } else {
throw new Error('Field (' + field + ') cannot be parsed');
- }
+ }
}
}
};
@@ -258,7 +286,7 @@ function CronJob(cronTime, onTick, onComplete, start, timeZone, context) {
this.context = (context || this);
this._callbacks = [];
this.onComplete = onComplete;
- this.cronTime = new CronTime(cronTime, timeZone);
+ this.cronTime = new CronTime(cronTime, timeZone);
this.addCallback(onTick);
@@ -273,7 +301,7 @@ CronJob.prototype = {
*/
addCallback: function(callback) {
//only functions
- if(typeof callback == 'function') this._callbacks.push(callback);
+ if (typeof callback == 'function') this._callbacks.push(callback);
},
/**
@@ -301,7 +329,7 @@ CronJob.prototype = {
* Start the cronjob.
*/
start: function() {
- if(this.running) return;
+ if (this.running) return;
var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
var self = this;
@@ -362,32 +390,30 @@ CronJob.prototype = {
/**
* Stop the cronjob.
*/
- stop: function()
- {
+ stop: function() {
clearTimeout(this._timeout);
this.running = false;
if (this.onComplete) this.onComplete();
}
};
+if (exports) {
+ exports.job = function(cronTime, onTick, onComplete) {
+ return new CronJob(cronTime, onTick, onComplete);
+ }
-exports.job = function(cronTime, onTick, onComplete) {
- return new CronJob(cronTime, onTick, onComplete);
-}
+ exports.time = function(cronTime, timeZone) {
+ return new CronTime(cronTime, timeZone);
+ }
-exports.time = function(cronTime, timeZone) {
- return new CronTime(cronTime, timeZone);
-}
+ exports.sendAt = function(cronTime) {
+ return exports.time(cronTime).sendAt();
+ }
-exports.sendAt = function(cronTime) {
- return exports.time(cronTime).sendAt();
-}
+ exports.timeout = function(cronTime) {
+ return exports.time(cronTime).getTimeout();
+ }
-exports.timeout = function(cronTime) {
- return exports.time(cronTime).getTimeout();
+ exports.CronJob = CronJob;
+ exports.CronTime = CronTime;
}
-
-
-exports.CronJob = CronJob;
-exports.CronTime = CronTime;
-
Y.

Isn't it a mistake to set exports to null ?
Because nothing will be exported if there is no time module.

Please sign in to comment.
Something went wrong with that request. Please try again.