underscore.isnumeric
Version:
Adds an isNumeric method to Underscore based on the jQuery.isNumeric implementation.
54 lines (50 loc) • 1.41 kB
JavaScript
(function (root, factory) {
// AMD. Register as an anonymous module.
if (typeof define === 'function' && define.amd) {
define(['exports', 'underscore'], factory);
}
// CommonJS
else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
var _;
// Atempt to load lodash.
try {
_ = require('lodash');
}
catch (err) {}
// Attempt to load underscore if lodash was unavailable.
if ('undefined' === typeof _) {
try {
_ = require('underscore');
}
catch (err) {}
}
// If neither lodash nor underscore was available throw an error.
if ('undefined' === typeof _) {
throw 'UnderscoreJS (or compatible alternative) not found.';
}
factory(exports, _);
}
// Browser globals
else {
factory((root.commonJsStrict = {}), root.underscore);
}
}(this, function (exports, _) {
_.mixin({
/**
* An implementation of jQuery's isNumeric in for UnderscoreJS.
*
* parseFloat NaNs numeric-cast false positives (null|true|false|"") but misinterprets
* leading-number strings, particularly hex literals ("0x...").
*
* Subtraction forces infinities to NaN.
*
* Adding 1 corrects loss of precision from parseFloat (#15100).
*
* @return void
*/
isNumeric: function (object) {
var stringObject = object && object.toString();
return !_.isArray(object) && (stringObject - parseFloat(stringObject) + 1) >= 0;
}
});
}));