Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 4831b6f

Browse files
committed
chore: fix prettier errors
1 parent cc01564 commit 4831b6f

File tree

2 files changed

+90
-88
lines changed

2 files changed

+90
-88
lines changed
Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,105 @@
1+
import { vi } from 'vitest';
12

2-
import { vi } from "vitest";
3+
import { createRgbaImage } from '../../../test/testUtils.js';
4+
import { encode } from '../encode.js';
5+
import { encodeBase64 } from '../encodeBase64.js';
36

4-
import { createRgbaImage } from "../../../test/testUtils.js";
5-
import { encode } from "../encode.js";
6-
import { encodeBase64 } from "../encodeBase64.js";
7-
8-
test("basic image (png)",()=>{
7+
test('basic image (png)', () => {
98
const image = testUtils.createGreyImage([
109
[0, 0, 0, 0, 0],
1110
[0, 255, 255, 255, 0],
1211
[0, 255, 0, 255, 0],
1312
[0, 255, 255, 255, 0],
1413
[255, 0, 255, 0, 255],
1514
]);
16-
const base64Url = encodeBase64(image,'png');
15+
const base64Url = encodeBase64(image, 'png');
1716

18-
expect(base64Url).toBe("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==");
17+
expect(base64Url).toBe(
18+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==',
19+
);
1920
});
2021

21-
test("basic image 2 (jpeg)",()=>{
22+
test('basic image 2 (jpeg)', () => {
2223
const image = testUtils.createGreyImage([
2324
[255, 255, 255, 255, 255],
2425
[255, 0, 0, 0, 255],
2526
[255, 0, 0, 0, 255],
2627
[255, 0, 0, 0, 255],
2728
[255, 255, 255, 255, 255],
2829
]);
29-
const format = 'jpeg'
30-
const base64 = encodeBase64(image,format);
31-
const base64Data = Buffer.from(encode(image,{format}))
32-
.toString('base64');
30+
const format = 'jpeg';
31+
const base64 = encodeBase64(image, format);
32+
const base64Data = Buffer.from(encode(image, { format })).toString('base64');
3333
expect(typeof base64).toBe('string');
34-
expect(base64Data).toBe(base64.slice( base64.indexOf(',') + 1));
34+
expect(base64Data).toBe(base64.slice(base64.indexOf(',') + 1));
3535
});
3636

37-
test("legacy image-js test",()=>{
38-
const image = createRgbaImage([[
39-
255,
40-
0,
41-
0,
42-
255, // red
43-
0,
44-
255,
45-
0,
46-
255,// green
47-
0,
48-
0,
49-
255,
50-
255 // blue
51-
],[255,
37+
test('legacy image-js test', () => {
38+
const image = createRgbaImage([
39+
[
40+
255,
41+
0,
42+
0,
43+
255, // red
44+
0,
45+
255,
46+
0,
47+
255, // green
48+
0,
49+
0,
50+
255,
51+
255, // blue
52+
],
53+
[
5254
255,
53-
0,
54-
255,// yellow
5555
255,
5656
0,
57-
255,
58-
255,// magenta
57+
255, // yellow
58+
255,
5959
0,
6060
255,
61-
255,
62-
255,// cyan
63-
],[ 0,
64-
0,
65-
0,
66-
255,// black
67-
255,
68-
255,
69-
255,
70-
255, // white
71-
127,
72-
127,
73-
127,
74-
255,//grey
75-
]]);
76-
const format = 'jpeg';
77-
const url = encodeBase64(image,format);
78-
const base64Data = Buffer.from(encode(image,{format}))
79-
.toString('base64')
80-
expect(typeof url).toBe('string');
81-
expect(base64Data).toBe(url.slice(url.indexOf(',') + 1));
82-
})
83-
test("browser testing",()=>{
61+
255, // magenta
62+
0,
63+
255,
64+
255,
65+
255, // cyan
66+
],
67+
[
68+
0,
69+
0,
70+
0,
71+
255, // black
72+
255,
73+
255,
74+
255,
75+
255, // white
76+
127,
77+
127,
78+
127,
79+
255, //grey
80+
],
81+
]);
82+
const format = 'jpeg';
83+
const url = encodeBase64(image, format);
84+
const base64Data = Buffer.from(encode(image, { format })).toString('base64');
85+
expect(typeof url).toBe('string');
86+
expect(base64Data).toBe(url.slice(url.indexOf(',') + 1));
87+
});
88+
test('browser testing', () => {
8489
const image = testUtils.createGreyImage([
8590
[255, 255, 255, 255, 255],
8691
[255, 0, 0, 0, 255],
8792
[255, 0, 0, 0, 255],
8893
[255, 0, 0, 0, 255],
8994
[255, 255, 255, 255, 255],
9095
]);
91-
const base64Node = encodeBase64(image,'jpg');
92-
vi.stubGlobal('window',()=>{
93-
const base64Browser = encodeBase64(image,'jpg');
94-
expect(base64Browser).not.toBe(base64Node);
95-
})
96-
vi.stubGlobal('document',()=>{
97-
const base64Browser = encodeBase64(image,'jpg');
98-
expect(base64Browser).not.toBe(base64Node);
99-
})
100-
})
96+
const base64Node = encodeBase64(image, 'jpg');
97+
vi.stubGlobal('window', () => {
98+
const base64Browser = encodeBase64(image, 'jpg');
99+
expect(base64Browser).not.toBe(base64Node);
100+
});
101+
vi.stubGlobal('document', () => {
102+
const base64Browser = encodeBase64(image, 'jpg');
103+
expect(base64Browser).not.toBe(base64Node);
104+
});
105+
});

src/save/encodeBase64.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
1-
21
import type { Image } from '../Image.js';
32

4-
import {encode} from './encode.js';
5-
import type {ImageFormat} from './encode.js';
6-
3+
import { encode } from './encode.js';
4+
import type { ImageFormat } from './encode.js';
75

86
/**
97
* Converts image into a base64 URL string.
108
* @param image - Image to get base64 encoding from.
119
* @param format - Image format.
1210
* @returns base64 string.
1311
*/
14-
export function encodeBase64(image:Image,format:ImageFormat){
15-
const isNode = typeof window === 'undefined' && typeof document === 'undefined';
16-
if(isNode){
17-
return encodeBase64InNode(image,format);
18-
}else{
19-
return encodeBase64InBrowser(image,format);
12+
export function encodeBase64(image: Image, format: ImageFormat) {
13+
const isNode =
14+
typeof window === 'undefined' && typeof document === 'undefined';
15+
if (isNode) {
16+
return encodeBase64InNode(image, format);
17+
} else {
18+
return encodeBase64InBrowser(image, format);
2019
}
2120
}
2221

23-
2422
/**
2523
* Converts image into a base64 URL string in NodeJs.
2624
* @param image - Image to get base64 encoding from.
2725
* @param format - Image format.
2826
* @returns base64 string.
2927
*/
30-
function encodeBase64InNode(image:Image,format:ImageFormat){
31-
const encodedData = encode(image,{format});
32-
return `data:image/${format};base64,${(Buffer.from(encodedData)
33-
.toString('base64'))}`
28+
function encodeBase64InNode(image: Image, format: ImageFormat) {
29+
const encodedData = encode(image, { format });
30+
return `data:image/${format};base64,${Buffer.from(encodedData).toString(
31+
'base64',
32+
)}`;
3433
}
3534
/**
3635
* Converts image into a base64 URL string in browser.
3736
* @param image - Image to get base64 encoding from.
3837
* @param format - Image format.
3938
* @returns base64 string.
4039
*/
41-
function encodeBase64InBrowser(image:Image,format:ImageFormat){
42-
const buffer = encode(image,{format});
40+
function encodeBase64InBrowser(image: Image, format: ImageFormat) {
41+
const buffer = encode(image, { format });
4342
let binaryString = '';
44-
for(const el of buffer){
43+
for (const el of buffer) {
4544
binaryString += String.fromCodePoint(el);
4645
}
4746
const base64String = btoa(binaryString);
48-
const dataURL = `data:image/${format};base64,${base64String}`;
49-
return dataURL;
47+
const dataURL = `data:image/${format};base64,${base64String}`;
48+
return dataURL;
5049
}
51-
52-

0 commit comments

Comments
 (0)