cron 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,163 +0,0 @@
1
- var _fields = ['seconds', 'minutes', 'hours', 'dayOfMonth', 'month', 'dayOfWeek'];
2
- var _daysInMonth = [30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
3
- var _constraints = [
4
- [0, 59],
5
- [0, 59],
6
- [0, 23],
7
- [1, 31],
8
- [1, 12],
9
- [0, 7]
10
- ];
11
- var _predef = {
12
- '@yearly': '0 0 1 1 *',
13
- '@monthly': '0 0 1 * *',
14
- '@weekly': '0 0 * * 0',
15
- '@daily': '0 0 * * *',
16
- '@hourly': '0 * * * *'
17
- };
18
- var _aliases = {
19
- dayOfWeek: {
20
- sun: 0,
21
- mon: 1,
22
- tue: 2,
23
- wed: 3,
24
- thu: 4,
25
- fri: 5,
26
- sat: 6
27
- },
28
- month: {
29
- jan: 1,
30
- feb: 2,
31
- mar: 3,
32
- apr: 4,
33
- may: 5,
34
- jun: 6,
35
- jul: 7,
36
- aug: 8,
37
- sep: 9,
38
- oct: 10,
39
- nov: 11,
40
- dec: 12
41
- }
42
- };
43
-
44
- var _validRegexes = [ validChars, validChars, validChars ];
45
-
46
- var validChars = /[\d-,*\/]+/g;
47
- //var validAdditionalChars = /[\dL\#\?-,*\/]+/g;
48
-
49
- var CronDate = function(v) {
50
- var date = (v) ? new Date(v) : new Date();
51
-
52
- //TODO: may add convenience methods here for adds
53
-
54
- return date;
55
- }
56
-
57
- var CronParser = function() {
58
- return {
59
- fields: {},
60
- next: function() {
61
- },
62
-
63
- parse: function(str) {
64
- function _isWildcard(value) {
65
- return /\*/.test(value);
66
- }
67
-
68
- function _getRange(start, stop, step) {
69
- if (!isNaN(start))
70
- start = parseInt(start, 10);
71
- if (!isNaN(stop))
72
- stop = parseInt(stop, 10);
73
- if (!isNaN(step))
74
- step = parseInt(step, 10);
75
-
76
- var values = [];
77
- for (var i = start; i <= stop; i += step) {
78
- values.push(i);
79
- }
80
-
81
- return values;
82
- }
83
-
84
- function _processValue(value, constraints) {
85
- var min = constraints[0], max = constraints[1];
86
- var isWildcard = _isWildcard(value);
87
-
88
- var val;
89
- if (isWildcard && value.length > 1) {
90
- //is wildcard but has a step value
91
- var splitSlash = value.split('/');
92
- val = _getRange(min, max, splitSlash[1]);
93
- } else if (isWildcard) {
94
- //is wildcard which means any
95
-
96
- val = _getRange(min, max, 1);
97
- } else if (value.indexOf('-') >= 0) {
98
- var splitRange = value.split('-');
99
- val = _getRange(splitRange[0], splitRange[1], 1);
100
- } else {
101
- //is some basic value or special character ie. L,#
102
- if (!isNaN(value)) {
103
- val = [parseInt(value, 10)];
104
- } else {
105
- val = [value];
106
- }
107
- }
108
-
109
- return val;
110
- }
111
-
112
- function _parseField(field, value, constraints) {
113
- //split by ,
114
- var splitComma = value.split(',');
115
- for (var i = 0; i < splitComma.length; i++) {
116
- var val = splitComma[i];
117
-
118
- //pulled from https://github.com/harrisiirak/cron-parser/blob/master/lib/expression.js#L131
119
- switch (field) {
120
- case 'month':
121
- case 'dayOfWeek':
122
- var aliases = _aliases[field];
123
-
124
- val = val.replace(/[a-z]{1,3}/gi, function(match) {
125
- match = match.toLowerCase();
126
-
127
- if (typeof aliases[match] !== undefined) {
128
- return aliases[match];
129
- } else {
130
- throw new Error('Cannot resolve alias "' + match + '"')
131
- }
132
- });
133
- break;
134
- }
135
-
136
- if (!this.fields[field])
137
- this.fields[field] = [];
138
- this.fields[field] = this.fields[field].concat(_processValue(val, constraints));
139
- }
140
- }
141
-
142
- var lowerStr = str.toLowerCase();
143
- if (lowerStr in _predef) {
144
- str = _predef[lowerStr];
145
- }
146
-
147
- var split = str.split(' ');
148
- var sL = split.length;
149
- if (sL === 1) {
150
- throw new Error('Couldn\'t parse input.');
151
- }
152
- if (sL === 5)
153
- split.unshift('0');
154
- for (var i = 0; i < split.length; i++) {
155
- _parseField.call(this, _fields[i], split[i], _constraints[i]);
156
- }
157
-
158
- return this;
159
- }
160
- };
161
- };
162
-
163
- module.exports = CronParser;
package/test.js DELETED
@@ -1,49 +0,0 @@
1
- var CronJob = require('./lib/cron').CronJob;
2
- var sinon = require('sinon');
3
-
4
- //var clock = sinon.useFakeTimers(new Date().getTime());
5
-
6
- //var previous = 0;
7
- //var counter = 0;
8
- //var cronSyncSchedule = '*/1 * * * * *';
9
-
10
- //var scheduleJob = new CronJob(cronSyncSchedule, function() {
11
- //counter++;
12
- //checkCounter();
13
- //}, function() {
14
- //console.log('scheduled job is completed successfully');
15
- //}, true, null);
16
-
17
- //function checkCounter() {
18
- //console.log(counter + ' - ' + previous);
19
- //if (counter != (previous + 1))
20
- //console.log('SKIPPED: ' + (counter - 1) + ", COUNTER: " + counter);
21
- //previous = counter;
22
- //}
23
-
24
- //clock.tick(31*24*60*60*1000);
25
-
26
- //new CronJob('0 0 0 1 * *',function(){
27
- //console.log("triggered");
28
- //},null,true);
29
-
30
- //clock.tick(24 * 60 * 60 * 1000);
31
-
32
-
33
- //clock.restore();
34
-
35
- //console.log(CronJob.prototype);
36
-
37
- //new CronJob('* * * * * *', function() {
38
- //console.log('tick: ' + Date.now());
39
- //}, null, true, null, null, true);
40
-
41
- new CronJob({
42
- cronTime: '0 0 17 1 * *',
43
- onTick: function() {
44
- console.log('tick: ' + Date.now());
45
- },
46
- start: true
47
- });
48
-
49
- console.log('do stuff');