Skip to content

Commit

Permalink
Add support for URL objects (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
joemaller and sindresorhus authored Dec 1, 2023
1 parent c69f5c7 commit be1db80
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ filenamifyUrl('http://sindresorhus.com/foo?bar=baz');
filenamifyUrl('http://sindresorhus.com/foo', {replacement: '🐴'});
//=> 'sindresorhus.com🐴foo'
filenamifyUrl(new URL('http://sindresorhus.com'));
//=> 'sindresorhus.com'
```
*/
export default function filenamifyUrl(url: string, options?: Options): string;
export default function filenamifyUrl(url: string | URL, options?: Options): string;

export {Options} from 'filenamify';
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import filenamify from 'filenamify';
import humanizeUrl from 'humanize-url';

export default function filenamifyUrl(string, options) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
export default function filenamifyUrl(url, options) {
if (!(typeof url === 'string' || url instanceof URL)) {
throw new TypeError('Expected a string or URL instance');
}

return filenamify(decodeURIComponent(humanizeUrl(string)), options);
return filenamify(decodeURIComponent(humanizeUrl(url.toString())), options);
}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ filenamifyUrl('http://sindresorhus.com/foo?bar=baz');

filenamifyUrl('http://sindresorhus.com/foo', {replacement: '🐴'});
//=> 'sindresorhus.com🐴foo'

filenamifyUrl(new URL('http://sindresorhus.com'));
//=> 'sindresorhus.com'
```

## API
Expand All @@ -28,7 +31,7 @@ Accepts a URL and returns a valid filename.

#### url

Type: `string`
Type: `string | URL`

A URL to convert to a valid filename.

Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ test('main', t => {
t.is(filenamifyUrl('sindresorhus.com/foo', {replacement: '🐴'}), 'sindresorhus.com🐴foo');
t.is(filenamifyUrl('http://www.sindresorhus.com/?query=pageres*|<>:"\\'), 'sindresorhus.com!query=pageres');
});

test('URLs', t => {
t.is(filenamifyUrl(new URL('http://user:pass@www.sindresorhus.com/foo/bar/')), 'sindresorhus.com!foo!bar');
t.is(filenamifyUrl(new URL('http://user@sindresorhus.com')), 'sindresorhus.com');
t.is(filenamifyUrl(new URL('http://www.sindresorhus.com/?query=pageres*|<>:"\\')), 'sindresorhus.com!query=pageres');
});

0 comments on commit be1db80

Please sign in to comment.