-
Notifications
You must be signed in to change notification settings - Fork 28
/
lib.js
38 lines (33 loc) · 1.03 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module.exports = (decode, encode) => {
const convertImage = async ({ image, format, quality }) => {
return await encode[format]({
width: image.width,
height: image.height,
data: image.data,
quality
});
};
const convert = async ({ buffer, format, quality, all }) => {
if (!encode[format]) {
throw new Error(`output format needs to be one of [${Object.keys(encode)}]`);
}
if (!all) {
const image = await decode({ buffer });
return await convertImage({ image, format, quality });
}
const images = await decode.all({ buffer });
return images.map(image => {
return {
convert: async () => await convertImage({
image: await image.decode(),
format,
quality
})
};
});
};
return {
one: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: false }),
all: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: true })
};
};