Document the radix argument of _.parseInt and ensure it ignores leading whitespace. [closes #254]

Former-commit-id: b1d0f7688402b3f41cfb030c63dcd097ba3f4e68
This commit is contained in:
John-David Dalton
2013-04-26 10:58:49 -07:00
parent d2de489e3a
commit 183b2548cf
8 changed files with 281 additions and 227 deletions

View File

@@ -55,8 +55,20 @@
/** Used to match "interpolate" template delimiters */
var reInterpolate = /<%=([\s\S]+?)%>/g;
/** Used to match leading zeros to be removed */
var reLeadingZeros = /^0+(?=.$)/;
/** Used to detect and test whitespace */
var whitespace = (
// whitespace
' \t\x0B\x0C\xA0\ufeff' +
// line terminators
'\n\r\u2028\u2029' +
// unicode category "Zs" space separators
'\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
);
/** Used to match leading whitespace and zeros to be removed */
var reLeadingSpacesAndZeros = RegExp('^[' + whitespace + ']*0+(?=.$)');
/** Used to ensure capturing order of template delimiters */
var reNoMatch = /($^)/;
@@ -1277,7 +1289,6 @@
*/
var defaults = createIterator(defaultsIteratorOptions);
/**
* This method is similar to `_.find`, except that it returns the key of the
* element that passes the callback check, instead of the element itself.
@@ -4878,6 +4889,8 @@
/**
* Converts the given `value` into an integer of the specified `radix`.
* If `radix` is `undefined` or `0`, a `radix` of `10` is used unless the
* `value` is a hexadecimal, in which case a `radix` of `16` is used.
*
* Note: This method avoids differences in native ES3 and ES5 `parseInt`
* implementations. See http://es5.github.com/#E.
@@ -4885,16 +4898,17 @@
* @static
* @memberOf _
* @category Utilities
* @param {Mixed} value The value to parse.
* @param {String} value The value to parse.
* @param {Number} [radix] The radix used to interpret the value to parse.
* @returns {Number} Returns the new integer value.
* @example
*
* _.parseInt('08');
* // => 8
*/
var parseInt = nativeParseInt('08') == 8 ? nativeParseInt : function(value, radix) {
var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) {
// Firefox and Opera still follow the ES3 specified implementation of `parseInt`
return nativeParseInt(isString(value) ? value.replace(reLeadingZeros, '') : value, radix || 0);
return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
};
/**