-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port documentation to JSDoc for
joinUrl
(#957)
docs(joinUrl): port documentation to JSDoc Also make the array of arguments `readonly`, which ensures no mutation of the input is permitted inside the function body. Slightly reword the documentation.
- Loading branch information
Showing
3 changed files
with
25 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@guardian/libs': patch | ||
--- | ||
|
||
`joinUrl`: better documentation using JSDoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
// detect two or more `/` chars after a word boundary | ||
/** detect two or more `/` chars after a word boundary */ | ||
const multipleSlashesInRoute = /\b\/{2,}/g; | ||
|
||
export const joinUrl = (...args: string[]): string => | ||
/** | ||
* Takes a variable number of strings as arguments, | ||
* joining them as a single valid URL string. | ||
* | ||
* Handles trailing or leading spaces and double slashes. | ||
* | ||
* @returns a normalised URL pathname | ||
* | ||
* @example | ||
* ```js | ||
* import { joinUrl } from '@guardian/libs'; | ||
* | ||
* const url = joinUrl('https://example.com/', '/media', '/', '//source.png'); | ||
* console.assert(url === 'https://example.com/media/source.png'); | ||
* ``` | ||
*/ | ||
export const joinUrl = (...args: readonly string[]): string => | ||
args.join('/').replace(multipleSlashesInRoute, '/'); |