diff --git a/resources/scripts/helpers/extractSearchFilters.spec.ts b/resources/scripts/helpers/extractSearchFilters.spec.ts index f5a11ea20..8f896ee4e 100644 --- a/resources/scripts/helpers/extractSearchFilters.spec.ts +++ b/resources/scripts/helpers/extractSearchFilters.spec.ts @@ -1,3 +1,5 @@ +import { describe, expect, it } from 'vitest'; + import extractSearchFilters from '@/helpers/extractSearchFilters'; type TestCase = [string, 0 | Record]; diff --git a/resources/scripts/helpers/splitStringWhitespace.spec.ts b/resources/scripts/helpers/splitStringWhitespace.spec.ts index b9d1b2c34..ad4353af0 100644 --- a/resources/scripts/helpers/splitStringWhitespace.spec.ts +++ b/resources/scripts/helpers/splitStringWhitespace.spec.ts @@ -1,3 +1,5 @@ +import { describe, expect, it } from 'vitest'; + import splitStringWhitespace from '@/helpers/splitStringWhitespace'; describe('@/helpers/splitStringWhitespace.ts', function () { diff --git a/resources/scripts/lib/formatters.spec.ts b/resources/scripts/lib/formatters.spec.ts index c786f5869..a14631389 100644 --- a/resources/scripts/lib/formatters.spec.ts +++ b/resources/scripts/lib/formatters.spec.ts @@ -49,15 +49,27 @@ describe('@/lib/formatters.ts', () => { expect(ip('127.0.0.1')).equals('127.0.0.1'); }); + it('should format an IPv4 address with a port', () => { + expect(ip('127.0.0.1:25565')).equals('[127.0.0.1:25565]'); + }); + it('should format an IPv6 address', () => { + expect(ip('::1')).equals('[::1]'); expect(ip(':::1')).equals('[:::1]'); expect(ip('2001:db8::')).equals('[2001:db8::]'); + + expect(ip('[::1]')).equals('[::1]'); + expect(ip('[2001:db8::]')).equals('[2001:db8::]'); + }); + + it('should format an IPv6 address with a port', () => { + expect(ip('[::1]:25565')).equals('[::1]:25565'); + expect(ip('[2001:db8::]:25565')).equals('[2001:db8::]:25565'); }); it('should handle random inputs', () => { expect(ip('1')).equals('1'); expect(ip('foobar')).equals('foobar'); - expect(ip('127.0.0.1:25565')).equals('[127.0.0.1:25565]'); }); }); }); diff --git a/resources/scripts/lib/formatters.ts b/resources/scripts/lib/formatters.ts index c15cc3d6b..177774c26 100644 --- a/resources/scripts/lib/formatters.ts +++ b/resources/scripts/lib/formatters.ts @@ -27,8 +27,12 @@ function bytesToString(bytes: number, decimals = 2): string { * Formats an IPv4 or IPv6 address. */ function ip(value: string): string { - // noinspection RegExpSimplifiable - return /^([a-f0-9:]+:+)+[a-f0-9]+$/.test(value) ? `[${value}]` : value; + // Check if the value is already formatted. + if (value.length > 0 && value[0] === '[') { + return value; + } + + return /([a-f0-9:]+:+)+[a-f0-9]+/.test(value) ? `[${value}]` : value; } export { ip, mbToBytes, bytesToString };