Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cron-converter

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cron-converter - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

src/util.js

4

bower.json

@@ -24,5 +24,5 @@ {

"dependencies": {
"lodash": "~3",
"moment": "~2"
"moment": "~2",
"sprintf": "~1"
}
}
{
"name": "cron-converter",
"version": "0.0.9",
"version": "0.0.10",
"description": "Cron string converter",

@@ -20,3 +20,3 @@ "main": "src/cron.js",

"devDependencies": {
"browserify": "^12.0.1",
"browserify": "~13",
"browserify-shim": "^3.8.11",

@@ -39,4 +39,4 @@ "gulp": "^3.9.0",

"dependencies": {
"lodash": "~3",
"moment": "~2"
"moment": "~2",
"sprintf-js": "~1"
},

@@ -49,5 +49,5 @@ "browserify": {

"browserify-shim": {
"lodash": "global:_",
"moment": "global:moment"
"moment": "global:moment",
"sprintf-js": "global:sprintf"
}
}
'use strict';
var _ = require('lodash');
var util = require('util');
var sprintf = require('sprintf-js');
if (typeof sprintf.sprintf === 'function') { // For node.js
sprintf = sprintf.sprintf;
}
var util = require('./util');

@@ -25,2 +28,22 @@ /**

/**
* Throws an exception.
* Appends the unit name to the message.
*
* @this {Part}
* @param {string} format A format string to use for the message.
* @param {array} param The parameter to interpolate into the format string.
*/
Part.prototype.throw = function(format, param) {
throw new Error(
sprintf(
'%(error)s for %(unitName)s',
{
error: sprintf(format, param),
unitName: this.unit.name
}
)
);
};
/**
* Validates a range of positive integers.

@@ -32,13 +55,14 @@ *

Part.prototype.fromArray = function(arr) {
var values = _.sortBy(
_.union(
var values = util.sort(
util.dedup(
this.fixSunday(
arr.map(
function(value) {
value = parseInt(value, 10);
if (isNaN(value)) {
throw new Error('Invalid value');
var parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
this.throw('Invalid value "%s"', value);
}
return value;
}
return parsedValue;
},
this
)

@@ -49,6 +73,7 @@ )

if (!values.length) {
throw new Error('Empty interval value');
this.throw('Empty interval value');
}
if (!this.inRange(values)) {
throw new Error('Value out of range');
var value = this.outOfRange(values);
if (typeof value !== 'undefined') {
this.throw('Value "%s" out of range', value);
}

@@ -68,3 +93,3 @@ this.values = values;

if (stringParts.length > 2) {
throw new Error('Interval syntax error');
this.throw('Invalid value "%s"', str);
}

@@ -74,11 +99,13 @@ var rangeString = this.replaceAlternatives(stringParts[0]);

if (rangeString === '*') {
parsedValues = _.range(unit.min, unit.max + 1);
parsedValues = util.range(unit.min, unit.max);
} else {
parsedValues = _.sortBy(
_.union(
parsedValues = util.sort(
util.dedup(
this.fixSunday(
_.flatten(
_.map(
rangeString.split(','),
this.parseRange
util.flatten(
rangeString.split(',').map(
function(range) {
return this.parseRange(range, str);
},
this
)

@@ -89,8 +116,13 @@ )

);
if (!this.inRange(parsedValues)) {
throw new Error('Value out of range');
var value = this.outOfRange(parsedValues);
if (typeof value !== 'undefined') {
this.throw('Value "%s" out of range', value);
}
}
var step = this.parseStep(stringParts[1]);
this.values = this.applyInterval(parsedValues, step);
var intervalValues = this.applyInterval(parsedValues, step);
if (!intervalValues.length) {
this.throw('Empty interval value "%s"', str);
}
this.values = intervalValues;
};

@@ -123,5 +155,6 @@

* @param {string} range The range string.
* @param {string} context The operation context string.
* @return {array} The resulting array.
*/
Part.prototype.parseRange = function(range) {
Part.prototype.parseRange = function(range, context) {
var subparts = range.split('-');

@@ -131,3 +164,3 @@ if (subparts.length === 1) {

if (isNaN(value)) {
throw new Error('Invalid value');
this.throw('Invalid value "%s"', context);
}

@@ -139,9 +172,13 @@ return [value];

if (maxValue <= minValue) {
throw new Error(
'Part syntax error: max range is less than min range'
this.throw(
'Max range is less than min range in "%s"',
range
);
}
return _.range(minValue, maxValue + 1);
return util.range(minValue, maxValue);
} else {
throw new Error('Part syntax error');
this.throw(
'Invalid value "%s"',
range
);
}

@@ -159,8 +196,8 @@ };

if (typeof step !== 'undefined') {
step = parseInt(step, 10);
if (isNaN(step) || step < 1) {
throw new Error('Invalid interval step value');
var parsedStep = parseInt(step, 10);
if (isNaN(parsedStep) || parsedStep < 1) {
this.throw('Invalid interval step value "%s"', step);
}
return parsedStep;
}
return step;
};

@@ -182,5 +219,2 @@

}
if (!values.length) {
throw new Error('Empty interval value');
}
return values;

@@ -208,16 +242,17 @@ };

/**
* Checks if a sorted array is in the range of this.unit
* Finds an element from values that is outside of the range of this.unit
*
* @this {Part}
* @param {array} values The values to test.
* @return {boolean} Whether the provided values were
* within the range specified by this.unit
* @return {mixed} An integer is a value out of range was found,
* otherwise undefined.
*/
Part.prototype.inRange = function(values) {
Part.prototype.outOfRange = function(values) {
var first = values[0];
var last = values[values.length - 1];
if (first < this.unit.min || last > this.unit.max) {
return false;
if (first < this.unit.min) {
return first;
} else if (last > this.unit.max) {
return last;
}
return true;
};

@@ -337,3 +372,3 @@

var startPart = null;
_.forEach(this.values, function(value, index, self) {
this.values.forEach(function(value, index, self) {
if (value !== self[index + 1] - 1) {

@@ -377,3 +412,3 @@ if (startPart !== null) {

}
retval = util.format(format, step);
retval = sprintf(format, step);
} else {

@@ -385,3 +420,3 @@ if (this.options.outputHashes) {

}
retval = util.format(
retval = sprintf(
format,

@@ -396,3 +431,3 @@ this.formatValue(this.min()),

if (range.length) {
return util.format(
return sprintf(
'%s-%s',

@@ -399,0 +434,0 @@ this.formatValue(range[0]),

@@ -13,11 +13,11 @@ 'use strict';

array: [[], [], [], [], []],
error: 'Empty interval value'
error: 'Empty interval value for minute'
},
{
array: [['a'], [1], [1], [1], [1]],
error: 'Invalid value'
error: 'Invalid value "a" for minute'
},
{
array: [[0], [0], [0], [0], [0]],
error: 'Value out of range'
error: 'Value "0" out of range for day'
}

@@ -24,0 +24,0 @@ ];

@@ -29,51 +29,51 @@ 'use strict';

string: '0 0 0 0 0',
error: 'Value out of range'
error: 'Value "0" out of range for day'
},
{
string: '0 0 0 1 0',
error: 'Value out of range'
error: 'Value "0" out of range for day'
},
{
string: '0 0 1 0 0',
error: 'Value out of range'
error: 'Value "0" out of range for month'
},
{
string: '/ / / / /',
error: 'Invalid value'
error: 'Invalid value "/" for minute'
},
{
string: '60 5 5 5 5',
error: 'Value out of range'
error: 'Value "60" out of range for minute'
},
{
string: '/5 5 5 5 5',
error: 'Invalid value'
error: 'Invalid value "/5" for minute'
},
{
string: '10-5/5 5 5 5 5',
error: 'Part syntax error: max range is less than min range'
error: 'Max range is less than min range in "10-5" for minute'
},
{
string: '* * 0 * *',
error: 'Value out of range'
error: 'Value "0" out of range for day'
},
{
string: '* * * 0 *',
error: 'Value out of range'
error: 'Value "0" out of range for month'
},
{
string: '0/5/5 * * 0 *',
error: 'Interval syntax error'
error: 'Invalid value "0/5/5" for minute'
},
{
string: '1-6/10 * * * *',
error: 'Empty interval value'
error: 'Empty interval value "1-6/10" for minute'
},
{
string: '5/a * * * *',
error: 'Invalid interval step value'
error: 'Invalid interval step value "a" for minute'
},
{
string: '5/ * * * *',
error: 'Invalid interval step value'
error: 'Invalid interval step value "" for minute'
}

@@ -80,0 +80,0 @@ ];

@@ -11,3 +11,4 @@ 'use strict';

max: 10,
error: 'Invalid value'
error: 'Invalid value "" for sample unit',
name: 'sample unit'
},

@@ -18,3 +19,4 @@ {

max: 2,
error: 'Part syntax error: max range is less than min range'
error: 'Max range is less than min range in "3-1" for sample unit',
name: 'sample unit'
},

@@ -25,3 +27,4 @@ {

max: 10,
error: 'Part syntax error'
error: 'Invalid value "1-2-3" for sample unit',
name: 'sample unit'
},

@@ -32,3 +35,4 @@ {

max: 20,
error: 'Value out of range'
error: 'Value "5" out of range for sample unit',
name: 'sample unit'
},

@@ -39,3 +43,4 @@ {

max: 10,
error: 'Invalid value'
error: 'Invalid value "**" for sample unit',
name: 'sample unit'
},

@@ -46,3 +51,4 @@ {

max: 10,
error: 'Empty interval value'
error: 'Empty interval value "0-" for sample unit',
name: 'sample unit'
}

@@ -49,0 +55,0 @@ ];

@@ -100,2 +100,9 @@ 'use strict';

max: 59
},
{
input: '5,5,6,6,7,7',
arr: [5,6,7],
output: '5-7',
min: 0,
max: 59
}

@@ -102,0 +109,0 @@ ];

@@ -99,1 +99,18 @@ 'use strict';

});
test('Should output execution time for valid schedule twice', function(t) {
t.plan(2);
var cron = new Cron();
cron.fromString('*/5 * * * *');
var schedule = cron.schedule('2013-02-08T09:32:15.000Z');
t.equal(
schedule.next().toJSON(),
'2013-02-08T09:35:00.000Z',
'Next first time'
);
t.equal(
schedule.next().toJSON(),
'2013-02-08T09:40:00.000Z',
'Next second time'
);
});

@@ -0,5 +1,7 @@

# Bugs
* Shim sprintf
# Features
* Speed up next and prev
* Detect invalid characters early
* Improve exception error messages
* Support hashes

@@ -16,5 +18,5 @@ * parse hashes

# Infrastructure
* Get rid of lodash?
* Better docs
* More test cases
* Use sauce
* L10N + I18N

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc