MAC.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const 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. * @constant
  38. * @default
  39. */
  40. IPV6_INTERFACE_ID: false,
  41. /**
  42. * Format MAC addresses operation.
  43. *
  44. * @param {string} input
  45. * @param {Object[]} args
  46. * @returns {string}
  47. */
  48. runFormat: function(input, args) {
  49. if (!input) return "";
  50. let outputCase = args[0],
  51. noDelim = args[1],
  52. dashDelim = args[2],
  53. colonDelim = args[3],
  54. ciscoStyle = args[4],
  55. ipv6IntID = args[5],
  56. outputList = [],
  57. macs = input.toLowerCase().split(/[,\s\r\n]+/);
  58. macs.forEach(function(mac) {
  59. let cleanMac = mac.replace(/[:.-]+/g, ""),
  60. macHyphen = cleanMac.replace(/(.{2}(?=.))/g, "$1-"),
  61. macColon = cleanMac.replace(/(.{2}(?=.))/g, "$1:"),
  62. macCisco = cleanMac.replace(/(.{4}(?=.))/g, "$1."),
  63. macIPv6 = cleanMac.slice(0, 6) + "fffe" + cleanMac.slice(6);
  64. macIPv6 = macIPv6.replace(/(.{4}(?=.))/g, "$1:");
  65. let bite = parseInt(macIPv6.slice(0, 2), 16) ^ 2;
  66. bite = bite.toString(16).padStart(2, "0");
  67. macIPv6 = bite + macIPv6.slice(2);
  68. if (outputCase === "Lower only") {
  69. if (noDelim) outputList.push(cleanMac);
  70. if (dashDelim) outputList.push(macHyphen);
  71. if (colonDelim) outputList.push(macColon);
  72. if (ciscoStyle) outputList.push(macCisco);
  73. if (ipv6IntID) outputList.push(macIPv6);
  74. } else if (outputCase === "Upper only") {
  75. if (noDelim) outputList.push(cleanMac.toUpperCase());
  76. if (dashDelim) outputList.push(macHyphen.toUpperCase());
  77. if (colonDelim) outputList.push(macColon.toUpperCase());
  78. if (ciscoStyle) outputList.push(macCisco.toUpperCase());
  79. if (ipv6IntID) outputList.push(macIPv6.toUpperCase());
  80. } else {
  81. if (noDelim) outputList.push(cleanMac, cleanMac.toUpperCase());
  82. if (dashDelim) outputList.push(macHyphen, macHyphen.toUpperCase());
  83. if (colonDelim) outputList.push(macColon, macColon.toUpperCase());
  84. if (ciscoStyle) outputList.push(macCisco, macCisco.toUpperCase());
  85. if (ipv6IntID) outputList.push(macIPv6, macIPv6.toUpperCase());
  86. }
  87. outputList.push(
  88. "" // Empty line to delimit groups
  89. );
  90. });
  91. // Return the data as a string
  92. return outputList.join("\n");
  93. },
  94. };
  95. export default MAC;