Skip to content

Commit

Permalink
feat: add mac address regex
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianMaliszewski committed Mar 8, 2021
1 parent 04c2c97 commit 80e303d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/regex/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
digits,
email,
fourDigitsCode,
macAddress,
phone,
sixDigitsCode,
spaces,
Expand All @@ -44,6 +45,7 @@ const sixDigitsCodeTest = '123456'
const url1 = 'http://console.scaleway.com'
const url2 = 'https://www.scaleway.com'
const whitespace = ' \t\n\r\x0b\x0c'
const macAddress1 = '1F:B5:FA:47:CD:C4'

describe('@regex', () => {
describe('alpha', () => {
Expand All @@ -61,6 +63,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alpha.test(string)).toBe(result)),
Expand All @@ -82,6 +85,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanum.test(string)).toBe(result)),
Expand All @@ -103,6 +107,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdash.test(string)).toBe(result)),
Expand All @@ -124,6 +129,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdashdots.test(string)).toBe(result)),
Expand All @@ -145,6 +151,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdashdotsorempty.test(string)).toBe(result)),
Expand All @@ -166,6 +173,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, true],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdashdotsspaces.test(string)).toBe(result)),
Expand All @@ -187,6 +195,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdashorempty.test(string)).toBe(result)),
Expand All @@ -208,6 +217,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, true],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdashspaces.test(string)).toBe(result)),
Expand All @@ -229,6 +239,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumdots.test(string)).toBe(result)),
Expand All @@ -248,6 +259,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumLowercase.test(string)).toBe(result)),
Expand All @@ -267,6 +279,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, true],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumSpacesDotsUnderscoreDash.test(string)).toBe(result)),
Expand All @@ -286,6 +299,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumUnderscoreDash.test(string)).toBe(result)),
Expand All @@ -305,6 +319,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(alphanumUnderscoreDollarDash.test(string)).toBe(result)),
Expand Down Expand Up @@ -344,6 +359,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(backupKey.test(string)).toBe(result)),
Expand Down Expand Up @@ -382,6 +398,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(digits.test(string)).toBe(result)),
Expand All @@ -401,6 +418,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(email.test(string)).toBe(result)),
Expand All @@ -421,12 +439,34 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(fourDigitsCode.test(string)).toBe(result)),
)
})

describe('macAddress', () => {
;[
[asciiLetters, false],
[asciiLowercase, false],
[asciiUppercase, false],
[digitsTest, false],
[emailTest, false],
[octdigits, false],
[fourDigitsTest, false],
[hexdigits, false],
[printable, false],
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, true],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(macAddress.test(string)).toBe(result)),
)
})

describe('phone', () => {
;[
[asciiLetters, false],
Expand All @@ -442,6 +482,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(phone.test(string)).toBe(result)),
Expand All @@ -463,6 +504,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, true],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(spaces.test(string)).toBe(result)),
Expand All @@ -485,6 +527,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
].forEach(([string, result]) =>
it(`should match regex ${string} to be ${result}`, () =>
expect(sixDigitsCode.test(string)).toBe(result)),
Expand All @@ -507,6 +550,7 @@ describe('@regex', () => {
[punctuation, false],
[whitespace, false],
[cronTest, false],
[macAddress1, false],
[url1, true],
[url2, true],
].forEach(([string, result]) =>
Expand Down
1 change: 1 addition & 0 deletions packages/regex/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ascii = /^[\x00-\x7F]+$/
export const backupKey = /^[A-Z0-9]{32}$/
export const cron = /^[0-9,/*-]+$/
export const digits = /^[0-9]*$/
export const macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
// Used by W3C
export const email = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
export const fourDigitsCode = /^[0-9]{4}$/
Expand Down

0 comments on commit 80e303d

Please sign in to comment.