isMACAddress.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import assertString from './util/assertString';
  2. var macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
  3. var macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/;
  4. var macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
  5. var macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/;
  6. var macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/;
  7. var macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/;
  8. export default function isMACAddress(str, options) {
  9. assertString(str);
  10. if (options !== null && options !== void 0 && options.eui) {
  11. options.eui = String(options.eui);
  12. }
  13. /**
  14. * @deprecated `no_colons` TODO: remove it in the next major
  15. */
  16. if (options !== null && options !== void 0 && options.no_colons || options !== null && options !== void 0 && options.no_separators) {
  17. if (options.eui === '48') {
  18. return macAddress48NoSeparators.test(str);
  19. }
  20. if (options.eui === '64') {
  21. return macAddress64NoSeparators.test(str);
  22. }
  23. return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str);
  24. }
  25. if ((options === null || options === void 0 ? void 0 : options.eui) === '48') {
  26. return macAddress48.test(str) || macAddress48WithDots.test(str);
  27. }
  28. if ((options === null || options === void 0 ? void 0 : options.eui) === '64') {
  29. return macAddress64.test(str) || macAddress64WithDots.test(str);
  30. }
  31. return isMACAddress(str, {
  32. eui: '48'
  33. }) || isMACAddress(str, {
  34. eui: '64'
  35. });
  36. }