refactor(ip-parsing): centralize multi-delim splitting in splitIps
Extend splitIps to accept comma/semicolon/newline delimiters with dedupe, and reuse it in DetailPanel for both the IP list and the service-URL host. Fixes ServiceBadge links pointing at concatenated string when IPs were entered with ';' or newline separators. Adds tests for new delimiters.
This commit is contained in:
@@ -55,6 +55,27 @@ describe('splitIps', () => {
|
||||
expect(splitIps('')).toEqual([])
|
||||
expect(splitIps(' ')).toEqual([])
|
||||
})
|
||||
|
||||
it('splits on semicolons', () => {
|
||||
expect(splitIps('10.0.0.1; 10.0.0.2')).toEqual(['10.0.0.1', '10.0.0.2'])
|
||||
})
|
||||
|
||||
it('splits on newlines', () => {
|
||||
expect(splitIps('10.0.0.1\n10.0.0.2')).toEqual(['10.0.0.1', '10.0.0.2'])
|
||||
})
|
||||
|
||||
it('splits on mixed delimiters', () => {
|
||||
expect(splitIps('10.0.0.1,10.0.0.2; 10.0.0.3\n10.0.0.4')).toEqual([
|
||||
'10.0.0.1',
|
||||
'10.0.0.2',
|
||||
'10.0.0.3',
|
||||
'10.0.0.4',
|
||||
])
|
||||
})
|
||||
|
||||
it('deduplicates repeated IPs', () => {
|
||||
expect(splitIps('10.0.0.1, 10.0.0.1; 10.0.0.2')).toEqual(['10.0.0.1', '10.0.0.2'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('primaryIp', () => {
|
||||
|
||||
@@ -25,16 +25,17 @@ function maskSingle(ip: string): string {
|
||||
*/
|
||||
export function maskIp(ip: string): string {
|
||||
if (!ip) return ip
|
||||
return ip.split(',').map(maskSingle).join(', ')
|
||||
return splitIps(ip).map(maskSingle).join(', ')
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a comma-separated IP string into an array of trimmed values.
|
||||
* Empty string returns [].
|
||||
* Split an IP string into trimmed values. Accepts comma, semicolon, or newline
|
||||
* delimiters (or any combination). Duplicates removed, empty entries dropped.
|
||||
*/
|
||||
export function splitIps(ip: string): string[] {
|
||||
if (!ip?.trim()) return []
|
||||
return ip.split(',').map((s) => s.trim()).filter(Boolean)
|
||||
const parts = ip.split(/[\n,;]+/).map((s) => s.trim()).filter(Boolean)
|
||||
return parts.filter((v, i) => parts.indexOf(v) === i)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user