index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict'
  2. var isNumber = require('lodash.isfinite')
  3. /**
  4. * @id isNumberLike
  5. * @function isNumberLike
  6. * Checks whether provided parameter looks like a number
  7. * @param {any} val - the value to check
  8. * @returns {boolean} looksLikeNumber - `true` if `val` looks like a number, `false` otherwise
  9. */
  10. module.exports = function isNumberLike (val) {
  11. // fail
  12. if (val === null || val === void 0 || val === false) {
  13. return false
  14. }
  15. if (typeof val === 'function' || val instanceof Array) {
  16. return false
  17. }
  18. var length = val.length
  19. if (!length) {
  20. return isNumber(val)
  21. }
  22. var i = 0
  23. // charAt is faster in v8
  24. if (val.charAt(0) === '-') {
  25. if (length === 1) {
  26. // fail do it
  27. return false
  28. }
  29. i = 1
  30. }
  31. var foundE = false
  32. var foundPeriod = false
  33. for (; i < length; i++) {
  34. var c = val.charAt(i)
  35. // bit range is outside number range
  36. if ((c <= '/' || c >= ':')) {
  37. if (c === 'e') {
  38. if (foundE) {
  39. return false
  40. }
  41. foundE = true
  42. } else if (c === '.') {
  43. if (foundPeriod) {
  44. return false
  45. }
  46. foundPeriod = true
  47. } else {
  48. return false
  49. }
  50. }
  51. }
  52. return true
  53. }