isMACAddress.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isMACAddress;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
  9. var macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/;
  10. var macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
  11. var macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/;
  12. var macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/;
  13. var macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/;
  14. function isMACAddress(str, options) {
  15. (0, _assertString.default)(str);
  16. if (options !== null && options !== void 0 && options.eui) {
  17. options.eui = String(options.eui);
  18. }
  19. /**
  20. * @deprecated `no_colons` TODO: remove it in the next major
  21. */
  22. if (options !== null && options !== void 0 && options.no_colons || options !== null && options !== void 0 && options.no_separators) {
  23. if (options.eui === '48') {
  24. return macAddress48NoSeparators.test(str);
  25. }
  26. if (options.eui === '64') {
  27. return macAddress64NoSeparators.test(str);
  28. }
  29. return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str);
  30. }
  31. if ((options === null || options === void 0 ? void 0 : options.eui) === '48') {
  32. return macAddress48.test(str) || macAddress48WithDots.test(str);
  33. }
  34. if ((options === null || options === void 0 ? void 0 : options.eui) === '64') {
  35. return macAddress64.test(str) || macAddress64WithDots.test(str);
  36. }
  37. return isMACAddress(str, {
  38. eui: '48'
  39. }) || isMACAddress(str, {
  40. eui: '64'
  41. });
  42. }
  43. module.exports = exports.default;
  44. module.exports.default = exports.default;