Skip to content
Merged
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
53 changes: 53 additions & 0 deletions packages/regex/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
email,
fourDigitsCode,
hexadecimal,
ip,
ipv4,
ipv6,
macAddress,
phone,
sixDigitsCode,
Expand Down Expand Up @@ -661,4 +664,54 @@ describe('@regex', () => {
expect(hexadecimal.test(string)).toBe(expected)
})
})

describe('ipv4', () => {
test.each([
['192.168.1.1', true],
['127.0.0.1', true],
['0.0.0.0', true],
['255.255.255.255', true],
['1.2.3.4 hi', false],
['256.256.256.256', false],
['999.999.999.999', false],
['1.2.3', false],
])('should match regex %s to be %s', (string, expected) => {
expect(ipv4.test(string)).toBe(expected)
})
})

describe('ipv6', () => {
test.each([
['1:2:3:4:5:6:7::', true],
['1:2:3:4:5:6::8', true],
['1:2::4:5:6:7:8', true],
['1::3:4:5:6:7:8', true],
['::2:3:4:5:6:7:8', true],
['::1.2.3.4', true],
['1:2::4:5:6:7:8 hi', false],
['192.168.1.1', false],
['127.0.0.1', false],
['typebot.io', false],
['256.256.256.256', false],
])('should match regex %s to be %s', (string, expected) => {
expect(ipv6.test(string)).toBe(expected)
})
})

describe('ip', () => {
test.each([
['1:2:3:4:5:6:7::', true],
['1:2:3:4:5:6::8', true],
['::2:3:4:5:6:7:8', true],
['::1.2.3.4', true],
['192.168.1.1', true],
['127.0.0.1', true],
['0.0.0.0', true],
['255.255.255.255', true],
['256.256.256.256', false],
['1:2:3::5:6:7:900.2.3.4', false],
])('should match regex %s to be %s', (string, expected) => {
expect(ip.test(string)).toBe(expected)
})
})
})
24 changes: 24 additions & 0 deletions packages/regex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ export const sixDigitsCode = /^[0-9]{6}$/
export const url =
/^http(s)?:\/\/?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/
export const hexadecimal = /^[0-9a-fA-F]+$/

// Pasted from `ip-regex` package (https://github.com/sindresorhus/ip-regex/blob/main/index.js)
const v4 =
'(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'
const v6segment = '[a-fA-F\\d]{1,4}'
const v6 = `
(?:
(?:${v6segment}:){7}(?:${v6segment}|:)|
(?:${v6segment}:){6}(?:${v4}|:${v6segment}|:)|
(?:${v6segment}:){5}(?::${v4}|(?::${v6segment}){1,2}|:)|
(?:${v6segment}:){4}(?:(?::${v6segment}){0,1}:${v4}|(?::${v6segment}){1,3}|:)|
(?:${v6segment}:){3}(?:(?::${v6segment}){0,2}:${v4}|(?::${v6segment}){1,4}|:)|
(?:${v6segment}:){2}(?:(?::${v6segment}){0,3}:${v4}|(?::${v6segment}){1,5}|:)|
(?:${v6segment}:){1}(?:(?::${v6segment}){0,4}:${v4}|(?::${v6segment}){1,6}|:)|
(?::(?:(?::${v6segment}){0,5}:${v4}|(?::${v6segment}){1,7}|:))
)(?:%[0-9a-zA-Z]{1,})?
`
.replace(/\s*\/\/.*$/gm, '')
.replace(/\n/g, '')
.trim()

export const ip = new RegExp(`^(?:${v4}|${v6})$`)
export const ipv4 = new RegExp(`^${v4}$`)
export const ipv6 = new RegExp(`^${v6}$`)