Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port documentation to JSDoc for joinUrl #957

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '/');