diff --git a/src/helpers/prevent-utils.js b/src/helpers/prevent-utils.js index bfd39d4c..e9327080 100644 --- a/src/helpers/prevent-utils.js +++ b/src/helpers/prevent-utils.js @@ -27,7 +27,7 @@ export const isValidCallback = (callback) => { * @returns {any} number as parsed delay or any input type if `delay` is not parsable */ export const parseRawDelay = (delay) => { - const parsedDelay = Math.floor(Number(delay)); + const parsedDelay = Math.floor(parseInt(delay, 10)); return typeof parsedDelay === 'number' && !nativeIsNaN(parsedDelay) ? parsedDelay : delay; }; diff --git a/tests/scriptlets/prevent-setInterval.test.js b/tests/scriptlets/prevent-setInterval.test.js index 39a9f472..47b75559 100644 --- a/tests/scriptlets/prevent-setInterval.test.js +++ b/tests/scriptlets/prevent-setInterval.test.js @@ -451,3 +451,35 @@ test('match any callback + non-number, decimal and string delays', (assert) => { const intervalTest5 = setInterval(fifth, '10'); testIntervals.push(intervalTest5); }); + +test('match any callback, falsy non-numbers delays dont collide with 0 ', (assert) => { + const done = assert.async(); + window.one = 'one'; + window.two = 'two'; + window.three = 'three'; + // We need to run our assertion after all timeouts + setTimeout(() => { + assert.equal(window.one, 'one', 'property \'one\' should NOT be changed'); + assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed'); + assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed'); + + assert.strictEqual(window.hit, 'FIRED', 'hit fired'); + done(); + }, 100); + + // run scriptlet code + const scriptletArgs = ['', '0']; + runScriptlet(name, scriptletArgs); + + const first = () => { window.one = 'NEW ONE'; }; + const intervalTest1 = setInterval(first, 0); + testIntervals.push(intervalTest1); + + const second = () => { window.two = 'NEW TWO'; }; + const intervalTest2 = setInterval(second, null); + testIntervals.push(intervalTest2); + + const third = () => { window.three = 'NEW THREE'; }; + const intervalTest3 = setInterval(third, undefined); + testIntervals.push(intervalTest3); +}); diff --git a/tests/scriptlets/prevent-setTimeout.test.js b/tests/scriptlets/prevent-setTimeout.test.js index 8f01fcba..0010cfa8 100644 --- a/tests/scriptlets/prevent-setTimeout.test.js +++ b/tests/scriptlets/prevent-setTimeout.test.js @@ -452,3 +452,35 @@ test('match any callback + non-number, decimal and string delays', (assert) => { const timeoutTest5 = setTimeout(fifth, '10'); testTimeouts.push(timeoutTest5); }); + +test('match any callback, falsy non-numbers delays dont collide with 0 ', (assert) => { + const done = assert.async(); + window.one = 'one'; + window.two = 'two'; + window.three = 'three'; + // We need to run our assertion after all timeouts + nativeSetTimeout(() => { + assert.equal(window.one, 'one', 'property \'one\' should NOT be changed'); + assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed'); + assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed'); + + assert.strictEqual(window.hit, 'FIRED', 'hit fired'); + done(); + }, 100); + + // run scriptlet code + const scriptletArgs = ['', '0']; + runScriptlet(name, scriptletArgs); + + const first = () => { window.one = 'NEW ONE'; }; + const timeoutTest1 = setTimeout(first, 0); + testTimeouts.push(timeoutTest1); + + const second = () => { window.two = 'NEW TWO'; }; + const timeoutTest2 = setTimeout(second, null); + testTimeouts.push(timeoutTest2); + + const third = () => { window.three = 'NEW THREE'; }; + const timeoutTest3 = setTimeout(third, undefined); + testTimeouts.push(timeoutTest3); +});