Skip to content

Commit 22bfef2

Browse files
committed
Applying PR Reviews
1 parent a59c04c commit 22bfef2

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

index.d.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
21
/**
3-
This function is used to determine the filename for each downloaded file.
4-
@param options - The options for renaming.
5-
@returns The new filename for the downloaded file.
2+
@param options - The options for renaming.
3+
4+
@returns - The new filename for the downloaded file.
5+
6+
@description - This function is used to determine the filename for each downloaded file.
67
*/
7-
export type RenameFunction = (options: {url: string; index: number; urls: string[]}) => string;
8+
export type RenameFunction = (options: {
9+
url: string;
10+
index: number;
11+
urls: string[];
12+
}) => string;
813

914
/*
1015
Options for the multiDownload function.
1116
*/
12-
export type MultiDownloadOptions = {
17+
export type Options = {
1318
/**
14-
The function to rename the downloaded files.
15-
@default undefined (use original filenames)
16-
*/
19+
@default undefined - (use original filenames)
20+
21+
@description - A function that returns the new filename for each downloaded file.
22+
*/
1723
rename?: RenameFunction;
1824
/**
1925
The delay time between each file download in milliseconds.
@@ -23,36 +29,42 @@ export type MultiDownloadOptions = {
2329
};
2430

2531
/**
26-
Download multiple files from an array of URLs.
27-
@param urls - The URLs to download.
28-
@param options - The options for downloading.
29-
@returns A promise that resolves when all files have been downloaded.
32+
@param urls - The URLs to download.
33+
34+
@param options - The options for downloading.
35+
36+
@returns - A promise that resolves when all files have been downloaded.
37+
38+
@description - Download multiple files from an array of URLs.
39+
3040
@example
3141
```
32-
import { multiDownload } from 'multi-download';
42+
import { multiDownload } from "multi-download";
3343
3444
async function downloadFiles() {
35-
const urls = [
36-
'https://example.com/file1.txt',
37-
'https://example.com/file2.txt',
38-
'https://example.com/file3.txt',
39-
];
40-
41-
try {
42-
await multiDownload(urls, {
43-
rename: ({ url, index }) => `file${index + 1}.txt`,
44-
downloadInterval: 1000,
45-
});
46-
console.log('Files downloaded successfully');
47-
} catch (error) {
48-
console.error('Error downloading files:', error);
49-
}
45+
46+
const urls = [
47+
"https://example.com/file1.txt",
48+
"https://example.com/file2.txt",
49+
"https://example.com/file3.txt",
50+
];
51+
52+
try {
53+
await multiDownload(urls, {
54+
rename: ({ url, index }) => `file${index + 1}.txt`,
55+
downloadInterval: 1000,
56+
});
57+
console.log("Files downloaded successfully");
58+
} catch (error) {
59+
console.error("Error downloading files:", error);
60+
}
61+
5062
}
5163
52-
downloadFiles();
64+
await downloadFiles();
5365
```
5466
*/
5567
export default function multiDownload(
5668
urls: string[],
57-
options?: MultiDownloadOptions
69+
options?: Options
5870
): Promise<void>;

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const delay = milliseconds =>
2-
new Promise(resolve => {
1+
const delay = (milliseconds) =>
2+
new Promise((resolve) => {
33
setTimeout(resolve, milliseconds);
44
});
55

66
const download = async (url, name) => {
7-
const a = document.createElement('a');
7+
const a = document.createElement("a");
88
a.download = name;
99
a.href = url;
10-
a.style.display = 'none';
10+
a.style.display = "none";
1111
document.body.append(a);
1212
a.click();
1313

@@ -16,31 +16,31 @@ const download = async (url, name) => {
1616
a.remove();
1717
};
1818

19-
const getName = ({url}) => {
19+
const getName = ({ url }) => {
2020
const match = url.match(/\/([^/]+)$/);
21-
return match ? match[1] : 'file';
21+
return match ? match[1] : "file";
2222
};
2323

2424
export default async function multiDownload(urls = [], options = {}) {
25-
const {downloadInterval = 1000, rename = getName} = options;
25+
const { downloadInterval = 1000, rename = getName } = options;
2626
if (!urls?.length) {
27-
throw new Error('`urls` required');
27+
throw new Error("`urls` required");
2828
}
2929

30-
if (urls.some(url => typeof url !== 'string')) {
31-
throw new Error('`urls` must be an array of strings');
30+
if (urls.some((url) => typeof url !== "string")) {
31+
throw new Error("`urls` must be an array of strings");
3232
}
3333

34-
if (typeof downloadInterval !== 'number') {
35-
throw new TypeError('`downloadInterval` must be a number');
34+
if (typeof downloadInterval !== "number") {
35+
throw new TypeError("The `downloadInterval` option must be a number");
3636
}
3737

38-
if (typeof rename !== 'function') {
39-
throw new TypeError('`rename` must be a function');
38+
if (typeof rename !== "function") {
39+
throw new TypeError("The `rename` option must be a function");
4040
}
4141

4242
for (const [index, url] of urls.entries()) {
43-
const name = rename({url, index, urls});
43+
const name = rename({ url, index, urls });
4444
await delay(index * downloadInterval); // eslint-disable-line no-await-in-loop
4545
download(url, name);
4646
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multi-download",
3-
"version": "4.1.0",
3+
"version": "4.0.0",
44
"description": "Download multiple files at once in the browser",
55
"license": "MIT",
66
"repository": "sindresorhus/multi-download",

0 commit comments

Comments
 (0)