@etsoo/shared 1.1.21 → 1.1.24

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
+ });
@@ -187,19 +187,19 @@ export declare namespace DataTypes {
187
187
  /**
188
188
  * Name, like zh-CN
189
189
  */
190
- readonly name: string;
190
+ name: string;
191
191
  /**
192
192
  * Label for description, like Simplifined Chinese
193
193
  */
194
- readonly label: string;
194
+ label: string;
195
195
  /**
196
196
  * Resources
197
197
  */
198
- readonly resources: Readonly<T>;
198
+ resources: T;
199
199
  /**
200
200
  * Compatible names
201
201
  */
202
- readonly compatibleName?: string[];
202
+ compatibleName?: string[];
203
203
  }>;
204
204
  /**
205
205
  * Convert value to target type
@@ -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
@@ -55,15 +61,15 @@ export declare namespace DomUtils {
55
61
  * @param culture Detected culture
56
62
  */
57
63
  const getCulture: <T extends {}>(items: Readonly<{
58
- readonly name: string;
59
- readonly label: string;
60
- readonly resources: Readonly<T>;
61
- readonly compatibleName?: string[] | undefined;
64
+ name: string;
65
+ label: string;
66
+ resources: T;
67
+ compatibleName?: string[] | undefined;
62
68
  }>[], culture: string) => Readonly<{
63
- readonly name: string;
64
- readonly label: string;
65
- readonly resources: Readonly<T>;
66
- readonly compatibleName?: string[] | undefined;
69
+ name: string;
70
+ label: string;
71
+ resources: T;
72
+ compatibleName?: string[] | undefined;
67
73
  }> | undefined;
68
74
  /**
69
75
  * Get an unique key combined with current URL
@@ -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,12 @@
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 < 1)
6
+ precision = 4;
7
+ const p = Math.pow(10, precision !== null && precision !== void 0 ? precision : 4);
8
+ return Math.round(this * p) / p;
9
+ };
4
10
  var NumberUtils;
5
11
  (function (NumberUtils) {
6
12
  /**
@@ -187,19 +187,19 @@ export declare namespace DataTypes {
187
187
  /**
188
188
  * Name, like zh-CN
189
189
  */
190
- readonly name: string;
190
+ name: string;
191
191
  /**
192
192
  * Label for description, like Simplifined Chinese
193
193
  */
194
- readonly label: string;
194
+ label: string;
195
195
  /**
196
196
  * Resources
197
197
  */
198
- readonly resources: Readonly<T>;
198
+ resources: T;
199
199
  /**
200
200
  * Compatible names
201
201
  */
202
- readonly compatibleName?: string[];
202
+ compatibleName?: string[];
203
203
  }>;
204
204
  /**
205
205
  * Convert value to target type
@@ -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
@@ -55,15 +61,15 @@ export declare namespace DomUtils {
55
61
  * @param culture Detected culture
56
62
  */
57
63
  const getCulture: <T extends {}>(items: Readonly<{
58
- readonly name: string;
59
- readonly label: string;
60
- readonly resources: Readonly<T>;
61
- readonly compatibleName?: string[] | undefined;
64
+ name: string;
65
+ label: string;
66
+ resources: T;
67
+ compatibleName?: string[] | undefined;
62
68
  }>[], culture: string) => Readonly<{
63
- readonly name: string;
64
- readonly label: string;
65
- readonly resources: Readonly<T>;
66
- readonly compatibleName?: string[] | undefined;
69
+ name: string;
70
+ label: string;
71
+ resources: T;
72
+ compatibleName?: string[] | undefined;
67
73
  }> | undefined;
68
74
  /**
69
75
  * Get an unique key combined with current URL
@@ -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,9 @@
1
+ Number.prototype.toExact = function (precision) {
2
+ if (precision == null || precision < 1)
3
+ precision = 4;
4
+ const p = Math.pow(10, precision !== null && precision !== void 0 ? precision : 4);
5
+ return Math.round(this * p) / p;
6
+ };
1
7
  export var NumberUtils;
2
8
  (function (NumberUtils) {
3
9
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.21",
3
+ "version": "1.1.24",
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/DataTypes.ts CHANGED
@@ -241,22 +241,22 @@ export namespace DataTypes {
241
241
  /**
242
242
  * Name, like zh-CN
243
243
  */
244
- readonly name: string;
244
+ name: string;
245
245
 
246
246
  /**
247
247
  * Label for description, like Simplifined Chinese
248
248
  */
249
- readonly label: string;
249
+ label: string;
250
250
 
251
251
  /**
252
252
  * Resources
253
253
  */
254
- readonly resources: Readonly<T>;
254
+ resources: T;
255
255
 
256
256
  /**
257
257
  * Compatible names
258
258
  */
259
- readonly compatibleName?: string[];
259
+ compatibleName?: string[];
260
260
  }>;
261
261
 
262
262
  /**
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,19 @@
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 < 1) precision = 4;
13
+ const p = Math.pow(10, precision ?? 4);
14
+ return Math.round(this * p) / p;
15
+ };
16
+
1
17
  export namespace NumberUtils {
2
18
  /**
3
19
  * Format number