@etsoo/shared 1.1.20 → 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|
@@ -183,23 +183,23 @@ export declare namespace DataTypes {
183
183
  /**
184
184
  * Culture definiton
185
185
  */
186
- type CultureDefinition = Readonly<{
186
+ type CultureDefinition<T extends {} = StringRecord> = Readonly<{
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<StringRecord>;
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
@@ -54,17 +60,16 @@ export declare namespace DomUtils {
54
60
  * @param items Available cultures
55
61
  * @param culture Detected culture
56
62
  */
57
- const getCulture: (items: DataTypes.CultureDefinition[], culture: string) => Readonly<{
58
- readonly name: string; /**
59
- * Cast data to target type
60
- * @param source Source data
61
- * @param template Template for generation
62
- * @param keepSource Means even the template does not include the definition, still keep the item
63
- * @returns Result
64
- */
65
- readonly label: string;
66
- readonly resources: Readonly<DataTypes.StringRecord>;
67
- readonly compatibleName?: string[] | undefined;
63
+ const getCulture: <T extends {}>(items: Readonly<{
64
+ name: string;
65
+ label: string;
66
+ resources: T;
67
+ compatibleName?: string[] | undefined;
68
+ }>[], culture: string) => Readonly<{
69
+ name: string;
70
+ label: string;
71
+ resources: T;
72
+ compatibleName?: string[] | undefined;
68
73
  }> | undefined;
69
74
  /**
70
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
@@ -183,23 +183,23 @@ export declare namespace DataTypes {
183
183
  /**
184
184
  * Culture definiton
185
185
  */
186
- type CultureDefinition = Readonly<{
186
+ type CultureDefinition<T extends {} = StringRecord> = Readonly<{
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<StringRecord>;
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
@@ -54,17 +60,16 @@ export declare namespace DomUtils {
54
60
  * @param items Available cultures
55
61
  * @param culture Detected culture
56
62
  */
57
- const getCulture: (items: DataTypes.CultureDefinition[], culture: string) => Readonly<{
58
- readonly name: string; /**
59
- * Cast data to target type
60
- * @param source Source data
61
- * @param template Template for generation
62
- * @param keepSource Means even the template does not include the definition, still keep the item
63
- * @returns Result
64
- */
65
- readonly label: string;
66
- readonly resources: Readonly<DataTypes.StringRecord>;
67
- readonly compatibleName?: string[] | undefined;
63
+ const getCulture: <T extends {}>(items: Readonly<{
64
+ name: string;
65
+ label: string;
66
+ resources: T;
67
+ compatibleName?: string[] | undefined;
68
+ }>[], culture: string) => Readonly<{
69
+ name: string;
70
+ label: string;
71
+ resources: T;
72
+ compatibleName?: string[] | undefined;
68
73
  }> | undefined;
69
74
  /**
70
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.20",
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.17.0",
59
- "@typescript-eslint/parser": "^5.17.0",
60
- "eslint": "^8.12.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.21.0",
59
+ "@typescript-eslint/parser": "^5.21.0",
60
+ "eslint": "^8.14.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
- "eslint-plugin-import": "^2.25.4",
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
@@ -237,26 +237,26 @@ export namespace DataTypes {
237
237
  /**
238
238
  * Culture definiton
239
239
  */
240
- export type CultureDefinition = Readonly<{
240
+ export type CultureDefinition<T extends {} = StringRecord> = Readonly<{
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<StringRecord>;
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
@@ -281,8 +303,8 @@ export namespace DomUtils {
281
303
  * @param items Available cultures
282
304
  * @param culture Detected culture
283
305
  */
284
- export const getCulture = (
285
- items: DataTypes.CultureDefinition[],
306
+ export const getCulture = <T extends {}>(
307
+ items: DataTypes.CultureDefinition<T>[],
286
308
  culture: string
287
309
  ) => {
288
310
  if (items.length === 0) {