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

Added default value field to toNumber function #5852

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 31 additions & 10 deletions src/toNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const freeParseInt = parseInt;
* @since 4.0.0
* @category Lang
* @param {*} value The value to process.
* @param {Object} [options={}] The options object.
* @param {number} [options.defaultValue=0] If NaN occurs, the options.defaultValue argument will be returned.
* @returns {number} Returns the number.
* @see isInteger, toInteger, isNumber
* @example
Expand All @@ -40,28 +42,47 @@ const freeParseInt = parseInt;
*
* toNumber('3.2')
* // => 3.2
*
* toNumber("someStringValue")
* // => NaN
*
* toNumber("someStringValue", {})
* // => NaN
*
* toNumber("someStringValue", {defaultValue: 0})
* // => 0
*
* toNumber("23", {defaultValue: 0})
* // => 23
*/
function toNumber(value) {
function toNumber(value, options = { defaultValue: NAN }) {
const { defaultValue = NAN } = options;
let finalValue;
if (typeof value === 'number') {
return value;
}
if (isSymbol(value)) {
return NAN;
return defaultValue;
}
if (isObject(value)) {
const other = typeof value.valueOf === 'function' ? value.valueOf() : value;
value = isObject(other) ? `${other}` : other;
}
if (typeof value !== 'string') {
return value === 0 ? value : +value;
finalValue = value === 0 ? value : +value;
} else {
value = value.replace(reTrim, '');
const isBinary = reIsBinary.test(value);

finalValue =
isBinary || reIsOctal.test(value)
? freeParseInt(value.slice(2), isBinary ? 2 : 8)
: reIsBadHex.test(value)
? defaultValue
: +value;
}
value = value.replace(reTrim, '');
const isBinary = reIsBinary.test(value);
return isBinary || reIsOctal.test(value)
? freeParseInt(value.slice(2), isBinary ? 2 : 8)
: reIsBadHex.test(value)
? NAN
: +value;
if (Number.isNaN(finalValue)) return defaultValue;
return finalValue;
}

export default toNumber;
22 changes: 22 additions & 0 deletions test/toNumber.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import toNumber from '../src/toNumber';
describe('toNumber', () => {
it('should return the given number', () => {
expect(toNumber(34)).toBe(34);
});

it('should convert the given number', () => {
expect(toNumber('23')).toBe(23);
});

it('should return the NaN', () => {
expect(toNumber('this-shall-pass')).toBe(NaN);
});

it('should return the default value as NaN', () => {
expect(toNumber('this-shall-pass', {})).toBe(NaN);
});

it('should return the default value', () => {
expect(toNumber({}, { defaultValue: 47 })).toBe(47);
});
});