number.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.range = range;
  4. exports.formatNumber = formatNumber;
  5. function range(num, min, max) {
  6. return Math.min(Math.max(num, min), max);
  7. }
  8. function trimExtraChar(value, _char, regExp) {
  9. var index = value.indexOf(_char);
  10. var prefix = '';
  11. if (index === -1) {
  12. return value;
  13. }
  14. if (_char === '-' && index !== 0) {
  15. return value.slice(0, index);
  16. }
  17. if (_char === '.' && value.match(/^(\.|-\.)/)) {
  18. prefix = index ? '-0' : '0';
  19. }
  20. return prefix + value.slice(0, index + 1) + value.slice(index).replace(regExp, '');
  21. }
  22. function formatNumber(value, allowDot, allowMinus) {
  23. if (allowDot === void 0) {
  24. allowDot = true;
  25. }
  26. if (allowMinus === void 0) {
  27. allowMinus = true;
  28. }
  29. if (allowDot) {
  30. value = trimExtraChar(value, '.', /\./g);
  31. } else {
  32. value = value.split('.')[0];
  33. }
  34. if (allowMinus) {
  35. value = trimExtraChar(value, '-', /-/g);
  36. } else {
  37. value = value.replace(/-/, '');
  38. }
  39. var regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;
  40. return value.replace(regExp, '');
  41. }