a27d0057b486e8ec0f370af3af164834432b15955a04bc4c4e376c9fc95fa80acec7ce6d71f37a25ddeafcef19ff5f9efb382b54907c8d36579cbdf662816c-exec 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 'use strict';
  2. const Hoek = require('@hapi/hoek');
  3. const Any = require('../any');
  4. const Ref = require('../../ref');
  5. const internals = {
  6. precisionRx: /(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/,
  7. normalizeExponent(str) {
  8. return str
  9. .replace(/\.?0+e/, 'e')
  10. .replace(/e\+/, 'e')
  11. .replace(/^\+/, '')
  12. .replace(/^(-?)0+([1-9])/, '$1$2');
  13. },
  14. normalizeDecimal(str) {
  15. str = str
  16. .replace(/^\+/, '')
  17. .replace(/\.0+$/, '')
  18. .replace(/^(-?)0+([1-9])/, '$1$2');
  19. if (str.includes('.') && str.endsWith('0')) {
  20. str = str.replace(/0+$/, '');
  21. }
  22. return str;
  23. }
  24. };
  25. internals.Number = class extends Any {
  26. constructor() {
  27. super();
  28. this._type = 'number';
  29. this._flags.unsafe = false;
  30. this._invalids.add(Infinity);
  31. this._invalids.add(-Infinity);
  32. }
  33. _base(value, state, options) {
  34. const result = {
  35. errors: null,
  36. value
  37. };
  38. if (typeof value === 'string' &&
  39. options.convert) {
  40. const matches = value.match(/^\s*[+-]?\d+(?:\.\d+)?(?:e([+-]?\d+))?\s*$/i);
  41. if (matches) {
  42. value = value.trim();
  43. result.value = parseFloat(value);
  44. if (!this._flags.unsafe) {
  45. if (value.includes('e')) {
  46. if (internals.normalizeExponent(`${result.value / Math.pow(10, matches[1])}e${matches[1]}`) !== internals.normalizeExponent(value)) {
  47. result.errors = this.createError('number.unsafe', { value }, state, options);
  48. return result;
  49. }
  50. }
  51. else {
  52. if (result.value.toString() !== internals.normalizeDecimal(value)) {
  53. result.errors = this.createError('number.unsafe', { value }, state, options);
  54. return result;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. const isNumber = typeof result.value === 'number' && !isNaN(result.value);
  61. if (options.convert && 'precision' in this._flags && isNumber) {
  62. // This is conceptually equivalent to using toFixed but it should be much faster
  63. const precision = Math.pow(10, this._flags.precision);
  64. result.value = Math.round(result.value * precision) / precision;
  65. }
  66. if (isNumber) {
  67. if (!this._flags.unsafe &&
  68. (value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER)) {
  69. result.errors = this.createError('number.unsafe', { value }, state, options);
  70. }
  71. }
  72. else {
  73. result.errors = this.createError('number.base', { value }, state, options);
  74. }
  75. return result;
  76. }
  77. multiple(base) {
  78. const isRef = Ref.isRef(base);
  79. if (!isRef) {
  80. Hoek.assert(typeof base === 'number' && isFinite(base), 'multiple must be a number');
  81. Hoek.assert(base > 0, 'multiple must be greater than 0');
  82. }
  83. return this._test('multiple', base, function (value, state, options) {
  84. const divisor = isRef ? base(state.reference || state.parent, options) : base;
  85. if (isRef && (typeof divisor !== 'number' || !isFinite(divisor))) {
  86. return this.createError('number.ref', { ref: base.key }, state, options);
  87. }
  88. if (value % divisor === 0) {
  89. return value;
  90. }
  91. return this.createError('number.multiple', { multiple: base, value }, state, options);
  92. });
  93. }
  94. integer() {
  95. return this._test('integer', undefined, function (value, state, options) {
  96. return Math.trunc(value) - value === 0 ? value : this.createError('number.integer', { value }, state, options);
  97. });
  98. }
  99. unsafe(enabled = true) {
  100. Hoek.assert(typeof enabled === 'boolean', 'enabled must be a boolean');
  101. if (this._flags.unsafe === enabled) {
  102. return this;
  103. }
  104. const obj = this.clone();
  105. obj._flags.unsafe = enabled;
  106. return obj;
  107. }
  108. negative() {
  109. return this._test('negative', undefined, function (value, state, options) {
  110. if (value < 0) {
  111. return value;
  112. }
  113. return this.createError('number.negative', { value }, state, options);
  114. });
  115. }
  116. positive() {
  117. return this._test('positive', undefined, function (value, state, options) {
  118. if (value > 0) {
  119. return value;
  120. }
  121. return this.createError('number.positive', { value }, state, options);
  122. });
  123. }
  124. precision(limit) {
  125. Hoek.assert(Number.isSafeInteger(limit), 'limit must be an integer');
  126. Hoek.assert(!('precision' in this._flags), 'precision already set');
  127. const obj = this._test('precision', limit, function (value, state, options) {
  128. const places = value.toString().match(internals.precisionRx);
  129. const decimals = Math.max((places[1] ? places[1].length : 0) - (places[2] ? parseInt(places[2], 10) : 0), 0);
  130. if (decimals <= limit) {
  131. return value;
  132. }
  133. return this.createError('number.precision', { limit, value }, state, options);
  134. });
  135. obj._flags.precision = limit;
  136. return obj;
  137. }
  138. port() {
  139. return this._test('port', undefined, function (value, state, options) {
  140. if (!Number.isSafeInteger(value) || value < 0 || value > 65535) {
  141. return this.createError('number.port', { value }, state, options);
  142. }
  143. return value;
  144. });
  145. }
  146. };
  147. internals.compare = function (type, compare) {
  148. return function (limit) {
  149. const isRef = Ref.isRef(limit);
  150. const isNumber = typeof limit === 'number' && !isNaN(limit);
  151. Hoek.assert(isNumber || isRef, 'limit must be a number or reference');
  152. return this._test(type, limit, function (value, state, options) {
  153. let compareTo;
  154. if (isRef) {
  155. compareTo = limit(state.reference || state.parent, options);
  156. if (!(typeof compareTo === 'number' && !isNaN(compareTo))) {
  157. return this.createError('number.ref', { ref: limit.key }, state, options);
  158. }
  159. }
  160. else {
  161. compareTo = limit;
  162. }
  163. if (compare(value, compareTo)) {
  164. return value;
  165. }
  166. return this.createError('number.' + type, { limit: compareTo, value }, state, options);
  167. });
  168. };
  169. };
  170. internals.Number.prototype.min = internals.compare('min', (value, limit) => value >= limit);
  171. internals.Number.prototype.max = internals.compare('max', (value, limit) => value <= limit);
  172. internals.Number.prototype.greater = internals.compare('greater', (value, limit) => value > limit);
  173. internals.Number.prototype.less = internals.compare('less', (value, limit) => value < limit);
  174. module.exports = new internals.Number();