Skip to content

Commit 4551a4d

Browse files
committed
feat: add convertDataUrlToBlob util
1 parent 1d42bec commit 4551a4d

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

jest-setup-file.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const fetch = require('node-fetch');
2+
3+
global.fetch = fetch;
4+
15
/**
26
* This file is referenced as a testing setup file for Jest. It runs once per test file immediately before executing the test.
37
*

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
"@ionic/core": "5.1.1",
3434
"@types/jest": "25.2.3",
3535
"@types/node": "14.0.6",
36+
"@types/node-fetch": "2.5.7",
3637
"@types/nprogress": "0.2.0",
3738
"jest": "26.0.1",
3839
"moment": "2.26.0",
3940
"moment-timezone": "0.5.31",
41+
"node-fetch": "3.0.0-beta.7",
4042
"nprogress": "0.2.0",
4143
"prettier": "2.0.5",
4244
"prettier-plugin-organize-imports": "1.0.4",

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The class implements `toValue`, `toJSON` and `toString` for easier serialization
119119
* `stopPropagation(event)`: Handler to stop event propagation.
120120
* `readFile(file)`: Read a file using a `FileReader` and return its data as a base64 encoded string.
121121
* `uuid()`: Generate a v4 compliant uuid.
122+
* `convertDataUrlToBlob(data)`: Convert the given data url into a blob.
122123

123124
## Publishing
124125

src/utils/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,8 @@ export const uuid = () =>
246246
const v = c === 'x' ? r : (r & 0x3) | 0x8;
247247
return v.toString(16);
248248
});
249+
250+
/**
251+
* Convert a data url to a blob.
252+
*/
253+
export const convertDataUrlToBlob = async (dataUrl: string) => fetch(dataUrl).then((res) => res.blob());

src/utils/utils.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { base64ImageToDataUrl, clone, getByPath, getRandomInt } from '.';
1+
import { base64ImageToDataUrl, clone, convertDataUrlToBlob, getByPath, getRandomInt } from '.';
22

33
describe('Utils', () => {
44
describe('getRandomInt(min, max)', () => {
@@ -88,4 +88,24 @@ describe('Utils', () => {
8888
expect((copy as any).doesNotExist).toBe((object as any).doesNotExist);
8989
});
9090
});
91+
92+
describe('convertDataUrlToBlob', () => {
93+
it('should convert a data url to a blob', async () => {
94+
const jpg = `data:image/jpeg;base64,
95+
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDADIiJSwlHzIsKSw4NTI7S31RS0VFS5ltc1p9tZ++u7Kf
96+
r6zI4f/zyNT/16yv+v/9////////wfD/////////////2wBDATU4OEtCS5NRUZP/zq/O////////
97+
////////////////////////////////////////////////////////////wAARCAAYAEADAREA
98+
AhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAQMAAgQF/8QAJRABAAIBBAEEAgMAAAAAAAAAAQIR
99+
AAMSITEEEyJBgTORUWFx/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAA
100+
AAD/2gAMAwEAAhEDEQA/AOgM52xQDrjvAV5Xv0vfKUALlTQfeBm0HThMNHXkL0Lw/swN5qgA8yT4
101+
MCS1OEOJV8mBz9Z05yfW8iSx7p4j+jA1aD6Wj7ZMzstsfvAas4UyRHvjrAkC9KhpLMClQntlqFc2
102+
X1gUj4viwVObKrddH9YDoHvuujAEuNV+bLwFS8XxdSr+Cq3Vf+4F5RgQl6ZR2p1eAzU/HX80YBYy
103+
JLCuexwJCO2O1bwCRidAfWBSctswbI12GAJT3yiwFR7+MBjGK2g/WAJR3FdF84E2rK5VR0YH/9k=`;
104+
105+
const jpgBlob = await convertDataUrlToBlob(jpg);
106+
107+
expect(jpgBlob.size).toBe(512);
108+
expect(jpgBlob.type).toBe('image/jpeg');
109+
});
110+
});
91111
});

0 commit comments

Comments
 (0)