Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error in case of step value is NaN #227

Merged
merged 5 commits into from Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});

});