ToNumber.js 783 B

1234567891011121314151617181920
  1. 'use strict';
  2. var ToPrimitive = require('./ToPrimitive');
  3. // http://262.ecma-international.org/5.1/#sec-9.3
  4. module.exports = function ToNumber(value) {
  5. var prim = ToPrimitive(value, Number);
  6. if (typeof prim !== 'string') {
  7. return +prim; // eslint-disable-line no-implicit-coercion
  8. }
  9. // eslint-disable-next-line no-control-regex
  10. var trimmed = prim.replace(/^[ \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u0085]+|[ \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u0085]+$/g, '');
  11. if ((/^0[ob]|^[+-]0x/).test(trimmed)) {
  12. return NaN;
  13. }
  14. return +trimmed; // eslint-disable-line no-implicit-coercion
  15. };