misc.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. /**
  20. * Convert value from bytes to appropriate measure
  21. */
  22. formatBandwidth: function (value) {
  23. if (value) {
  24. if (value < 1024) {
  25. value = '<1KB';
  26. } else {
  27. if (value < 1048576) {
  28. value = (value / 1024).toFixed(1) + 'KB';
  29. } else if (value >= 1048576 && value < 1073741824){
  30. value = (value / 1048576).toFixed(1) + 'MB';
  31. } else {
  32. value = (value / 1073741824).toFixed(2) + 'GB';
  33. }
  34. }
  35. }
  36. return value;
  37. },
  38. /**
  39. * Convert ip address to integer
  40. * @param ip
  41. * @return integer
  42. */
  43. ipToInt: function(ip){
  44. // * example 1: ipToInt('192.0.34.166');
  45. // * returns 1: 3221234342
  46. // * example 2: ipToInt('255.255.255.256');
  47. // * returns 2: false
  48. // Verify IP format.
  49. if (!/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip)) {
  50. return false; // Invalid format.
  51. }
  52. // Reuse ip variable for component counter.
  53. var d = ip.split('.');
  54. return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
  55. }
  56. };