MAC.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * MAC address operations.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2016
  6. * @license Apache-2.0
  7. *
  8. * @namespace
  9. */
  10. var MAC = {
  11. /**
  12. * @constant
  13. * @default
  14. */
  15. OUTPUT_CASE: ["Both", "Upper only", "Lower only"],
  16. /**
  17. * @constant
  18. * @default
  19. */
  20. NO_DELIM: true,
  21. /**
  22. * @constant
  23. * @default
  24. */
  25. DASH_DELIM: true,
  26. /**
  27. * @constant
  28. * @default
  29. */
  30. COLON_DELIM: true,
  31. /**
  32. * @constant
  33. * @default
  34. */
  35. CISCO_STYLE: false,
  36. /**
  37. * Format MAC addresses operation.
  38. *
  39. * @param {string} input
  40. * @param {Object[]} args
  41. * @returns {string}
  42. */
  43. run_format: function(input, args) {
  44. if (!input) return "";
  45. var output_case = args[0],
  46. no_delim = args[1],
  47. dash_delim = args[2],
  48. colon_delim = args[3],
  49. cisco_style = args[4],
  50. output_list = [],
  51. macs = input.toLowerCase().split(/[,\s\r\n]+/);
  52. macs.forEach(function(mac) {
  53. var cleanMac = mac.replace(/[:.-]+/g, ""),
  54. macHyphen = cleanMac.replace(/(.{2}(?=.))/g, "$1-"),
  55. macColon = cleanMac.replace(/(.{2}(?=.))/g, "$1:"),
  56. macCisco = cleanMac.replace(/(.{4}(?=.))/g, "$1.");
  57. if (output_case === "Lower only") {
  58. if (no_delim) output_list.push(cleanMac);
  59. if (dash_delim) output_list.push(macHyphen);
  60. if (colon_delim) output_list.push(macColon);
  61. if (cisco_style) output_list.push(macCisco);
  62. } else if (output_case === "Upper only") {
  63. if (no_delim) output_list.push(cleanMac.toUpperCase());
  64. if (dash_delim) output_list.push(macHyphen.toUpperCase());
  65. if (colon_delim) output_list.push(macColon.toUpperCase());
  66. if (cisco_style) output_list.push(macCisco.toUpperCase());
  67. } else {
  68. if (no_delim) output_list.push(cleanMac, cleanMac.toUpperCase());
  69. if (dash_delim) output_list.push(macHyphen, macHyphen.toUpperCase());
  70. if (colon_delim) output_list.push(macColon, macColon.toUpperCase());
  71. if (cisco_style) output_list.push(macCisco, macCisco.toUpperCase());
  72. }
  73. output_list.push(
  74. "" // Empty line to delimit groups
  75. );
  76. });
  77. // Return the data as a string
  78. return output_list.join("\n");
  79. },
  80. };