@etsoo/shared 1.1.22 → 1.1.25

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|
@@ -151,6 +152,7 @@ Numbers related utilities
151
152
  |format|Format number|
152
153
  |formatMoney|Format money number|
153
154
  |parse|Parse float value|
155
+ |toExact|To the exact precision number avoiding precision lost|
154
156
 
155
157
  ## StorageUtils
156
158
  Storage related utilities
@@ -23,3 +23,10 @@ test('Tests for parseWithUnit', () => {
23
23
  expect(NumberUtils.parseWithUnit('16')).toStrictEqual([16, '']);
24
24
  expect(NumberUtils.parseWithUnit('a16')).toBeUndefined();
25
25
  });
26
+
27
+ test('Tests for toExact', () => {
28
+ // 0.7000000000000001
29
+ const result = 0.8 - 0.1;
30
+ expect(result).not.toBe(0.7);
31
+ expect(result.toExact()).toBe(0.7);
32
+ });
@@ -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
@@ -1,3 +1,12 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberUtils = void 0;
4
+ Number.prototype.toExact = function (precision) {
5
+ if (precision == null || precision < 0)
6
+ precision = 2;
7
+ if (precision === 0)
8
+ return Math.round(this);
9
+ const p = Math.pow(10, precision);
10
+ return Math.round(this * p) / p;
11
+ };
4
12
  var NumberUtils;
5
13
  (function (NumberUtils) {
6
14
  /**
@@ -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
@@ -1,3 +1,12 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -1,3 +1,11 @@
1
+ Number.prototype.toExact = function (precision) {
2
+ if (precision == null || precision < 0)
3
+ precision = 2;
4
+ if (precision === 0)
5
+ return Math.round(this);
6
+ const p = Math.pow(10, precision);
7
+ return Math.round(this * p) / p;
8
+ };
1
9
  export var NumberUtils;
2
10
  (function (NumberUtils) {
3
11
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.22",
3
+ "version": "1.1.25",
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
@@ -1,3 +1,22 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
10
+
11
+ Number.prototype.toExact = function (this: number, precision?: number) {
12
+ if (precision == null || precision < 0) precision = 2;
13
+
14
+ if (precision === 0) return Math.round(this);
15
+
16
+ const p = Math.pow(10, precision);
17
+ return Math.round(this * p) / p;
18
+ };
19
+
1
20
  export namespace NumberUtils {
2
21
  /**
3
22
  * Format number