Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

number.js 1019B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const assert = require('assert');
  3. /*!
  4. * Given a value, cast it to a number, or throw a `CastError` if the value
  5. * cannot be casted. `null` and `undefined` are considered valid.
  6. *
  7. * @param {Any} value
  8. * @param {String} [path] optional the path to set on the CastError
  9. * @return {Boolean|null|undefined}
  10. * @throws {Error} if `value` is not one of the allowed values
  11. * @api private
  12. */
  13. module.exports = function castNumber(val) {
  14. assert.ok(!isNaN(val));
  15. if (val == null) {
  16. return val;
  17. }
  18. if (val === '') {
  19. return null;
  20. }
  21. if (typeof val === 'string' || typeof val === 'boolean') {
  22. val = Number(val);
  23. }
  24. assert.ok(!isNaN(val));
  25. if (val instanceof Number) {
  26. return val;
  27. }
  28. if (typeof val === 'number') {
  29. return val;
  30. }
  31. if (!Array.isArray(val) && typeof val.valueOf === 'function') {
  32. return Number(val.valueOf());
  33. }
  34. if (val.toString && !Array.isArray(val) && val.toString() == Number(val)) {
  35. return new Number(val);
  36. }
  37. assert.ok(false);
  38. };