Skip to content

cleanPath

Clean an URL by removing duplicate slashes. The protocol part of the URL is not modified.

Available since v1.0.0

import { cleanPath } from '@helpers4/url';
cleanPath(url: string | null | undefined): string | null | undefined
ParameterTypeDescription
urlstring | null | undefinedThe URL string to be processed. Can be string, undefined, or null.

string | null | undefined — The cleaned URL string, or undefined if the input is undefined, or null if the input is null.

Cleans an URL by removing duplicate slashes while preserving the protocol.

cleanPath('/path//to///resource')
// => '/path/to/resource'

The double slash after the protocol (http://) is not modified.

cleanPath('http://example.com//path')
// => 'http://example.com/path'

Returns null for null input and undefined for undefined input.

cleanPath(null)      // => null
cleanPath(undefined) // => undefined

View source on GitHub