mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat: add is-valid-ip-address (#1591)
This commit is contained in:
25
apps/web/src/utils/__tests__/is-valid-ip-address.spec.ts
Normal file
25
apps/web/src/utils/__tests__/is-valid-ip-address.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { isValidIPAddress } from '../is-valid-ip-address';
|
||||
|
||||
describe('isValidIpAddress', () => {
|
||||
test('should return true for valid IP address', () => {
|
||||
['115.42.150.37', '192.168.0.1', '110.234.52.124'].forEach(ip => {
|
||||
expect(isValidIPAddress(ip)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('should return false for invalid IP address', () => {
|
||||
[
|
||||
'210.110',
|
||||
'255',
|
||||
'y.y.y.y',
|
||||
'255.0.0.y',
|
||||
'666.10.10.20',
|
||||
'4444.11.11.11',
|
||||
'33.3333.33.3',
|
||||
].forEach(ip => {
|
||||
expect(isValidIPAddress(ip)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
5
apps/web/src/utils/is-valid-ip-address.ts
Normal file
5
apps/web/src/utils/is-valid-ip-address.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export function isValidIPAddress(address: string) {
|
||||
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
|
||||
address
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user