Skip to content

Commit

Permalink
Merge pull request #227 from ZimGil/master
Browse files Browse the repository at this point in the history
Throw error in case of step value is NaN
  • Loading branch information
merencia committed Mar 10, 2021
2 parents 12d58ad + c60463e commit 977c6ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/convert-expression/step-values-conversion.js
Expand Up @@ -2,16 +2,20 @@

module.exports = (() => {
function convertSteps(expressions){
const stepValuePattern = /^(.+)\/(\d+)$/;
for(let i = 0; i < expressions.length; i++){
const match = stepValuePattern.exec(expressions[i]);
const isStepValue = match !== null && match.length > 0;
var stepValuePattern = /^(.+)\/(\w+)$/;
for(var i = 0; i < expressions.length; i++){
var match = stepValuePattern.exec(expressions[i]);
var isStepValue = match !== null && match.length > 0;
if(isStepValue){
const values = match[1].split(',');
const stepValues = [];
const divider = parseInt(match[2], 10);
for(let j = 0; j <= values.length; j++){
const value = parseInt(values[j], 10);
var baseDivider = match[2];
if(isNaN(baseDivider)){
throw baseDivider + ' is not a valid step value';
}
var values = match[1].split(',');
var stepValues = [];
var divider = parseInt(baseDivider, 10);
for(var j = 0; j <= values.length; j++){
var value = parseInt(values[j], 10);
if(value % divider === 0){
stepValues.push(value);
}
Expand Down
10 changes: 8 additions & 2 deletions test/convert-expression/step-values-conversion-test.js
Expand Up @@ -4,10 +4,16 @@ const { expect } = require('chai');
const conversion = require('../../src/convert-expression/step-values-conversion');

describe('step-values-conversion.js', () => {
it('shuld convert step values', () => {
let expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
it('should convert step values', () => {
var expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expressions = conversion(expressions);
expect(expressions[0]).to.equal('2,4,6,8,10');
expect(expressions[1]).to.equal('0,5');
});

it('should throw an error if step value is not a number', () => {
var expressions = '1,2,3,4,5,6,7,8,9,10/someString 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expect(() => conversion(expressions)).to.throw('someString is not a valid step value');
});

});

0 comments on commit 977c6ca

Please sign in to comment.