@bouko/ts 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,3 +12,9 @@ export declare function base64ToFile(data: string | undefined, opts?: {
12
12
  name?: string;
13
13
  type?: string;
14
14
  }): File | undefined;
15
+ export declare function dataToFile(data: string, opts?: {
16
+ name?: string;
17
+ }): File;
18
+ export declare function dataToFile(data: string | undefined, opts?: {
19
+ name?: string;
20
+ }): File | undefined;
@@ -22,3 +22,23 @@ export function base64ToFile(data, opts = {}) {
22
22
  const byteArray = Uint8Array.from(atob(data), c => c.charCodeAt(0));
23
23
  return new File([byteArray], name || "file", { type });
24
24
  }
25
+ export function dataToFile(data, opts) {
26
+ if (!data)
27
+ return;
28
+ // If the data is in "data:[mime];base64,..." format, extract it
29
+ const matches = data.match(/^data:(.*?);base64,(.*)$/);
30
+ let mime;
31
+ let base64Data;
32
+ if (matches) {
33
+ mime = matches[1];
34
+ base64Data = matches[2];
35
+ }
36
+ else {
37
+ // If not a full data URI, just assume it's raw base64 without metadata
38
+ base64Data = data;
39
+ }
40
+ const byteArray = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0));
41
+ return new File([byteArray], opts?.name || "file", {
42
+ type: mime || "application/octet-stream",
43
+ });
44
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  "name": "@bouko/ts",
4
4
 
5
- "version": "0.3.1",
5
+ "version": "0.3.3",
6
6
 
7
7
  "main": "./dist/index.js",
8
8