mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-26 17:56:13 -04:00
feat(new tool): random MAC address generator (#657)
* #521 Random MAC address generator * refactor(mac-address-generator): improved ux * refactor(mac-address-generator): improved ux --------- Co-authored-by: Corentin THOMASSET <corentin.thomasset74@gmail.com>
This commit is contained in:
parent
681f7bf644
commit
cc3425dc77
8 changed files with 205 additions and 2 deletions
|
@ -0,0 +1,18 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
export { splitPrefix, generateRandomMacAddress };
|
||||
|
||||
function splitPrefix(prefix: string): string[] {
|
||||
const base = prefix.match(/[^0-9a-f]/i) === null ? prefix.match(/.{1,2}/g) ?? [] : prefix.split(/[^0-9a-f]/i);
|
||||
|
||||
return base.filter(Boolean).map(byte => byte.padStart(2, '0'));
|
||||
}
|
||||
|
||||
function generateRandomMacAddress({ prefix: rawPrefix = '', separator = ':', getRandomByte = () => _.random(0, 255).toString(16).padStart(2, '0') }: { prefix?: string; separator?: string; getRandomByte?: () => string } = {}) {
|
||||
const prefix = splitPrefix(rawPrefix);
|
||||
|
||||
const randomBytes = _.times(6 - prefix.length, getRandomByte);
|
||||
const bytes = [...prefix, ...randomBytes];
|
||||
|
||||
return bytes.join(separator);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue