number.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. export { isNumber, clamp } from 'lodash-es';
  2. /**
  3. * Returns the remainder of division of `n` by `m`. You should use this
  4. * instead of the built-in operation as the built-in operation does not
  5. * properly handle negative numbers.
  6. */
  7. export function mod(n, m) {
  8. return ((n % m) + m) % m;
  9. }
  10. export function random(lower, upper) {
  11. if (upper == null) {
  12. upper = lower == null ? 1 : lower; // eslint-disable-line
  13. lower = 0; // eslint-disable-line
  14. }
  15. else if (upper < lower) {
  16. const tmp = lower;
  17. lower = upper; // eslint-disable-line
  18. upper = tmp; // eslint-disable-line
  19. }
  20. return Math.floor(Math.random() * (upper - lower + 1) + lower);
  21. }
  22. export function isPercentage(val) {
  23. return typeof val === 'string' && val.slice(-1) === '%';
  24. }
  25. export function normalizePercentage(num, ref) {
  26. if (num == null) {
  27. return 0;
  28. }
  29. let raw;
  30. if (typeof num === 'string') {
  31. raw = parseFloat(num);
  32. if (isPercentage(num)) {
  33. raw /= 100;
  34. if (Number.isFinite(raw)) {
  35. return raw * ref;
  36. }
  37. }
  38. }
  39. else {
  40. raw = num;
  41. }
  42. if (!Number.isFinite(raw)) {
  43. return 0;
  44. }
  45. if (raw > 0 && raw < 1) {
  46. return raw * ref;
  47. }
  48. return raw;
  49. }
  50. export function parseCssNumeric(val, units) {
  51. function getUnit(regexp) {
  52. const matches = new RegExp(`(?:\\d+(?:\\.\\d+)*)(${regexp})$`).exec(val);
  53. if (!matches) {
  54. return null;
  55. }
  56. return matches[1];
  57. }
  58. const number = parseFloat(val);
  59. if (Number.isNaN(number)) {
  60. return null;
  61. }
  62. // determine the unit
  63. let regexp;
  64. if (units == null) {
  65. // accept any unit, as well as no unit
  66. regexp = '[A-Za-z]*';
  67. }
  68. else if (Array.isArray(units)) {
  69. if (units.length === 0) {
  70. return null;
  71. }
  72. regexp = units.join('|');
  73. }
  74. else if (typeof units === 'string') {
  75. regexp = units;
  76. }
  77. const unit = getUnit(regexp);
  78. if (unit === null) {
  79. return null;
  80. }
  81. return {
  82. unit,
  83. value: number,
  84. };
  85. }
  86. export function normalizeSides(box) {
  87. if (typeof box === 'object') {
  88. let left = 0;
  89. let top = 0;
  90. let right = 0;
  91. let bottom = 0;
  92. if (box.vertical != null && Number.isFinite(box.vertical)) {
  93. top = bottom = box.vertical;
  94. }
  95. if (box.horizontal != null && Number.isFinite(box.horizontal)) {
  96. right = left = box.horizontal;
  97. }
  98. if (box.left != null && Number.isFinite(box.left))
  99. left = box.left;
  100. if (box.top != null && Number.isFinite(box.top))
  101. top = box.top;
  102. if (box.right != null && Number.isFinite(box.right))
  103. right = box.right;
  104. if (box.bottom != null && Number.isFinite(box.bottom))
  105. bottom = box.bottom;
  106. return { top, right, bottom, left };
  107. }
  108. let val = 0;
  109. if (box != null && Number.isFinite(box)) {
  110. val = box;
  111. }
  112. return { top: val, right: val, bottom: val, left: val };
  113. }
  114. //# sourceMappingURL=number.js.map