yummies 7.11.0 → 7.13.0

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.
Files changed (159) hide show
  1. package/README.md +5 -87
  2. package/async.cjs +179 -48
  3. package/async.cjs.map +1 -1
  4. package/async.d.ts +125 -7
  5. package/async.js +180 -54
  6. package/async.js.map +1 -1
  7. package/chunk-CVq3Gv4J.cjs +50 -0
  8. package/chunk-YKewjYmz.js +37 -0
  9. package/common.cjs +48 -8
  10. package/common.cjs.map +1 -1
  11. package/common.d.ts +53 -2
  12. package/common.js +49 -11
  13. package/common.js.map +1 -1
  14. package/complex.cjs +275 -128
  15. package/complex.cjs.map +1 -1
  16. package/complex.d.ts +66 -0
  17. package/complex.js +275 -133
  18. package/complex.js.map +1 -1
  19. package/cookie.cjs +17 -7
  20. package/cookie.cjs.map +1 -1
  21. package/cookie.d.ts +26 -0
  22. package/cookie.js +18 -9
  23. package/cookie.js.map +1 -1
  24. package/css.cjs +163 -39
  25. package/css.cjs.map +1 -1
  26. package/css.d.ts +115 -6
  27. package/css.js +159 -41
  28. package/css.js.map +1 -1
  29. package/data.cjs +90 -55
  30. package/data.cjs.map +1 -1
  31. package/data.d.ts +50 -0
  32. package/data.js +91 -61
  33. package/data.js.map +1 -1
  34. package/date-time.cjs +594 -412
  35. package/date-time.cjs.map +1 -1
  36. package/date-time.d.ts +105 -0
  37. package/date-time.js +591 -421
  38. package/date-time.js.map +1 -1
  39. package/device.cjs +65 -23
  40. package/device.cjs.map +1 -1
  41. package/device.d.ts +49 -0
  42. package/device.js +66 -31
  43. package/device.js.map +1 -1
  44. package/encodings.cjs +275 -266
  45. package/encodings.cjs.map +1 -1
  46. package/encodings.d.ts +25 -0
  47. package/encodings.js +276 -268
  48. package/encodings.js.map +1 -1
  49. package/errors.cjs +36 -18
  50. package/errors.cjs.map +1 -1
  51. package/errors.d.ts +17 -0
  52. package/errors.js +35 -19
  53. package/errors.js.map +1 -1
  54. package/file.cjs +58 -24
  55. package/file.cjs.map +1 -1
  56. package/file.d.ts +32 -0
  57. package/file.js +59 -27
  58. package/file.js.map +1 -1
  59. package/format.cjs +125 -83
  60. package/format.cjs.map +1 -1
  61. package/format.d.ts +18 -0
  62. package/format.js +118 -82
  63. package/format.js.map +1 -1
  64. package/html.cjs +242 -137
  65. package/html.cjs.map +1 -1
  66. package/html.d.ts +81 -0
  67. package/html.js +239 -150
  68. package/html.js.map +1 -1
  69. package/id.cjs +90 -17
  70. package/id.cjs.map +1 -1
  71. package/id.d.ts +16 -0
  72. package/id.js +89 -24
  73. package/id.js.map +1 -1
  74. package/imports.cjs +57 -29
  75. package/imports.cjs.map +1 -1
  76. package/imports.d.ts +24 -0
  77. package/imports.js +56 -31
  78. package/imports.js.map +1 -1
  79. package/math.cjs +32 -6
  80. package/math.cjs.map +1 -1
  81. package/math.d.ts +33 -0
  82. package/math.js +33 -10
  83. package/math.js.map +1 -1
  84. package/media.cjs +291 -84
  85. package/media.cjs.map +1 -1
  86. package/media.d.ts +204 -2
  87. package/media.js +290 -93
  88. package/media.js.map +1 -1
  89. package/mobx.cjs +449 -193
  90. package/mobx.cjs.map +1 -1
  91. package/mobx.d.ts +108 -0
  92. package/mobx.js +447 -200
  93. package/mobx.js.map +1 -1
  94. package/ms.cjs +37 -10
  95. package/ms.cjs.map +1 -1
  96. package/ms.d.ts +16 -0
  97. package/ms.js +38 -13
  98. package/ms.js.map +1 -1
  99. package/number.cjs +29 -7
  100. package/number.cjs.map +1 -1
  101. package/number.d.ts +16 -0
  102. package/number.js +30 -9
  103. package/number.js.map +1 -1
  104. package/package.json +11 -3
  105. package/parser.cjs +117 -64
  106. package/parser.cjs.map +1 -1
  107. package/parser.d.ts +17 -0
  108. package/parser.js +111 -64
  109. package/parser.js.map +1 -1
  110. package/price.cjs +24 -18
  111. package/price.cjs.map +1 -1
  112. package/price.d.ts +24 -0
  113. package/price.js +25 -20
  114. package/price.js.map +1 -1
  115. package/random.cjs +95 -13
  116. package/random.cjs.map +1 -1
  117. package/random.d.ts +80 -0
  118. package/random.js +96 -22
  119. package/random.js.map +1 -1
  120. package/react.cjs +673 -214
  121. package/react.cjs.map +1 -1
  122. package/react.d.ts +21 -0
  123. package/react.js +674 -239
  124. package/react.js.map +1 -1
  125. package/sound.cjs +30 -9
  126. package/sound.cjs.map +1 -1
  127. package/sound.d.ts +16 -0
  128. package/sound.js +31 -11
  129. package/sound.js.map +1 -1
  130. package/storage.cjs +49 -50
  131. package/storage.cjs.map +1 -1
  132. package/storage.d.ts +24 -0
  133. package/storage.js +50 -53
  134. package/storage.js.map +1 -1
  135. package/text.cjs +67 -34
  136. package/text.cjs.map +1 -1
  137. package/text.d.ts +16 -0
  138. package/text.js +68 -37
  139. package/text.js.map +1 -1
  140. package/type-guard.cjs +292 -72
  141. package/type-guard.cjs.map +1 -1
  142. package/type-guard.d.ts +18 -0
  143. package/type-guard.js +288 -73
  144. package/type-guard.js.map +1 -1
  145. package/types.cjs +0 -2
  146. package/types.d.ts +41 -0
  147. package/types.global.cjs +0 -2
  148. package/types.global.d.ts +41 -0
  149. package/types.global.js +0 -2
  150. package/types.js +0 -2
  151. package/vibrate.cjs +47 -6
  152. package/vibrate.cjs.map +1 -1
  153. package/vibrate.d.ts +39 -1
  154. package/vibrate.js +48 -8
  155. package/vibrate.js.map +1 -1
  156. package/types.cjs.map +0 -1
  157. package/types.global.cjs.map +0 -1
  158. package/types.global.js.map +0 -1
  159. package/types.js.map +0 -1
package/media.cjs CHANGED
@@ -1,99 +1,305 @@
1
- "use strict";
2
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const math = require("yummies/math");
2
+ require("./chunk-CVq3Gv4J.cjs");
3
+ let yummies_math = require("yummies/math");
4
+ //#region src/media.ts
5
+ /**
6
+ * ---header-docs-section---
7
+ * # yummies/media
8
+ *
9
+ * ## Description
10
+ *
11
+ * Binary and media helpers: Base64 data URLs, object URLs, image orientation fixes, and related
12
+ * browser APIs wrapped in promises. Centralizes `FileReader`/`URL.createObjectURL` usage so image
13
+ * pipelines and uploads stay consistent and easier to test or mock.
14
+ *
15
+ * ## Usage
16
+ *
17
+ * ```ts
18
+ * import { blobToBase64 } from "yummies/media";
19
+ * ```
20
+ */
21
+ /**
22
+ * Reads a {@link Blob} as a **data URL** string (`data:<mime>;base64,...`) using {@link FileReader#readAsDataURL}.
23
+ *
24
+ * Useful for previewing uploads, embedding small assets inline, or serializing binary for APIs that
25
+ * expect Base64-in-JSON. The result includes the MIME prefix, not raw Base64 alone — use
26
+ * {@link decodeDataUrl} if you need the payload and type separately.
27
+ *
28
+ * @param blob - Any `Blob` or `File` (files are blobs).
29
+ * @returns Resolves to the data URL string; rejects if reading fails.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const dataUrl = await blobToBase64(file);
34
+ * previewImg.src = dataUrl;
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * const fromFetch = await fetch('/api/export').then((r) => r.blob());
40
+ * const dataUrl = await blobToBase64(fromFetch);
41
+ * ```
42
+ */
4
43
  function blobToBase64(blob) {
5
- return new Promise((resolve, reject) => {
6
- const reader = new FileReader();
7
- reader.onloadend = () => resolve(reader.result);
8
- reader.onerror = reject;
9
- reader.readAsDataURL(blob);
10
- });
44
+ return new Promise((resolve, reject) => {
45
+ const reader = new FileReader();
46
+ reader.onloadend = () => resolve(reader.result);
47
+ reader.onerror = reject;
48
+ reader.readAsDataURL(blob);
49
+ });
11
50
  }
12
- const blobToUrl = (urlOrBlob) => urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;
13
- const fileToBlob = (file) => {
14
- return new Blob([file], { type: file.type });
51
+ /**
52
+ * If `urlOrBlob` is already a string, returns it unchanged. If it is a {@link Blob}, returns
53
+ * `URL.createObjectURL(blob)` a short-lived `blob:` URL valid in this document until
54
+ * {@link URL.revokeObjectURL} is called.
55
+ *
56
+ * Pair with {@link renderImage} or `<img src>` without re-fetching binary data. **Remember to
57
+ * `revokeObjectURL`** when the URL is no longer needed to avoid retaining blob memory.
58
+ *
59
+ * @param urlOrBlob - Remote/http(s) URL string, data URL, or a `Blob` / `File`.
60
+ * @returns A string suitable for `HTMLImageElement.src` or similar.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const src = blobToUrl(uploadedFile);
65
+ * img.src = src;
66
+ * // when done:
67
+ * URL.revokeObjectURL(src);
68
+ * ```
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * blobToUrl('https://cdn.example.com/logo.png'); // passed through as-is
73
+ * ```
74
+ */
75
+ var blobToUrl = (urlOrBlob) => urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;
76
+ /**
77
+ * Creates a new {@link Blob} from a {@link File}, copying the bytes and keeping `file.type` as
78
+ * the blob’s MIME type. Handy when an API accepts `Blob` but you only have `File`, or you want a
79
+ * plain blob without the `File` name/lastModified metadata.
80
+ *
81
+ * @param file - Source file from an `<input type="file">` or drag-and-drop.
82
+ * @returns A `Blob` with the same content and `type` as the file.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const blob = fileToBlob(fileFromInput);
87
+ * await uploadEndpoint(blob);
88
+ * ```
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const blob = fileToBlob(imageFile);
93
+ * const url = URL.createObjectURL(blob);
94
+ * ```
95
+ */
96
+ var fileToBlob = (file) => {
97
+ return new Blob([file], { type: file.type });
15
98
  };
16
- const imageToBlob = (imageElement, mimeType = "image/png") => {
17
- const canvas = document.createElement("canvas");
18
- canvas.width = imageElement.naturalWidth || 300;
19
- canvas.height = imageElement.naturalHeight || 300;
20
- canvas.getContext("2d").drawImage(imageElement, 0, 0);
21
- const dataUri = canvas.toDataURL(mimeType, 1);
22
- const base64data = dataUri.split(",")[1];
23
- const base64MimeType = dataUri.split(";")[0].slice(5);
24
- const bytes = globalThis.atob(base64data);
25
- const buf = new ArrayBuffer(bytes.length);
26
- const array = new Uint8Array(buf);
27
- for (let index = 0; index < bytes.length; index++) {
28
- array[index] = bytes.charCodeAt(index);
29
- }
30
- const blob = new Blob([array], { type: base64MimeType });
31
- return blob;
99
+ /**
100
+ * Draws an {@link HTMLImageElement} onto an offscreen canvas, then builds a {@link Blob} from the
101
+ * raster (via `canvas.toDataURL` + binary decode). Dimensions use `naturalWidth` / `naturalHeight`,
102
+ * falling back to `300×300` if those are zero (e.g. not yet decoded).
103
+ *
104
+ * **CORS:** if the image is cross-origin without proper CORS headers, the canvas may be
105
+ * *tainted* and `toDataURL` can throw — same browser rules as any canvas export.
106
+ *
107
+ * @param imageElement - Loaded image (`complete` / decoded recommended).
108
+ * @param mimeType - Output MIME type, e.g. `'image/png'` (default) or `'image/jpeg'`. Encoder support is browser-dependent.
109
+ * @returns Encoded image as a `Blob` with a matching `type` when possible.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * const img = await renderImage('/photo.jpg');
114
+ * const jpegBlob = imageToBlob(img, 'image/jpeg');
115
+ * ```
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const pngBlob = imageToBlob(cachedImg); // default image/png
120
+ * ```
121
+ */
122
+ var imageToBlob = (imageElement, mimeType = "image/png") => {
123
+ const canvas = document.createElement("canvas");
124
+ canvas.width = imageElement.naturalWidth || 300;
125
+ canvas.height = imageElement.naturalHeight || 300;
126
+ canvas.getContext("2d").drawImage(imageElement, 0, 0);
127
+ const dataUri = canvas.toDataURL(mimeType, 1);
128
+ const base64data = dataUri.split(",")[1];
129
+ const base64MimeType = dataUri.split(";")[0].slice(5);
130
+ const bytes = globalThis.atob(base64data);
131
+ const buf = new ArrayBuffer(bytes.length);
132
+ const array = new Uint8Array(buf);
133
+ for (let index = 0; index < bytes.length; index++) array[index] = bytes.charCodeAt(index);
134
+ return new Blob([array], { type: base64MimeType });
32
135
  };
33
- const renderImage = (urlOrBlob) => new Promise((resolve, reject) => {
34
- const image = new Image();
35
- image.src = blobToUrl(urlOrBlob);
36
- image.onload = () => resolve(image);
37
- image.onerror = () => reject();
136
+ /**
137
+ * Loads a resource into a new `HTMLImageElement`: `src` is set via {@link blobToUrl} (so blobs get
138
+ * object URLs, strings are used directly). Resolves on `load` with the same element; rejects on
139
+ * `error` (e.g. bad URL, network failure, corrupt image) **with no rejection value**.
140
+ *
141
+ * Does not add the node to the DOM — use the returned element for canvas, measuring, or
142
+ * {@link imageToBlob}.
143
+ *
144
+ * @param urlOrBlob - Remote URL, data URL, or `Blob` / `File`.
145
+ * @returns Promise that fulfills with the loaded `HTMLImageElement`.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const img = await renderImage('https://example.com/pic.png');
150
+ * document.body.appendChild(img);
151
+ * ```
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * const img = await renderImage(pickedFile);
156
+ * const blob = imageToBlob(img, 'image/webp');
157
+ * ```
158
+ */
159
+ var renderImage = (urlOrBlob) => new Promise((resolve, reject) => {
160
+ const image = new Image();
161
+ image.src = blobToUrl(urlOrBlob);
162
+ image.onload = () => resolve(image);
163
+ image.onerror = () => reject();
38
164
  });
39
165
  function cropImageFromCanvas(context) {
40
- const canvas = context.canvas;
41
- let w = canvas.width;
42
- let h = canvas.height;
43
- const pix = { x: [], y: [] };
44
- const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
45
- let x;
46
- let y;
47
- let index;
48
- for (y = 0; y < h; y++) {
49
- for (x = 0; x < w; x++) {
50
- index = (y * w + x) * 4;
51
- if (imageData.data[index + 3] > 0) {
52
- pix.x.push(x);
53
- pix.y.push(y);
54
- }
55
- }
56
- }
57
- pix.x.sort((a, b) => a - b);
58
- pix.y.sort((a, b) => a - b);
59
- const n = pix.x.length - 1;
60
- w = 1 + pix.x[n] - pix.x[0];
61
- h = 1 + pix.y[n] - pix.y[0];
62
- const cut = context.getImageData(pix.x[0], pix.y[0], w, h);
63
- canvas.width = w;
64
- canvas.height = h;
65
- context.putImageData(cut, 0, 0);
66
- return canvas;
166
+ const canvas = context.canvas;
167
+ let w = canvas.width;
168
+ let h = canvas.height;
169
+ const pix = {
170
+ x: [],
171
+ y: []
172
+ };
173
+ const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
174
+ let x;
175
+ let y;
176
+ let index;
177
+ for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
178
+ index = (y * w + x) * 4;
179
+ if (imageData.data[index + 3] > 0) {
180
+ pix.x.push(x);
181
+ pix.y.push(y);
182
+ }
183
+ }
184
+ pix.x.sort((a, b) => a - b);
185
+ pix.y.sort((a, b) => a - b);
186
+ const n = pix.x.length - 1;
187
+ w = 1 + pix.x[n] - pix.x[0];
188
+ h = 1 + pix.y[n] - pix.y[0];
189
+ const cut = context.getImageData(pix.x[0], pix.y[0], w, h);
190
+ canvas.width = w;
191
+ canvas.height = h;
192
+ context.putImageData(cut, 0, 0);
193
+ return canvas;
67
194
  }
68
- const rotateImage = (image, angle) => {
69
- const maxSize = Math.max(image.width, image.height);
70
- const canvas = document.createElement("canvas");
71
- canvas.width = maxSize;
72
- canvas.height = maxSize;
73
- const context = canvas.getContext("2d");
74
- context.save();
75
- context.translate(canvas.width / 2, canvas.height / 2);
76
- context.rotate(math.degToRad(angle));
77
- context.drawImage(image, -image.width / 2, -image.height / 2);
78
- context.restore();
79
- cropImageFromCanvas(context);
80
- return renderImage(canvas.toDataURL("image/png"));
195
+ /**
196
+ * Rotates `image` around its center on a square canvas (side = max(width, height)), then **crops**
197
+ * transparent margins by trimming to the bounding box of pixels with non-zero alpha.
198
+ * Returns a **new** loaded `HTMLImageElement` (PNG data URL under the hood via {@link renderImage}).
199
+ *
200
+ * `angle` is in **degrees**; converted with {@link degToRad} from `yummies/math`.
201
+ *
202
+ * Very large sources can stress memory on some mobile browsers (known iPhone issues see TODO in source).
203
+ *
204
+ * @param image - Source image (should be decoded; uses `width` / `height`).
205
+ * @param angle - Rotation in degrees (e.g. `90` for quarter turn).
206
+ * @returns Promise of a new `HTMLImageElement` showing the rotated, cropped result.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * const upright = await rotateImage(landscapeImg, 90);
211
+ * ```
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * const fixed = await rotateImage(await renderImage(file), -15);
216
+ * ```
217
+ */
218
+ var rotateImage = (image, angle) => {
219
+ const maxSize = Math.max(image.width, image.height);
220
+ const canvas = document.createElement("canvas");
221
+ canvas.width = maxSize;
222
+ canvas.height = maxSize;
223
+ const context = canvas.getContext("2d");
224
+ context.save();
225
+ context.translate(canvas.width / 2, canvas.height / 2);
226
+ context.rotate((0, yummies_math.degToRad)(angle));
227
+ context.drawImage(image, -image.width / 2, -image.height / 2);
228
+ context.restore();
229
+ cropImageFromCanvas(context);
230
+ return renderImage(canvas.toDataURL("image/png"));
81
231
  };
232
+ /**
233
+ * Parses a `data:` URL of the form `data:<mime>;base64,<payload>` into its MIME type and raw
234
+ * Base64 body (without the `data:...;base64,` prefix). Non-matching strings yield `undefined`
235
+ * fields in the result object.
236
+ *
237
+ * @param url - Full data URL string.
238
+ * @returns `{ mimeType, data }` — both optional when the regex does not match.
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * const { mimeType, data } = decodeDataUrl('data:image/png;base64,iVBORw0KGgo=');
243
+ * // mimeType === 'image/png', data === 'iVBORw0KGgo='
244
+ * ```
245
+ *
246
+ * @example
247
+ * ```ts
248
+ * decodeDataUrl('not-a-data-url'); // { mimeType: undefined, data: undefined }
249
+ * ```
250
+ */
82
251
  function decodeDataUrl(url) {
83
- const regex = /^data:(.*);base64,\s?(.*)$/;
84
- const matches = new RegExp(regex).exec(url);
85
- return {
86
- mimeType: matches?.[1],
87
- data: matches?.[2]
88
- };
252
+ const matches = (/* @__PURE__ */ new RegExp(/^data:(.*);base64,\s?(.*)$/)).exec(url);
253
+ return {
254
+ mimeType: matches?.[1],
255
+ data: matches?.[2]
256
+ };
89
257
  }
90
- const isHttpUrl = (url) => {
91
- return url.startsWith("https://") || url.startsWith("http://");
258
+ /**
259
+ * Returns `true` if `url` starts with `https://` or `http://` (case-sensitive, as in
260
+ * `String#startsWith`). Does not validate that the rest is a well-formed URL — only the scheme
261
+ * prefix. `blob:`, `data:`, and relative paths return `false`.
262
+ *
263
+ * @param url - String to test (often a user-provided or configured href).
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * isHttpUrl('https://example.com/path'); // true
268
+ * isHttpUrl('http://localhost:3000'); // true
269
+ * ```
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * isHttpUrl('//cdn.example.com'); // false — no explicit http(s) prefix
274
+ * isHttpUrl('/assets/logo.png'); // false — relative path
275
+ * ```
276
+ */
277
+ var isHttpUrl = (url) => {
278
+ return url.startsWith("https://") || url.startsWith("http://");
92
279
  };
93
- const isBase64Image = (str) => {
94
- const { mimeType, data } = decodeDataUrl(str);
95
- return !!(data && mimeType?.startsWith("image/"));
280
+ /**
281
+ * Returns `true` when `str` is a data URL that {@link decodeDataUrl} can parse **and** the MIME
282
+ * type starts with `image/` (e.g. `image/png`, `image/jpeg`). Requires both a non-empty Base64
283
+ * payload and an image MIME — arbitrary `data:text/plain;base64,...` is `false`.
284
+ *
285
+ * @param str - Candidate string (often `img.src` or API payload).
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * isBase64Image('data:image/png;base64,iVBORw0KGgo='); // true
290
+ * ```
291
+ *
292
+ * @example
293
+ * ```ts
294
+ * isBase64Image('https://example.com/x.png'); // false
295
+ * isBase64Image('data:text/plain;base64,SGk='); // false
296
+ * ```
297
+ */
298
+ var isBase64Image = (str) => {
299
+ const { mimeType, data } = decodeDataUrl(str);
300
+ return !!(data && mimeType?.startsWith("image/"));
96
301
  };
302
+ //#endregion
97
303
  exports.blobToBase64 = blobToBase64;
98
304
  exports.blobToUrl = blobToUrl;
99
305
  exports.decodeDataUrl = decodeDataUrl;
@@ -103,4 +309,5 @@ exports.isBase64Image = isBase64Image;
103
309
  exports.isHttpUrl = isHttpUrl;
104
310
  exports.renderImage = renderImage;
105
311
  exports.rotateImage = rotateImage;
106
- //# sourceMappingURL=media.cjs.map
312
+
313
+ //# sourceMappingURL=media.cjs.map
package/media.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"media.cjs","sources":["../src/media.ts"],"sourcesContent":["import { degToRad } from 'yummies/math';\n\nexport function blobToBase64(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onloadend = () => resolve(reader.result as string);\n reader.onerror = reject;\n reader.readAsDataURL(blob);\n });\n}\n\nexport const blobToUrl = (urlOrBlob: string | Blob) =>\n urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;\n\nexport const fileToBlob = (file: File) => {\n return new Blob([file], { type: file.type });\n};\n\nexport const imageToBlob = (\n imageElement: HTMLImageElement,\n mimeType: string = 'image/png',\n) => {\n const canvas = document.createElement('canvas');\n\n canvas.width = imageElement.naturalWidth || 300;\n canvas.height = imageElement.naturalHeight || 300;\n\n canvas.getContext('2d')!.drawImage(imageElement, 0, 0);\n\n const dataUri = canvas.toDataURL(mimeType, 1);\n const base64data = dataUri.split(',')[1];\n const base64MimeType = dataUri.split(';')[0].slice(5);\n\n const bytes = globalThis.atob(base64data);\n const buf = new ArrayBuffer(bytes.length);\n const array = new Uint8Array(buf);\n\n for (let index = 0; index < bytes.length; index++) {\n array[index] = bytes.charCodeAt(index);\n }\n\n const blob = new Blob([array], { type: base64MimeType });\n\n return blob;\n};\n\n/**\n * Loads and renders an image using `Image`.\n *\n * @returns {Promise<HTMLImageElement>}\n */\nexport const renderImage = (urlOrBlob: Blob | string) =>\n new Promise<HTMLImageElement>((resolve, reject) => {\n const image = new Image();\n image.src = blobToUrl(urlOrBlob);\n image.onload = () => resolve(image);\n image.onerror = () => reject();\n });\n\nfunction cropImageFromCanvas(context: CanvasRenderingContext2D) {\n const canvas = context.canvas;\n let w = canvas.width;\n let h = canvas.height;\n const pix: { x: number[]; y: number[] } = { x: [], y: [] };\n const imageData = context.getImageData(0, 0, canvas.width, canvas.height);\n let x: number;\n let y: number;\n let index: number;\n\n for (y = 0; y < h; y++) {\n for (x = 0; x < w; x++) {\n index = (y * w + x) * 4;\n if (imageData.data[index + 3] > 0) {\n pix.x.push(x);\n pix.y.push(y);\n }\n }\n }\n pix.x.sort((a, b) => a - b);\n pix.y.sort((a, b) => a - b);\n const n = pix.x.length - 1;\n\n w = 1 + pix.x[n] - pix.x[0];\n h = 1 + pix.y[n] - pix.y[0];\n const cut = context.getImageData(pix.x[0], pix.y[0], w, h);\n\n canvas.width = w;\n canvas.height = h;\n context.putImageData(cut, 0, 0);\n return canvas;\n}\n\n// TODO: ломает iphone с огромными изображениями\nexport const rotateImage = (image: HTMLImageElement, angle: number) => {\n const maxSize = Math.max(image.width, image.height);\n const canvas = document.createElement('canvas');\n canvas.width = maxSize;\n canvas.height = maxSize;\n const context = canvas.getContext('2d')!;\n context.save();\n context.translate(canvas.width / 2, canvas.height / 2);\n context.rotate(degToRad(angle));\n context.drawImage(image, -image.width / 2, -image.height / 2);\n context.restore();\n cropImageFromCanvas(context);\n return renderImage(canvas.toDataURL('image/png'));\n};\n\ninterface DecodedDataUrl {\n mimeType?: string;\n data?: string;\n}\n\n/*\n * Returning object which contains base64 data and mime type of passed data url string.\n * */\nexport function decodeDataUrl(url: string): DecodedDataUrl {\n const regex = /^data:(.*);base64,\\s?(.*)$/;\n const matches = new RegExp(regex).exec(url);\n\n return {\n mimeType: matches?.[1],\n data: matches?.[2],\n };\n}\n\nexport const isHttpUrl = (url: string): boolean => {\n return url.startsWith('https://') || url.startsWith('http://');\n};\n\nexport const isBase64Image = (str: string): boolean => {\n const { mimeType, data } = decodeDataUrl(str);\n return !!(data && mimeType?.startsWith('image/'));\n};\n"],"names":["degToRad"],"mappings":";;;AAEO,SAAS,aAAa,MAA6B;AACxD,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,IAAI,WAAA;AACnB,WAAO,YAAY,MAAM,QAAQ,OAAO,MAAgB;AACxD,WAAO,UAAU;AACjB,WAAO,cAAc,IAAI;AAAA,EAC3B,CAAC;AACH;AAEO,MAAM,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,YAAY,OAAO,UAAU,WAAW,CAAC;AAClD;AAUO,SAAS,cAAc,KAA6B;AACzD,QAAM,QAAQ;AACd,QAAM,UAAU,IAAI,OAAO,KAAK,EAAE,KAAK,GAAG;AAE1C,SAAO;AAAA,IACL,UAAU,UAAU,CAAC;AAAA,IACrB,MAAM,UAAU,CAAC;AAAA,EAAA;AAErB;AAEO,MAAM,YAAY,CAAC,QAAyB;AACjD,SAAO,IAAI,WAAW,UAAU,KAAK,IAAI,WAAW,SAAS;AAC/D;AAEO,MAAM,gBAAgB,CAAC,QAAyB;AACrD,QAAM,EAAE,UAAU,SAAS,cAAc,GAAG;AAC5C,SAAO,CAAC,EAAE,QAAQ,UAAU,WAAW,QAAQ;AACjD;;;;;;;;;;"}
1
+ {"version":3,"file":"media.cjs","names":[],"sources":["../src/media.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/media\n *\n * ## Description\n *\n * Binary and media helpers: Base64 data URLs, object URLs, image orientation fixes, and related\n * browser APIs wrapped in promises. Centralizes `FileReader`/`URL.createObjectURL` usage so image\n * pipelines and uploads stay consistent and easier to test or mock.\n *\n * ## Usage\n *\n * ```ts\n * import { blobToBase64 } from \"yummies/media\";\n * ```\n */\n\nimport { degToRad } from 'yummies/math';\n\n/**\n * Reads a {@link Blob} as a **data URL** string (`data:<mime>;base64,...`) using {@link FileReader#readAsDataURL}.\n *\n * Useful for previewing uploads, embedding small assets inline, or serializing binary for APIs that\n * expect Base64-in-JSON. The result includes the MIME prefix, not raw Base64 alone — use\n * {@link decodeDataUrl} if you need the payload and type separately.\n *\n * @param blob - Any `Blob` or `File` (files are blobs).\n * @returns Resolves to the data URL string; rejects if reading fails.\n *\n * @example\n * ```ts\n * const dataUrl = await blobToBase64(file);\n * previewImg.src = dataUrl;\n * ```\n *\n * @example\n * ```ts\n * const fromFetch = await fetch('/api/export').then((r) => r.blob());\n * const dataUrl = await blobToBase64(fromFetch);\n * ```\n */\nexport function blobToBase64(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onloadend = () => resolve(reader.result as string);\n reader.onerror = reject;\n reader.readAsDataURL(blob);\n });\n}\n\n/**\n * If `urlOrBlob` is already a string, returns it unchanged. If it is a {@link Blob}, returns\n * `URL.createObjectURL(blob)` — a short-lived `blob:` URL valid in this document until\n * {@link URL.revokeObjectURL} is called.\n *\n * Pair with {@link renderImage} or `<img src>` without re-fetching binary data. **Remember to\n * `revokeObjectURL`** when the URL is no longer needed to avoid retaining blob memory.\n *\n * @param urlOrBlob - Remote/http(s) URL string, data URL, or a `Blob` / `File`.\n * @returns A string suitable for `HTMLImageElement.src` or similar.\n *\n * @example\n * ```ts\n * const src = blobToUrl(uploadedFile);\n * img.src = src;\n * // when done:\n * URL.revokeObjectURL(src);\n * ```\n *\n * @example\n * ```ts\n * blobToUrl('https://cdn.example.com/logo.png'); // passed through as-is\n * ```\n */\nexport const blobToUrl = (urlOrBlob: string | Blob) =>\n urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;\n\n/**\n * Creates a new {@link Blob} from a {@link File}, copying the bytes and keeping `file.type` as\n * the blob’s MIME type. Handy when an API accepts `Blob` but you only have `File`, or you want a\n * plain blob without the `File` name/lastModified metadata.\n *\n * @param file - Source file from an `<input type=\"file\">` or drag-and-drop.\n * @returns A `Blob` with the same content and `type` as the file.\n *\n * @example\n * ```ts\n * const blob = fileToBlob(fileFromInput);\n * await uploadEndpoint(blob);\n * ```\n *\n * @example\n * ```ts\n * const blob = fileToBlob(imageFile);\n * const url = URL.createObjectURL(blob);\n * ```\n */\nexport const fileToBlob = (file: File) => {\n return new Blob([file], { type: file.type });\n};\n\n/**\n * Draws an {@link HTMLImageElement} onto an offscreen canvas, then builds a {@link Blob} from the\n * raster (via `canvas.toDataURL` + binary decode). Dimensions use `naturalWidth` / `naturalHeight`,\n * falling back to `300×300` if those are zero (e.g. not yet decoded).\n *\n * **CORS:** if the image is cross-origin without proper CORS headers, the canvas may be\n * *tainted* and `toDataURL` can throw — same browser rules as any canvas export.\n *\n * @param imageElement - Loaded image (`complete` / decoded recommended).\n * @param mimeType - Output MIME type, e.g. `'image/png'` (default) or `'image/jpeg'`. Encoder support is browser-dependent.\n * @returns Encoded image as a `Blob` with a matching `type` when possible.\n *\n * @example\n * ```ts\n * const img = await renderImage('/photo.jpg');\n * const jpegBlob = imageToBlob(img, 'image/jpeg');\n * ```\n *\n * @example\n * ```ts\n * const pngBlob = imageToBlob(cachedImg); // default image/png\n * ```\n */\nexport const imageToBlob = (\n imageElement: HTMLImageElement,\n mimeType: string = 'image/png',\n) => {\n const canvas = document.createElement('canvas');\n\n canvas.width = imageElement.naturalWidth || 300;\n canvas.height = imageElement.naturalHeight || 300;\n\n canvas.getContext('2d')!.drawImage(imageElement, 0, 0);\n\n const dataUri = canvas.toDataURL(mimeType, 1);\n const base64data = dataUri.split(',')[1];\n const base64MimeType = dataUri.split(';')[0].slice(5);\n\n const bytes = globalThis.atob(base64data);\n const buf = new ArrayBuffer(bytes.length);\n const array = new Uint8Array(buf);\n\n for (let index = 0; index < bytes.length; index++) {\n array[index] = bytes.charCodeAt(index);\n }\n\n const blob = new Blob([array], { type: base64MimeType });\n\n return blob;\n};\n\n/**\n * Loads a resource into a new `HTMLImageElement`: `src` is set via {@link blobToUrl} (so blobs get\n * object URLs, strings are used directly). Resolves on `load` with the same element; rejects on\n * `error` (e.g. bad URL, network failure, corrupt image) **with no rejection value**.\n *\n * Does not add the node to the DOM — use the returned element for canvas, measuring, or\n * {@link imageToBlob}.\n *\n * @param urlOrBlob - Remote URL, data URL, or `Blob` / `File`.\n * @returns Promise that fulfills with the loaded `HTMLImageElement`.\n *\n * @example\n * ```ts\n * const img = await renderImage('https://example.com/pic.png');\n * document.body.appendChild(img);\n * ```\n *\n * @example\n * ```ts\n * const img = await renderImage(pickedFile);\n * const blob = imageToBlob(img, 'image/webp');\n * ```\n */\nexport const renderImage = (urlOrBlob: Blob | string) =>\n new Promise<HTMLImageElement>((resolve, reject) => {\n const image = new Image();\n image.src = blobToUrl(urlOrBlob);\n image.onload = () => resolve(image);\n image.onerror = () => reject();\n });\n\nfunction cropImageFromCanvas(context: CanvasRenderingContext2D) {\n const canvas = context.canvas;\n let w = canvas.width;\n let h = canvas.height;\n const pix: { x: number[]; y: number[] } = { x: [], y: [] };\n const imageData = context.getImageData(0, 0, canvas.width, canvas.height);\n let x: number;\n let y: number;\n let index: number;\n\n for (y = 0; y < h; y++) {\n for (x = 0; x < w; x++) {\n index = (y * w + x) * 4;\n if (imageData.data[index + 3] > 0) {\n pix.x.push(x);\n pix.y.push(y);\n }\n }\n }\n pix.x.sort((a, b) => a - b);\n pix.y.sort((a, b) => a - b);\n const n = pix.x.length - 1;\n\n w = 1 + pix.x[n] - pix.x[0];\n h = 1 + pix.y[n] - pix.y[0];\n const cut = context.getImageData(pix.x[0], pix.y[0], w, h);\n\n canvas.width = w;\n canvas.height = h;\n context.putImageData(cut, 0, 0);\n return canvas;\n}\n\n// TODO: ломает iphone с огромными изображениями\n/**\n * Rotates `image` around its center on a square canvas (side = max(width, height)), then **crops**\n * transparent margins by trimming to the bounding box of pixels with non-zero alpha.\n * Returns a **new** loaded `HTMLImageElement` (PNG data URL under the hood via {@link renderImage}).\n *\n * `angle` is in **degrees**; converted with {@link degToRad} from `yummies/math`.\n *\n * Very large sources can stress memory on some mobile browsers (known iPhone issues — see TODO in source).\n *\n * @param image - Source image (should be decoded; uses `width` / `height`).\n * @param angle - Rotation in degrees (e.g. `90` for quarter turn).\n * @returns Promise of a new `HTMLImageElement` showing the rotated, cropped result.\n *\n * @example\n * ```ts\n * const upright = await rotateImage(landscapeImg, 90);\n * ```\n *\n * @example\n * ```ts\n * const fixed = await rotateImage(await renderImage(file), -15);\n * ```\n */\nexport const rotateImage = (image: HTMLImageElement, angle: number) => {\n const maxSize = Math.max(image.width, image.height);\n const canvas = document.createElement('canvas');\n canvas.width = maxSize;\n canvas.height = maxSize;\n const context = canvas.getContext('2d')!;\n context.save();\n context.translate(canvas.width / 2, canvas.height / 2);\n context.rotate(degToRad(angle));\n context.drawImage(image, -image.width / 2, -image.height / 2);\n context.restore();\n cropImageFromCanvas(context);\n return renderImage(canvas.toDataURL('image/png'));\n};\n\ninterface DecodedDataUrl {\n mimeType?: string;\n data?: string;\n}\n\n/**\n * Parses a `data:` URL of the form `data:<mime>;base64,<payload>` into its MIME type and raw\n * Base64 body (without the `data:...;base64,` prefix). Non-matching strings yield `undefined`\n * fields in the result object.\n *\n * @param url - Full data URL string.\n * @returns `{ mimeType, data }` — both optional when the regex does not match.\n *\n * @example\n * ```ts\n * const { mimeType, data } = decodeDataUrl('data:image/png;base64,iVBORw0KGgo=');\n * // mimeType === 'image/png', data === 'iVBORw0KGgo='\n * ```\n *\n * @example\n * ```ts\n * decodeDataUrl('not-a-data-url'); // { mimeType: undefined, data: undefined }\n * ```\n */\nexport function decodeDataUrl(url: string): DecodedDataUrl {\n const regex = /^data:(.*);base64,\\s?(.*)$/;\n const matches = new RegExp(regex).exec(url);\n\n return {\n mimeType: matches?.[1],\n data: matches?.[2],\n };\n}\n\n/**\n * Returns `true` if `url` starts with `https://` or `http://` (case-sensitive, as in\n * `String#startsWith`). Does not validate that the rest is a well-formed URL — only the scheme\n * prefix. `blob:`, `data:`, and relative paths return `false`.\n *\n * @param url - String to test (often a user-provided or configured href).\n *\n * @example\n * ```ts\n * isHttpUrl('https://example.com/path'); // true\n * isHttpUrl('http://localhost:3000'); // true\n * ```\n *\n * @example\n * ```ts\n * isHttpUrl('//cdn.example.com'); // false — no explicit http(s) prefix\n * isHttpUrl('/assets/logo.png'); // false — relative path\n * ```\n */\nexport const isHttpUrl = (url: string): boolean => {\n return url.startsWith('https://') || url.startsWith('http://');\n};\n\n/**\n * Returns `true` when `str` is a data URL that {@link decodeDataUrl} can parse **and** the MIME\n * type starts with `image/` (e.g. `image/png`, `image/jpeg`). Requires both a non-empty Base64\n * payload and an image MIME — arbitrary `data:text/plain;base64,...` is `false`.\n *\n * @param str - Candidate string (often `img.src` or API payload).\n *\n * @example\n * ```ts\n * isBase64Image('data:image/png;base64,iVBORw0KGgo='); // true\n * ```\n *\n * @example\n * ```ts\n * isBase64Image('https://example.com/x.png'); // false\n * isBase64Image('data:text/plain;base64,SGk='); // false\n * ```\n */\nexport const isBase64Image = (str: string): boolean => {\n const { mimeType, data } = decodeDataUrl(str);\n return !!(data && mimeType?.startsWith('image/'));\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,aAAa,MAA6B;AACxD,QAAO,IAAI,SAAS,SAAS,WAAW;EACtC,MAAM,SAAS,IAAI,YAAY;AAC/B,SAAO,kBAAkB,QAAQ,OAAO,OAAiB;AACzD,SAAO,UAAU;AACjB,SAAO,cAAc,KAAK;GAC1B;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BJ,IAAa,aAAa,cACxB,qBAAqB,OAAO,IAAI,gBAAgB,UAAU,GAAG;;;;;;;;;;;;;;;;;;;;;AAsB/D,IAAa,cAAc,SAAe;AACxC,QAAO,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AA0B9C,IAAa,eACX,cACA,WAAmB,gBAChB;CACH,MAAM,SAAS,SAAS,cAAc,SAAS;AAE/C,QAAO,QAAQ,aAAa,gBAAgB;AAC5C,QAAO,SAAS,aAAa,iBAAiB;AAE9C,QAAO,WAAW,KAAK,CAAE,UAAU,cAAc,GAAG,EAAE;CAEtD,MAAM,UAAU,OAAO,UAAU,UAAU,EAAE;CAC7C,MAAM,aAAa,QAAQ,MAAM,IAAI,CAAC;CACtC,MAAM,iBAAiB,QAAQ,MAAM,IAAI,CAAC,GAAG,MAAM,EAAE;CAErD,MAAM,QAAQ,WAAW,KAAK,WAAW;CACzC,MAAM,MAAM,IAAI,YAAY,MAAM,OAAO;CACzC,MAAM,QAAQ,IAAI,WAAW,IAAI;AAEjC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C/B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;AA2BT,IAAa,eAAe,OAAyB,UAAkB;CACrE,MAAM,UAAU,KAAK,IAAI,MAAM,OAAO,MAAM,OAAO;CACnD,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,QAAO,QAAQ;AACf,QAAO,SAAS;CAChB,MAAM,UAAU,OAAO,WAAW,KAAK;AACvC,SAAQ,MAAM;AACd,SAAQ,UAAU,OAAO,QAAQ,GAAG,OAAO,SAAS,EAAE;AACtD,SAAQ,QAAA,GAAA,aAAA,UAAgB,MAAM,CAAC;AAC/B,SAAQ,UAAU,OAAO,CAAC,MAAM,QAAQ,GAAG,CAAC,MAAM,SAAS,EAAE;AAC7D,SAAQ,SAAS;AACjB,qBAAoB,QAAQ;AAC5B,QAAO,YAAY,OAAO,UAAU,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;AA2BnD,SAAgB,cAAc,KAA6B;CAEzD,MAAM,2BAAU,IAAI,OADN,6BACmB,EAAC,KAAK,IAAI;AAE3C,QAAO;EACL,UAAU,UAAU;EACpB,MAAM,UAAU;EACjB;;;;;;;;;;;;;;;;;;;;;AAsBH,IAAa,aAAa,QAAyB;AACjD,QAAO,IAAI,WAAW,WAAW,IAAI,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;;AAqBhE,IAAa,iBAAiB,QAAyB;CACrD,MAAM,EAAE,UAAU,SAAS,cAAc,IAAI;AAC7C,QAAO,CAAC,EAAE,QAAQ,UAAU,WAAW,SAAS"}
package/media.d.ts CHANGED
@@ -1,20 +1,222 @@
1
+ /**
2
+ * ---header-docs-section---
3
+ * # yummies/media
4
+ *
5
+ * ## Description
6
+ *
7
+ * Binary and media helpers: Base64 data URLs, object URLs, image orientation fixes, and related
8
+ * browser APIs wrapped in promises. Centralizes `FileReader`/`URL.createObjectURL` usage so image
9
+ * pipelines and uploads stay consistent and easier to test or mock.
10
+ *
11
+ * ## Usage
12
+ *
13
+ * ```ts
14
+ * import { blobToBase64 } from "yummies/media";
15
+ * ```
16
+ */
17
+ /**
18
+ * Reads a {@link Blob} as a **data URL** string (`data:<mime>;base64,...`) using {@link FileReader#readAsDataURL}.
19
+ *
20
+ * Useful for previewing uploads, embedding small assets inline, or serializing binary for APIs that
21
+ * expect Base64-in-JSON. The result includes the MIME prefix, not raw Base64 alone — use
22
+ * {@link decodeDataUrl} if you need the payload and type separately.
23
+ *
24
+ * @param blob - Any `Blob` or `File` (files are blobs).
25
+ * @returns Resolves to the data URL string; rejects if reading fails.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const dataUrl = await blobToBase64(file);
30
+ * previewImg.src = dataUrl;
31
+ * ```
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const fromFetch = await fetch('/api/export').then((r) => r.blob());
36
+ * const dataUrl = await blobToBase64(fromFetch);
37
+ * ```
38
+ */
1
39
  declare function blobToBase64(blob: Blob): Promise<string>;
40
+ /**
41
+ * If `urlOrBlob` is already a string, returns it unchanged. If it is a {@link Blob}, returns
42
+ * `URL.createObjectURL(blob)` — a short-lived `blob:` URL valid in this document until
43
+ * {@link URL.revokeObjectURL} is called.
44
+ *
45
+ * Pair with {@link renderImage} or `<img src>` without re-fetching binary data. **Remember to
46
+ * `revokeObjectURL`** when the URL is no longer needed to avoid retaining blob memory.
47
+ *
48
+ * @param urlOrBlob - Remote/http(s) URL string, data URL, or a `Blob` / `File`.
49
+ * @returns A string suitable for `HTMLImageElement.src` or similar.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const src = blobToUrl(uploadedFile);
54
+ * img.src = src;
55
+ * // when done:
56
+ * URL.revokeObjectURL(src);
57
+ * ```
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * blobToUrl('https://cdn.example.com/logo.png'); // passed through as-is
62
+ * ```
63
+ */
2
64
  declare const blobToUrl: (urlOrBlob: string | Blob) => string;
65
+ /**
66
+ * Creates a new {@link Blob} from a {@link File}, copying the bytes and keeping `file.type` as
67
+ * the blob’s MIME type. Handy when an API accepts `Blob` but you only have `File`, or you want a
68
+ * plain blob without the `File` name/lastModified metadata.
69
+ *
70
+ * @param file - Source file from an `<input type="file">` or drag-and-drop.
71
+ * @returns A `Blob` with the same content and `type` as the file.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * const blob = fileToBlob(fileFromInput);
76
+ * await uploadEndpoint(blob);
77
+ * ```
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const blob = fileToBlob(imageFile);
82
+ * const url = URL.createObjectURL(blob);
83
+ * ```
84
+ */
3
85
  declare const fileToBlob: (file: File) => Blob;
86
+ /**
87
+ * Draws an {@link HTMLImageElement} onto an offscreen canvas, then builds a {@link Blob} from the
88
+ * raster (via `canvas.toDataURL` + binary decode). Dimensions use `naturalWidth` / `naturalHeight`,
89
+ * falling back to `300×300` if those are zero (e.g. not yet decoded).
90
+ *
91
+ * **CORS:** if the image is cross-origin without proper CORS headers, the canvas may be
92
+ * *tainted* and `toDataURL` can throw — same browser rules as any canvas export.
93
+ *
94
+ * @param imageElement - Loaded image (`complete` / decoded recommended).
95
+ * @param mimeType - Output MIME type, e.g. `'image/png'` (default) or `'image/jpeg'`. Encoder support is browser-dependent.
96
+ * @returns Encoded image as a `Blob` with a matching `type` when possible.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const img = await renderImage('/photo.jpg');
101
+ * const jpegBlob = imageToBlob(img, 'image/jpeg');
102
+ * ```
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * const pngBlob = imageToBlob(cachedImg); // default image/png
107
+ * ```
108
+ */
4
109
  declare const imageToBlob: (imageElement: HTMLImageElement, mimeType?: string) => Blob;
5
110
  /**
6
- * Loads and renders an image using `Image`.
111
+ * Loads a resource into a new `HTMLImageElement`: `src` is set via {@link blobToUrl} (so blobs get
112
+ * object URLs, strings are used directly). Resolves on `load` with the same element; rejects on
113
+ * `error` (e.g. bad URL, network failure, corrupt image) **with no rejection value**.
114
+ *
115
+ * Does not add the node to the DOM — use the returned element for canvas, measuring, or
116
+ * {@link imageToBlob}.
117
+ *
118
+ * @param urlOrBlob - Remote URL, data URL, or `Blob` / `File`.
119
+ * @returns Promise that fulfills with the loaded `HTMLImageElement`.
7
120
  *
8
- * @returns {Promise<HTMLImageElement>}
121
+ * @example
122
+ * ```ts
123
+ * const img = await renderImage('https://example.com/pic.png');
124
+ * document.body.appendChild(img);
125
+ * ```
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const img = await renderImage(pickedFile);
130
+ * const blob = imageToBlob(img, 'image/webp');
131
+ * ```
9
132
  */
10
133
  declare const renderImage: (urlOrBlob: Blob | string) => Promise<HTMLImageElement>;
134
+ /**
135
+ * Rotates `image` around its center on a square canvas (side = max(width, height)), then **crops**
136
+ * transparent margins by trimming to the bounding box of pixels with non-zero alpha.
137
+ * Returns a **new** loaded `HTMLImageElement` (PNG data URL under the hood via {@link renderImage}).
138
+ *
139
+ * `angle` is in **degrees**; converted with {@link degToRad} from `yummies/math`.
140
+ *
141
+ * Very large sources can stress memory on some mobile browsers (known iPhone issues — see TODO in source).
142
+ *
143
+ * @param image - Source image (should be decoded; uses `width` / `height`).
144
+ * @param angle - Rotation in degrees (e.g. `90` for quarter turn).
145
+ * @returns Promise of a new `HTMLImageElement` showing the rotated, cropped result.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const upright = await rotateImage(landscapeImg, 90);
150
+ * ```
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const fixed = await rotateImage(await renderImage(file), -15);
155
+ * ```
156
+ */
11
157
  declare const rotateImage: (image: HTMLImageElement, angle: number) => Promise<HTMLImageElement>;
12
158
  interface DecodedDataUrl {
13
159
  mimeType?: string;
14
160
  data?: string;
15
161
  }
162
+ /**
163
+ * Parses a `data:` URL of the form `data:<mime>;base64,<payload>` into its MIME type and raw
164
+ * Base64 body (without the `data:...;base64,` prefix). Non-matching strings yield `undefined`
165
+ * fields in the result object.
166
+ *
167
+ * @param url - Full data URL string.
168
+ * @returns `{ mimeType, data }` — both optional when the regex does not match.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * const { mimeType, data } = decodeDataUrl('data:image/png;base64,iVBORw0KGgo=');
173
+ * // mimeType === 'image/png', data === 'iVBORw0KGgo='
174
+ * ```
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * decodeDataUrl('not-a-data-url'); // { mimeType: undefined, data: undefined }
179
+ * ```
180
+ */
16
181
  declare function decodeDataUrl(url: string): DecodedDataUrl;
182
+ /**
183
+ * Returns `true` if `url` starts with `https://` or `http://` (case-sensitive, as in
184
+ * `String#startsWith`). Does not validate that the rest is a well-formed URL — only the scheme
185
+ * prefix. `blob:`, `data:`, and relative paths return `false`.
186
+ *
187
+ * @param url - String to test (often a user-provided or configured href).
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * isHttpUrl('https://example.com/path'); // true
192
+ * isHttpUrl('http://localhost:3000'); // true
193
+ * ```
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * isHttpUrl('//cdn.example.com'); // false — no explicit http(s) prefix
198
+ * isHttpUrl('/assets/logo.png'); // false — relative path
199
+ * ```
200
+ */
17
201
  declare const isHttpUrl: (url: string) => boolean;
202
+ /**
203
+ * Returns `true` when `str` is a data URL that {@link decodeDataUrl} can parse **and** the MIME
204
+ * type starts with `image/` (e.g. `image/png`, `image/jpeg`). Requires both a non-empty Base64
205
+ * payload and an image MIME — arbitrary `data:text/plain;base64,...` is `false`.
206
+ *
207
+ * @param str - Candidate string (often `img.src` or API payload).
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * isBase64Image('data:image/png;base64,iVBORw0KGgo='); // true
212
+ * ```
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * isBase64Image('https://example.com/x.png'); // false
217
+ * isBase64Image('data:text/plain;base64,SGk='); // false
218
+ * ```
219
+ */
18
220
  declare const isBase64Image: (str: string) => boolean;
19
221
 
20
222
  export { blobToBase64, blobToUrl, decodeDataUrl, fileToBlob, imageToBlob, isBase64Image, isHttpUrl, renderImage, rotateImage };