number_utils.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. module.exports = {
  18. /**
  19. * Convert byte size to other metrics.
  20. *
  21. * @param {Number} bytes to convert to string
  22. * @param {Number} precision Number to adjust precision of return value. Default is 0.
  23. * @param {String} parseType
  24. * JS method name for parse string to number. Default is "parseInt".
  25. * @param {Number} multiplyBy bytes by this number if given. This is needed
  26. * as <code>null * 1024 = 0</null>
  27. * @remarks The parseType argument can be "parseInt" or "parseFloat".
  28. * @return {String} Returns converted value with abbreviation.
  29. */
  30. bytesToSize: function (bytes, precision, parseType, multiplyBy) {
  31. if (bytes === null || bytes === undefined) {
  32. return 'n/a';
  33. } else {
  34. if (arguments[2] === undefined) {
  35. parseType = 'parseInt';
  36. }
  37. if (arguments[3] === undefined) {
  38. multiplyBy = 1;
  39. }
  40. var value = bytes * multiplyBy;
  41. var sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB' ];
  42. var posttxt = 0;
  43. while (value >= 1024) {
  44. posttxt++;
  45. value = value / 1024;
  46. }
  47. if (value === 0) {
  48. precision = 0;
  49. }
  50. var parsedValue = window[parseType](value);
  51. return parsedValue.toFixed(precision) + " " + sizes[posttxt];
  52. }
  53. },
  54. /**
  55. * Validates if the given string or number is an integer between the
  56. * values of min and max (inclusive). The minimum and maximum
  57. * checks are ignored if their valid is NaN.
  58. *
  59. * @method validateInteger
  60. * @param {string|number} str - input string
  61. * @param {string|number} [min]
  62. * @param {string|number} [max]
  63. */
  64. validateInteger : function(str, min, max) {
  65. if (str==null || str==undefined || (str + "").trim().length < 1) {
  66. return Em.I18n.t('number.validate.empty');
  67. } else {
  68. str = (str + "").trim();
  69. var number = parseInt(str);
  70. if (isNaN(number)) {
  71. return Em.I18n.t('number.validate.notValidNumber');
  72. } else {
  73. if (str.length != (number + "").length) {
  74. // parseInt("1abc") returns 1 as integer
  75. return Em.I18n.t('number.validate.notValidNumber');
  76. }
  77. if (!isNaN(min) && number < min) {
  78. return Em.I18n.t('number.validate.lessThanMinumum').format(min);
  79. }
  80. if (!isNaN(max) && number > max) {
  81. return Em.I18n.t('number.validate.moreThanMaximum').format(max);
  82. }
  83. }
  84. }
  85. return null;
  86. }
  87. };