-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isMACAddress): add EUI-64 validation (#1865)
* feat(isMACAddress): add EUI-64 validation * Update src/lib/isMACAddress.js Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com> * Add possible values of eui to README.md Co-authored-by: Rik Smale <WikiRik@users.noreply.github.com> Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com>
- Loading branch information
1 parent
9c12b4c
commit f055c11
Showing
3 changed files
with
148 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import assertString from './util/assertString'; | ||
|
||
const macAddress = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/; | ||
const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/; | ||
const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; | ||
const macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/; | ||
const macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/; | ||
const macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; | ||
const macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/; | ||
const macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/; | ||
const macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/; | ||
|
||
export default function isMACAddress(str, options) { | ||
assertString(str); | ||
if (options?.eui) { | ||
options.eui = String(options.eui); | ||
} | ||
/** | ||
* @deprecated `no_colons` TODO: remove it in the next major | ||
*/ | ||
if (options && (options.no_colons || options.no_separators)) { | ||
return macAddressNoSeparators.test(str); | ||
if (options?.no_colons || options?.no_separators) { | ||
if (options.eui === '48') { | ||
return macAddress48NoSeparators.test(str); | ||
} | ||
if (options.eui === '64') { | ||
return macAddress64NoSeparators.test(str); | ||
} | ||
return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str); | ||
} | ||
|
||
return macAddress.test(str) | ||
|| macAddressWithDots.test(str); | ||
if (options?.eui === '48') { | ||
return macAddress48.test(str) || macAddress48WithDots.test(str); | ||
} | ||
if (options?.eui === '64') { | ||
return macAddress64.test(str) || macAddress64WithDots.test(str); | ||
} | ||
return isMACAddress(str, { eui: '48' }) || isMACAddress(str, { eui: '64' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters