Skip to content

Commit

Permalink
Port documentation to JSDoc for joinUrl (#957)
Browse files Browse the repository at this point in the history
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
mxdvl authored Dec 19, 2023
1 parent 4d8f750 commit f85e2a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-penguins-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/libs': patch
---

`joinUrl`: better documentation using JSDoc
4 changes: 2 additions & 2 deletions libs/@guardian/libs/src/joinUrl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Handles trailing or leading spaces and double slashes.
```js
import { joinUrl } from '@guardian/libs';

const url = joinUrl('http://example.com/ ', ' /abc/', '/xyz/');
// 'http://example.com/abc/xyz'
const url = joinUrl('https://example.com/', '/media', '/', '//source.png');
console.assert(url === 'https://example.com/media/source.png');
```
20 changes: 18 additions & 2 deletions libs/@guardian/libs/src/joinUrl/joinUrl.ts
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, '/');

0 comments on commit f85e2a4

Please sign in to comment.