Skip to content

Commit

Permalink
improve helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 19, 2022
1 parent 8d52c1b commit dd79e29
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/helpers/prevent-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
isValidMatchNumber,
isValidMatchStr,
} from './string-utils';
import { nativeIsNaN } from './number-utils';

/**
* Checks whether the passed arg is proper callback
Expand All @@ -27,7 +26,7 @@ export const isValidCallback = (callback) => {
* @returns {any} number as parsed delay or any input type if `delay` is not parsable
*/
export const parseRawDelay = (delay) => {
return nativeIsNaN(parseInt(delay, 10)) ? delay : Math.floor(parseInt(delay, 10));
return Math.floor(Number(delay)) || delay;
};

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/helpers/prevent-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { parseRawDelay } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);
test('Test parseRawDelay', (assert) => {
assert.strictEqual(parseRawDelay(0), 0, 'parsing number ok');
assert.strictEqual(parseRawDelay(10), 10, 'parsing number ok');
assert.strictEqual(parseRawDelay(10.123), 10, 'parsing number ok');

assert.strictEqual(parseRawDelay('0'), 0, 'parsing number in string ok');
assert.strictEqual(parseRawDelay('10'), 10, 'parsing number in string ok');
assert.strictEqual(parseRawDelay('10.123'), 10, 'parsing number in string ok');

assert.strictEqual(parseRawDelay('string'), 'string', 'parsing string ok');

assert.strictEqual(parseRawDelay(null), null, 'parsing other types ok');
assert.strictEqual(parseRawDelay(undefined), undefined, 'parsing other types ok');
assert.strictEqual(parseRawDelay(false), false, 'parsing other types ok');
// as NaN !== NaN
assert.strictEqual(parseRawDelay(NaN).toString(), 'NaN', 'parsing other types ok');
});
20 changes: 1 addition & 19 deletions tests/helpers/string-utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toRegExp, parseRawDelay } from '../../src/helpers';
import { toRegExp } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';
Expand Down Expand Up @@ -46,21 +46,3 @@ test('Test toRegExp for invalid inputs', (assert) => {
toRegExp(inputStr);
});
});

test('Test parseRawDelay', (assert) => {
assert.strictEqual(parseRawDelay(0), 0, 'parsing number ok');
assert.strictEqual(parseRawDelay(10), 10, 'parsing number ok');
assert.strictEqual(parseRawDelay(10.123), 10, 'parsing number ok');

assert.strictEqual(parseRawDelay('0'), 0, 'parsing number in string ok');
assert.strictEqual(parseRawDelay('10'), 10, 'parsing number in string ok');
assert.strictEqual(parseRawDelay('10.123'), 10, 'parsing number in string ok');

assert.strictEqual(parseRawDelay('string'), 'string', 'parsing string ok');

assert.strictEqual(parseRawDelay(null), null, 'parsing other types ok');
assert.strictEqual(parseRawDelay(undefined), undefined, 'parsing other types ok');
assert.strictEqual(parseRawDelay(false), false, 'parsing other types ok');
// as NaN !== NaN
assert.strictEqual(parseRawDelay(NaN).toString(), 'NaN', 'parsing other types ok');
});

0 comments on commit dd79e29

Please sign in to comment.