Skip to content

Added inferer for mac eui-48 and eui-68 with delimiters . : - #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/formats/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inferMAC, JSONMACFormat } from './mac';
// This is the order the formats will be run in
import { inferDatetime, JSONDateTimeFormat } from "./datetime";
import { inferTimestamp, JSONTimestampFormat } from "./timestamp";
Expand Down Expand Up @@ -63,7 +64,8 @@ export type JSONStringFormat =
| JSONSemverFormat
| JSONJWTStringFormat
| JSONColorFormat
| JSONCreditCardFormat;
| JSONCreditCardFormat
| JSONMACFormat;

const formats = [
inferUri,
Expand All @@ -86,6 +88,7 @@ const formats = [
inferJWT,
inferColor,
inferCreditCard,
inferMAC,
];

export function inferFormat(value: string): JSONStringFormat | undefined {
Expand Down
27 changes: 27 additions & 0 deletions src/formats/mac.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type JSONMACFormat = {
name: "mac";
variant: "EUI-48" | "EUI-64";
splitter: ":" | "." | "-";
};

const macRegexDDot = /^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$/;
const macRegexDot = /^[a-fA-F0-9]{2}(\.[a-fA-F0-9]{2}){5}$/;
const macRegexDash = /^[a-fA-F0-9]{2}(-[a-fA-F0-9]{2}){5}$/;

const mac64RegexDDot = /^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){7}$/;
const mac64RegexDot = /^[a-fA-F0-9]{2}(.[a-fA-F0-9]{2}){7}$/;
const mac64RegexDash = /^[a-fA-F0-9]{2}(-[a-fA-F0-9]{2}){7}$/;

export function inferMAC(value: string): JSONMACFormat | undefined {
if (value.length === 17) {
if (macRegexDDot.exec(value)) return { name: "mac", variant: "EUI-48", splitter: ":" };
else if (macRegexDot.exec(value)) return { name: "mac", variant: "EUI-48", splitter: "." };
else if (macRegexDash.exec(value)) return { name: "mac", variant: "EUI-48", splitter: "-" };
} else if (value.length === 23) {
if (mac64RegexDDot.exec(value)) return { name: "mac", variant: "EUI-64", splitter: ":" };
else if (mac64RegexDot.exec(value)) return { name: "mac", variant: "EUI-64", splitter: "." };
else if (mac64RegexDash.exec(value)) return { name: "mac", variant: "EUI-64", splitter: "-" };
}

return undefined;
}
48 changes: 48 additions & 0 deletions tests/stringFormats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,54 @@ describe("credit cards", () => {
);
});

describe("mac", () => {
test.each([
"38:EE:4A:47:3A:36",
"ad:af:bd:71:12:87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: ":"
},
});
});

test.each([
"38-EE-4A-47-3A-36",
"ad-af-bd-71-12-87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: "-"
},
});
});

test.each([
"38.EE.4A.47.3A.36",
"ad.af.bd.71.12.87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: "."
},
});
});
});


describe("without format", () => {
test.each(["46", "2244994945", "1212092628029698048"])(
"%p should be inferred as having no format",
Expand Down