Skip to content

Commit ecc8633

Browse files
committed
style: format code
1 parent 0a91392 commit ecc8633

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

lib/image-compression.js

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
getExifOrientation,
77
getNewCanvasAndCtx,
88
handleMaxWidthOrHeight,
9-
isAutoOrientationInBrowser
10-
} from './utils'
9+
isAutoOrientationInBrowser,
10+
} from './utils';
1111

1212
/**
1313
* Compress an image file.
@@ -25,93 +25,95 @@ import {
2525
* @param {number} previousProgress - for internal try catch rerunning start from previous progress
2626
* @returns {Promise<File | Blob>}
2727
*/
28-
export default async function compress (file, options, previousProgress = 0) {
29-
let progress = previousProgress
28+
export default async function compress(file, options, previousProgress = 0) {
29+
let progress = previousProgress;
3030

31-
function incProgress (inc = 5) {
32-
progress += inc
33-
options.onProgress(Math.min(progress, 100))
31+
function incProgress(inc = 5) {
32+
progress += inc;
33+
options.onProgress(Math.min(progress, 100));
3434
}
3535

36-
function setProgress (p) {
37-
progress = Math.min(Math.max(p, progress), 100)
38-
options.onProgress(progress)
36+
function setProgress(p) {
37+
progress = Math.min(Math.max(p, progress), 100);
38+
options.onProgress(progress);
3939
}
4040

41-
let remainingTrials = options.maxIteration || 10
41+
let remainingTrials = options.maxIteration || 10;
4242

43-
const maxSizeByte = options.maxSizeMB * 1024 * 1024
43+
const maxSizeByte = options.maxSizeMB * 1024 * 1024;
4444

45-
incProgress()
45+
incProgress();
4646

4747
// drawFileInCanvas
48-
let [img, origCanvas] = await drawFileInCanvas(file)
48+
const [, origCanvas] = await drawFileInCanvas(file);
4949

50-
incProgress()
50+
incProgress();
5151

5252
// handleMaxWidthOrHeight
53-
const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)
53+
const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options);
5454

55-
incProgress()
55+
incProgress();
5656

5757
// exifOrientation
58-
const exifOrientation = options.exifOrientation || await getExifOrientation(file)
59-
incProgress()
60-
const orientationFixedCanvas = (await isAutoOrientationInBrowser()) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, exifOrientation)
61-
incProgress()
58+
const exifOrientation = options.exifOrientation || await getExifOrientation(file);
59+
incProgress();
60+
const orientationFixedCanvas = (await isAutoOrientationInBrowser()) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, exifOrientation);
61+
incProgress();
6262

63-
let quality = options.initialQuality || 1.0
63+
let quality = options.initialQuality || 1.0;
6464

65-
const outputFileType = options.fileType || file.type
65+
const outputFileType = options.fileType || file.type;
6666

67-
let tempFile = await canvasToFile(orientationFixedCanvas, outputFileType, file.name, file.lastModified, quality)
68-
incProgress()
67+
const tempFile = await canvasToFile(orientationFixedCanvas, outputFileType, file.name, file.lastModified, quality);
68+
incProgress();
6969

70-
const origExceedMaxSize = tempFile.size > maxSizeByte
71-
const sizeBecomeLarger = tempFile.size > file.size
70+
const origExceedMaxSize = tempFile.size > maxSizeByte;
71+
const sizeBecomeLarger = tempFile.size > file.size;
7272
// console.log('original file size', file.size)
7373
// console.log('current file size', tempFile.size)
7474

7575
// check if we need to compress or resize
7676
if (!origExceedMaxSize && !sizeBecomeLarger) {
7777
// no need to compress
78-
setProgress(100)
79-
return tempFile
78+
setProgress(100);
79+
return tempFile;
8080
}
8181

82-
const sourceSize = file.size
83-
const renderedSize = tempFile.size
84-
let currentSize = renderedSize
85-
let compressedFile
86-
let newCanvas, ctx
87-
let canvas = orientationFixedCanvas
82+
const sourceSize = file.size;
83+
const renderedSize = tempFile.size;
84+
let currentSize = renderedSize;
85+
let compressedFile;
86+
let newCanvas; let
87+
ctx;
88+
let canvas = orientationFixedCanvas;
8889
while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {
89-
const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width
90+
const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width;
9091
const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;
9192
// console.log('current width', newWidth);
9293
// console.log('current height', newHeight);
93-
[newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)
94+
[newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight);
9495

95-
ctx.drawImage(canvas, 0, 0, newWidth, newHeight)
96+
ctx.drawImage(canvas, 0, 0, newWidth, newHeight);
9697

97-
quality *= 0.95
98-
compressedFile = await canvasToFile(newCanvas, outputFileType, file.name, file.lastModified, quality)
98+
quality *= 0.95;
99+
// eslint-disable-next-line no-await-in-loop
100+
compressedFile = await canvasToFile(newCanvas, outputFileType, file.name, file.lastModified, quality);
99101

100-
cleanupCanvasMemory(canvas)
102+
cleanupCanvasMemory(canvas);
101103

102-
canvas = newCanvas
104+
canvas = newCanvas;
103105

104-
currentSize = compressedFile.size
106+
currentSize = compressedFile.size;
105107
// console.log('currentSize', currentSize)
106-
setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))
108+
setProgress(Math.min(99, Math.floor(((renderedSize - currentSize) / (renderedSize - maxSizeByte)) * 100)));
107109
}
108110

109-
cleanupCanvasMemory(canvas)
110-
cleanupCanvasMemory(newCanvas)
111-
cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)
112-
cleanupCanvasMemory(orientationFixedCanvas)
113-
cleanupCanvasMemory(origCanvas)
111+
cleanupCanvasMemory(canvas);
112+
cleanupCanvasMemory(newCanvas);
113+
cleanupCanvasMemory(maxWidthOrHeightFixedCanvas);
114+
cleanupCanvasMemory(orientationFixedCanvas);
115+
cleanupCanvasMemory(origCanvas);
114116

115-
setProgress(100)
116-
return compressedFile
117+
setProgress(100);
118+
return compressedFile;
117119
}

0 commit comments

Comments
 (0)