@etsoo/shared 1.1.22 → 1.1.23

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.
package/README.md CHANGED
@@ -122,6 +122,7 @@ DOM/window related utilities
122
122
  |detectedCountry|Current detected country|
123
123
  |detectedCulture|Current detected culture|
124
124
  |dimensionEqual|Check two rectangles equality|
125
+ |fileToDataURL|File to data URL|
125
126
  |formDataToObject|Form data to object|
126
127
  |getCulture|Get the available culture definition|
127
128
  |getDataChanges|Get data changed fields with input data updated|
@@ -43,6 +43,12 @@ export declare namespace DomUtils {
43
43
  * @param d2 Dimension 2
44
44
  */
45
45
  function dimensionEqual(d1?: DOMRect, d2?: DOMRect): boolean;
46
+ /**
47
+ * File to data URL
48
+ * @param file File
49
+ * @returns Data URL
50
+ */
51
+ function fileToDataURL(file: File): Promise<string>;
46
52
  /**
47
53
  * Form data to object
48
54
  * @param form Form data
@@ -213,6 +213,27 @@ var DomUtils;
213
213
  return false;
214
214
  }
215
215
  DomUtils.dimensionEqual = dimensionEqual;
216
+ /**
217
+ * File to data URL
218
+ * @param file File
219
+ * @returns Data URL
220
+ */
221
+ async function fileToDataURL(file) {
222
+ return new Promise((resolve, reject) => {
223
+ const reader = new FileReader();
224
+ reader.onerror = reject;
225
+ reader.onload = () => {
226
+ const data = reader.result;
227
+ if (data == null) {
228
+ reject();
229
+ return;
230
+ }
231
+ resolve(data);
232
+ };
233
+ reader.readAsDataURL(file);
234
+ });
235
+ }
236
+ DomUtils.fileToDataURL = fileToDataURL;
216
237
  /**
217
238
  * Form data to object
218
239
  * @param form Form data
@@ -43,6 +43,12 @@ export declare namespace DomUtils {
43
43
  * @param d2 Dimension 2
44
44
  */
45
45
  function dimensionEqual(d1?: DOMRect, d2?: DOMRect): boolean;
46
+ /**
47
+ * File to data URL
48
+ * @param file File
49
+ * @returns Data URL
50
+ */
51
+ function fileToDataURL(file: File): Promise<string>;
46
52
  /**
47
53
  * Form data to object
48
54
  * @param form Form data
@@ -210,6 +210,27 @@ export var DomUtils;
210
210
  return false;
211
211
  }
212
212
  DomUtils.dimensionEqual = dimensionEqual;
213
+ /**
214
+ * File to data URL
215
+ * @param file File
216
+ * @returns Data URL
217
+ */
218
+ async function fileToDataURL(file) {
219
+ return new Promise((resolve, reject) => {
220
+ const reader = new FileReader();
221
+ reader.onerror = reject;
222
+ reader.onload = () => {
223
+ const data = reader.result;
224
+ if (data == null) {
225
+ reject();
226
+ return;
227
+ }
228
+ resolve(data);
229
+ };
230
+ reader.readAsDataURL(file);
231
+ });
232
+ }
233
+ DomUtils.fileToDataURL = fileToDataURL;
213
234
  /**
214
235
  * Form data to object
215
236
  * @param form Form data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.22",
3
+ "version": "1.1.23",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,13 +55,13 @@
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.4.1",
58
- "@typescript-eslint/eslint-plugin": "^5.20.0",
59
- "@typescript-eslint/parser": "^5.20.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.21.0",
59
+ "@typescript-eslint/parser": "^5.21.0",
60
60
  "eslint": "^8.14.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.26.0",
63
63
  "jest": "^27.5.1",
64
64
  "ts-jest": "^27.1.4",
65
- "typescript": "^4.6.3"
65
+ "typescript": "^4.6.4"
66
66
  }
67
67
  }
package/src/DomUtils.ts CHANGED
@@ -261,6 +261,28 @@ export namespace DomUtils {
261
261
  return false;
262
262
  }
263
263
 
264
+ /**
265
+ * File to data URL
266
+ * @param file File
267
+ * @returns Data URL
268
+ */
269
+ export async function fileToDataURL(file: File) {
270
+ return new Promise<string>((resolve, reject) => {
271
+ const reader = new FileReader();
272
+ reader.onerror = reject;
273
+ reader.onload = () => {
274
+ const data = reader.result;
275
+ if (data == null) {
276
+ reject();
277
+ return;
278
+ }
279
+
280
+ resolve(data as string);
281
+ };
282
+ reader.readAsDataURL(file);
283
+ });
284
+ }
285
+
264
286
  /**
265
287
  * Form data to object
266
288
  * @param form Form data