@etsoo/shared 1.1.45 → 1.1.48

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
@@ -88,6 +88,8 @@ Data type definitions and type safe functions
88
88
  |BasicTemplateType|Basic template type|
89
89
  |CombinedEnum|Combined type enum|
90
90
  |CultureDefinition|Culture definition|
91
+ |DI|Dynamic interface with multiple properties|
92
+ |DIS|Dynamic interface with single property|
91
93
  |EnumBase|Enum base type|
92
94
  |EnumValue|Enum value type|
93
95
  |ExtendedEnum|Extended type enum|
@@ -97,8 +99,10 @@ Data type definitions and type safe functions
97
99
  |IdType|Number and string combination id type|
98
100
  |IdItem|Item with id or id generator|
99
101
  |IdLabelItem|Item with id and label|
102
+ |IdLabelType|Item with id and label dynamic type|
100
103
  |KeyCollection|Key collection, like { key1: {}, key2: {} }|
101
104
  |Keys|Get specific type keys|
105
+ |ObjType|Generic object type|
102
106
  |Simple|Basic or basic array type|
103
107
  |SimpleEnum|Simple type enum|
104
108
  |SimpleNames|Simple type names|
@@ -118,6 +122,7 @@ Data type definitions and type safe functions
118
122
  |getEnumKey|Get enum string literal type value|
119
123
  |getEnumKeys|Get Enum keys|
120
124
  |getIdValue|Get object id field value|
125
+ |getIdValue1|Get object id field value 1|
121
126
  |getResult|Get input function or value result|
122
127
  |getStringValue|Get object string field value|
123
128
  |getValue|Get object field value|
@@ -1,5 +1,22 @@
1
1
  import { DataTypes } from '../src/DataTypes';
2
2
 
3
+ test('Tests for DI', () => {
4
+ const item: DataTypes.DIS<'id', number> & DataTypes.DIS<'label', string> = {
5
+ id: 1,
6
+ label: 'Etsoo'
7
+ };
8
+ expect(item.id).toBe(1);
9
+ });
10
+
11
+ test('Tests for IdLabelType', () => {
12
+ const item: DataTypes.IdLabelItem = {
13
+ id: 1,
14
+ label: 'Etsoo'
15
+ };
16
+ const itemCopy: DataTypes.IdLabelType<'id', 'label'> = { ...item };
17
+ expect(item).toEqual(itemCopy);
18
+ });
19
+
3
20
  test('Tests for convert', () => {
4
21
  expect(DataTypes.convert('5', 0)).toStrictEqual(5);
5
22
  expect(
@@ -92,8 +109,8 @@ test('Tests for IdLabelItem', () => {
92
109
  test('Tests for getValue', () => {
93
110
  const data = { id: 1, flag: true };
94
111
  expect(DataTypes.getValue(data, 'id')).toBe(1);
95
- expect(DataTypes.getValue(data, 'flag')).toBe('true');
96
- expect(DataTypes.getValue(data, 'unknown')).toBeNull();
112
+ expect(DataTypes.getValue(data, 'flag')).toBeTruthy();
113
+ expect(DataTypes.getValue(data, 'unknown')).toBeUndefined();
97
114
  });
98
115
 
99
116
  test('Tests for getIdValue', () => {
@@ -143,9 +143,9 @@ test('Tests for objectEqual', () => {
143
143
 
144
144
  test('Tests for objectUpdated', () => {
145
145
  const objPrev = { a: 1, b: 'abc', c: true, d: null, f: [1, 2] };
146
- const objNew = { a: 2, b: 'abc', d: new Date(), f: [1, 2, 3] };
146
+ const objNew = { a: 2, b: 'abc', d: new Date(), f: [1, 2, 3], g: true };
147
147
  const fields = Utils.objectUpdated(objNew, objPrev, ['d']);
148
- expect(fields.sort()).toStrictEqual(['a', 'c', 'f']);
148
+ expect(fields.sort()).toStrictEqual(['a', 'c', 'f', 'g']);
149
149
  });
150
150
 
151
151
  test('Tests for parseString', () => {
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Generic object type
3
+ * Narrow case, uses StringRecord
4
+ * Before was wrong with {}, from 4.8 unknown = {} | null | undefined
5
+ */
1
6
  /**
2
7
  * Interface data types
3
8
  */
@@ -123,8 +128,8 @@ export declare namespace DataTypes {
123
128
  /**
124
129
  * Key collection, like { key1: {}, key2: {} }
125
130
  */
126
- type KeyCollection<K extends readonly string[], I extends {}> = {
127
- [index in K[number]]: I;
131
+ type KeyCollection<K extends readonly string[], I extends object> = {
132
+ [P in K[number]]: I;
128
133
  };
129
134
  /**
130
135
  * Enum value type
@@ -168,6 +173,10 @@ export declare namespace DataTypes {
168
173
  */
169
174
  label: string;
170
175
  };
176
+ /**
177
+ * Item with id and label dynamic type
178
+ */
179
+ type IdLabelType<I extends string, L extends string, D extends IdType = number> = DIS<I, D> & DIS<L, string>;
171
180
  /**
172
181
  * Get specific type keys
173
182
  */
@@ -177,7 +186,7 @@ export declare namespace DataTypes {
177
186
  /**
178
187
  * Culture definiton
179
188
  */
180
- type CultureDefinition<T extends {} = StringRecord> = Readonly<{
189
+ type CultureDefinition<T extends StringRecord = StringRecord> = Readonly<{
181
190
  /**
182
191
  * Name, like zh-CN
183
192
  */
@@ -195,6 +204,18 @@ export declare namespace DataTypes {
195
204
  */
196
205
  compatibleName?: string[];
197
206
  }>;
207
+ /**
208
+ * Dynamic interface with multiple properties
209
+ */
210
+ type DI<K extends readonly string[], T> = {
211
+ [P in K[number]]: T;
212
+ };
213
+ /**
214
+ * Dynamic interface with single property
215
+ */
216
+ type DIS<K extends string, T> = {
217
+ [P in K]: T;
218
+ };
198
219
  /**
199
220
  * Convert value to target type
200
221
  * @param input Input value
@@ -262,21 +283,28 @@ export declare namespace DataTypes {
262
283
  * @param key Property name
263
284
  * @returns Value
264
285
  */
265
- function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
286
+ function getValue<T extends object, K extends keyof T | string>(data: T | undefined | null, key: K): K extends keyof T ? T[K] : undefined;
266
287
  /**
267
288
  * Get object id field value
268
289
  * @param data Data
269
290
  * @param key Property name
270
291
  * @returns Id value
271
292
  */
272
- function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
293
+ function getIdValue<T extends object, K extends Keys<T, string | number>>(data: T, key: K): T[K];
294
+ /**
295
+ * Get object id field value 1
296
+ * @param data Data
297
+ * @param key Property name
298
+ * @returns Id value
299
+ */
300
+ function getIdValue1<T extends object, K extends keyof T | string>(data: T | undefined | null, key: K): K extends keyof T ? (T[K] extends number ? number : string) : undefined;
273
301
  /**
274
302
  * Get object string field value
275
303
  * @param data Data
276
304
  * @param key Property name
277
305
  * @returns String value
278
306
  */
279
- function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
307
+ function getStringValue<T extends object>(data: T | undefined | null, key: keyof T | string): string | undefined;
280
308
  /**
281
309
  * Check the type is a basic type or not (type guard)
282
310
  * @param name Type name
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
+ /**
3
+ * Generic object type
4
+ * Narrow case, uses StringRecord
5
+ * Before was wrong with {}, from 4.8 unknown = {} | null | undefined
6
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
8
  exports.DataTypes = void 0;
4
9
  /**
@@ -290,14 +295,10 @@ var DataTypes;
290
295
  * @returns Value
291
296
  */
292
297
  function getValue(data, key) {
293
- if (data == null)
294
- return null;
295
- const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
296
- if (value == null)
297
- return null;
298
- if (typeof value === 'number')
299
- return value;
300
- return convert(value, 'string');
298
+ if (data != null && typeof key === 'string' && key in data) {
299
+ return Reflect.get(data, key);
300
+ }
301
+ return undefined;
301
302
  }
302
303
  DataTypes.getValue = getValue;
303
304
  /**
@@ -310,6 +311,21 @@ var DataTypes;
310
311
  return data[key];
311
312
  }
312
313
  DataTypes.getIdValue = getIdValue;
314
+ /**
315
+ * Get object id field value 1
316
+ * @param data Data
317
+ * @param key Property name
318
+ * @returns Id value
319
+ */
320
+ function getIdValue1(data, key) {
321
+ const value = getValue(data, key);
322
+ if (value == null)
323
+ return undefined;
324
+ if (typeof value === 'number')
325
+ return value;
326
+ return `${value}`;
327
+ }
328
+ DataTypes.getIdValue1 = getIdValue1;
313
329
  /**
314
330
  * Get object string field value
315
331
  * @param data Data
@@ -318,9 +334,11 @@ var DataTypes;
318
334
  */
319
335
  function getStringValue(data, key) {
320
336
  const value = getValue(data, key);
321
- if (typeof value === 'number')
322
- return value.toString();
323
- return value;
337
+ if (value == null)
338
+ return undefined;
339
+ if (typeof value === 'string')
340
+ return value;
341
+ return `${value}`;
324
342
  }
325
343
  DataTypes.getStringValue = getStringValue;
326
344
  /**
@@ -19,7 +19,7 @@ export declare namespace DomUtils {
19
19
  * @param source Source data to match
20
20
  * @param keepFields Fields need to be kept
21
21
  */
22
- function clearFormData(data: IFormData, source?: {}, keepFields?: string[]): IFormData;
22
+ function clearFormData(data: IFormData, source?: object, keepFields?: string[]): IFormData;
23
23
  /**
24
24
  * Cast data as template format
25
25
  * @param source Source data
@@ -35,7 +35,7 @@ export declare namespace DomUtils {
35
35
  * @param keepSource Means even the template does not include the definition, still keep the item
36
36
  * @returns Result
37
37
  */
38
- function dataValueAs<T extends {}>(source: IFormData | {}, templateValue: T, keepSource?: boolean): Partial<T>;
38
+ function dataValueAs<T extends object>(source: IFormData | object, templateValue: T, keepSource?: boolean): Partial<T>;
39
39
  /**
40
40
  * Current detected country
41
41
  */
@@ -67,7 +67,7 @@ export declare namespace DomUtils {
67
67
  * @param items Available cultures
68
68
  * @param culture Detected culture
69
69
  */
70
- const getCulture: <T extends {}>(items: Readonly<{
70
+ const getCulture: <T extends DataTypes.StringRecord>(items: Readonly<{
71
71
  name: string;
72
72
  label: string;
73
73
  resources: T;
@@ -117,5 +117,5 @@ export declare namespace DomUtils {
117
117
  * @param name Element name or first collection item
118
118
  * @param container Container, limits the element range
119
119
  */
120
- function setFocus(name: string | {}, container?: HTMLElement): void;
120
+ function setFocus(name: string | object, container?: HTMLElement): void;
121
121
  }
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get local storage object data
31
31
  * @param key Key name
32
32
  */
33
- function getLocalObject<T extends {}>(key: string): T | undefined;
33
+ function getLocalObject<T extends object>(key: string): T | undefined;
34
34
  /**
35
35
  * Get session storage data
36
36
  * @param key Key name
@@ -46,5 +46,5 @@ export declare namespace StorageUtils {
46
46
  * Get session storage object data
47
47
  * @param key Key name
48
48
  */
49
- function getSessionObject<T extends {}>(key: string): T | undefined;
49
+ function getSessionObject<T extends object>(key: string): T | undefined;
50
50
  }
@@ -53,7 +53,7 @@ export declare namespace Utils {
53
53
  * @param labelField Label field, default is label
54
54
  * @param blankLabel Blank label, default is ---
55
55
  */
56
- function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
56
+ function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
57
57
  /**
58
58
  * Base64 chars to number
59
59
  * @param base64Chars Base64 chars
@@ -65,7 +65,7 @@ export declare namespace Utils {
65
65
  * @param input Input object
66
66
  * @param fields Fields to correct
67
67
  */
68
- function correctTypes<T extends {}, F extends {
68
+ function correctTypes<T extends object, F extends {
69
69
  [P in keyof T]: DataTypes.BasicNames;
70
70
  }>(input: T, fields: F): void;
71
71
  /**
@@ -95,7 +95,7 @@ export declare namespace Utils {
95
95
  * @param ignoreFields Ignore fields
96
96
  * @returns
97
97
  */
98
- function getDataChanges(input: {}, initData: {}, ignoreFields?: string[]): string[];
98
+ function getDataChanges(input: object, initData: object, ignoreFields?: string[]): string[];
99
99
  /**
100
100
  * Get input function or value result
101
101
  * @param input Input function or value
@@ -150,7 +150,7 @@ export declare namespace Utils {
150
150
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
151
151
  * @returns Result
152
152
  */
153
- function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
153
+ function objectEqual(obj1: object, obj2: object, ignoreFields?: string[], strict?: number): boolean;
154
154
  /**
155
155
  * Get two object's unqiue properties
156
156
  * @param obj1 Object 1
@@ -158,7 +158,7 @@ export declare namespace Utils {
158
158
  * @param ignoreFields Ignored fields
159
159
  * @returns Unique properties
160
160
  */
161
- function objectKeys(obj1: {}, obj2: {}, ignoreFields?: string[]): Set<string>;
161
+ function objectKeys(obj1: object, obj2: object, ignoreFields?: string[]): Set<string>;
162
162
  /**
163
163
  * Get the new object's updated fields contrast to the previous object
164
164
  * @param objNew New object
@@ -167,7 +167,7 @@ export declare namespace Utils {
167
167
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
168
168
  * @returns Updated fields
169
169
  */
170
- function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
170
+ function objectUpdated(objNew: object, objPrev: object, ignoreFields?: string[], strict?: number): string[];
171
171
  /**
172
172
  * Parse string (JSON) to specific type, no type conversion
173
173
  * For type conversion, please use DataTypes.convert
@@ -202,7 +202,7 @@ export declare namespace Utils {
202
202
  * @param labels Labels
203
203
  * @param reference Key reference dictionary
204
204
  */
205
- const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
205
+ const setLabels: (source: DataTypes.StringRecord, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
206
206
  /**
207
207
  * Snake name to works, 'snake_name' to 'Snake Name'
208
208
  * @param name Name text
@@ -9,7 +9,6 @@ export declare class NodeStorage {
9
9
  source: [string, string][];
10
10
  /**
11
11
  * Constructor
12
- * @param session Is session storage
13
12
  */
14
13
  constructor();
15
14
  /**
@@ -8,7 +8,6 @@ exports.NodeStorage = void 0;
8
8
  class NodeStorage {
9
9
  /**
10
10
  * Constructor
11
- * @param session Is session storage
12
11
  */
13
12
  constructor() {
14
13
  /**
@@ -46,12 +46,12 @@ export interface IStorage {
46
46
  * Get object data
47
47
  * @param key Key name
48
48
  */
49
- getObject<T extends {}>(key: string): T | undefined;
49
+ getObject<T extends object>(key: string): T | undefined;
50
50
  /**
51
51
  * Get persisted object data
52
52
  * @param key Key name
53
53
  */
54
- getPersistedObject<T extends {}>(key: string): T | undefined;
54
+ getPersistedObject<T extends object>(key: string): T | undefined;
55
55
  /**
56
56
  * Set data
57
57
  * @param key Key name
@@ -48,12 +48,12 @@ export declare class WindowStorage implements IStorage {
48
48
  * Get object data
49
49
  * @param key Key name
50
50
  */
51
- getObject<T extends {}>(key: string): T | undefined;
51
+ getObject<T extends object>(key: string): T | undefined;
52
52
  /**
53
53
  * Get persisted object data
54
54
  * @param key Key name
55
55
  */
56
- getPersistedObject<T extends {}>(key: string): T | undefined;
56
+ getPersistedObject<T extends object>(key: string): T | undefined;
57
57
  /**
58
58
  * Set data
59
59
  * @param key Key name
@@ -41,7 +41,7 @@ interface EventOptions {
41
41
  once?: boolean;
42
42
  }
43
43
  declare type EventClassDef = {
44
- [key: string]: {};
44
+ [key: string]: object;
45
45
  };
46
46
  /**
47
47
  * Event class
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Generic object type
3
+ * Narrow case, uses StringRecord
4
+ * Before was wrong with {}, from 4.8 unknown = {} | null | undefined
5
+ */
1
6
  /**
2
7
  * Interface data types
3
8
  */
@@ -123,8 +128,8 @@ export declare namespace DataTypes {
123
128
  /**
124
129
  * Key collection, like { key1: {}, key2: {} }
125
130
  */
126
- type KeyCollection<K extends readonly string[], I extends {}> = {
127
- [index in K[number]]: I;
131
+ type KeyCollection<K extends readonly string[], I extends object> = {
132
+ [P in K[number]]: I;
128
133
  };
129
134
  /**
130
135
  * Enum value type
@@ -168,6 +173,10 @@ export declare namespace DataTypes {
168
173
  */
169
174
  label: string;
170
175
  };
176
+ /**
177
+ * Item with id and label dynamic type
178
+ */
179
+ type IdLabelType<I extends string, L extends string, D extends IdType = number> = DIS<I, D> & DIS<L, string>;
171
180
  /**
172
181
  * Get specific type keys
173
182
  */
@@ -177,7 +186,7 @@ export declare namespace DataTypes {
177
186
  /**
178
187
  * Culture definiton
179
188
  */
180
- type CultureDefinition<T extends {} = StringRecord> = Readonly<{
189
+ type CultureDefinition<T extends StringRecord = StringRecord> = Readonly<{
181
190
  /**
182
191
  * Name, like zh-CN
183
192
  */
@@ -195,6 +204,18 @@ export declare namespace DataTypes {
195
204
  */
196
205
  compatibleName?: string[];
197
206
  }>;
207
+ /**
208
+ * Dynamic interface with multiple properties
209
+ */
210
+ type DI<K extends readonly string[], T> = {
211
+ [P in K[number]]: T;
212
+ };
213
+ /**
214
+ * Dynamic interface with single property
215
+ */
216
+ type DIS<K extends string, T> = {
217
+ [P in K]: T;
218
+ };
198
219
  /**
199
220
  * Convert value to target type
200
221
  * @param input Input value
@@ -262,21 +283,28 @@ export declare namespace DataTypes {
262
283
  * @param key Property name
263
284
  * @returns Value
264
285
  */
265
- function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
286
+ function getValue<T extends object, K extends keyof T | string>(data: T | undefined | null, key: K): K extends keyof T ? T[K] : undefined;
266
287
  /**
267
288
  * Get object id field value
268
289
  * @param data Data
269
290
  * @param key Property name
270
291
  * @returns Id value
271
292
  */
272
- function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
293
+ function getIdValue<T extends object, K extends Keys<T, string | number>>(data: T, key: K): T[K];
294
+ /**
295
+ * Get object id field value 1
296
+ * @param data Data
297
+ * @param key Property name
298
+ * @returns Id value
299
+ */
300
+ function getIdValue1<T extends object, K extends keyof T | string>(data: T | undefined | null, key: K): K extends keyof T ? (T[K] extends number ? number : string) : undefined;
273
301
  /**
274
302
  * Get object string field value
275
303
  * @param data Data
276
304
  * @param key Property name
277
305
  * @returns String value
278
306
  */
279
- function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
307
+ function getStringValue<T extends object>(data: T | undefined | null, key: keyof T | string): string | undefined;
280
308
  /**
281
309
  * Check the type is a basic type or not (type guard)
282
310
  * @param name Type name
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Generic object type
3
+ * Narrow case, uses StringRecord
4
+ * Before was wrong with {}, from 4.8 unknown = {} | null | undefined
5
+ */
1
6
  /**
2
7
  * Interface data types
3
8
  */
@@ -287,14 +292,10 @@ export var DataTypes;
287
292
  * @returns Value
288
293
  */
289
294
  function getValue(data, key) {
290
- if (data == null)
291
- return null;
292
- const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
293
- if (value == null)
294
- return null;
295
- if (typeof value === 'number')
296
- return value;
297
- return convert(value, 'string');
295
+ if (data != null && typeof key === 'string' && key in data) {
296
+ return Reflect.get(data, key);
297
+ }
298
+ return undefined;
298
299
  }
299
300
  DataTypes.getValue = getValue;
300
301
  /**
@@ -307,6 +308,21 @@ export var DataTypes;
307
308
  return data[key];
308
309
  }
309
310
  DataTypes.getIdValue = getIdValue;
311
+ /**
312
+ * Get object id field value 1
313
+ * @param data Data
314
+ * @param key Property name
315
+ * @returns Id value
316
+ */
317
+ function getIdValue1(data, key) {
318
+ const value = getValue(data, key);
319
+ if (value == null)
320
+ return undefined;
321
+ if (typeof value === 'number')
322
+ return value;
323
+ return `${value}`;
324
+ }
325
+ DataTypes.getIdValue1 = getIdValue1;
310
326
  /**
311
327
  * Get object string field value
312
328
  * @param data Data
@@ -315,9 +331,11 @@ export var DataTypes;
315
331
  */
316
332
  function getStringValue(data, key) {
317
333
  const value = getValue(data, key);
318
- if (typeof value === 'number')
319
- return value.toString();
320
- return value;
334
+ if (value == null)
335
+ return undefined;
336
+ if (typeof value === 'string')
337
+ return value;
338
+ return `${value}`;
321
339
  }
322
340
  DataTypes.getStringValue = getStringValue;
323
341
  /**
@@ -19,7 +19,7 @@ export declare namespace DomUtils {
19
19
  * @param source Source data to match
20
20
  * @param keepFields Fields need to be kept
21
21
  */
22
- function clearFormData(data: IFormData, source?: {}, keepFields?: string[]): IFormData;
22
+ function clearFormData(data: IFormData, source?: object, keepFields?: string[]): IFormData;
23
23
  /**
24
24
  * Cast data as template format
25
25
  * @param source Source data
@@ -35,7 +35,7 @@ export declare namespace DomUtils {
35
35
  * @param keepSource Means even the template does not include the definition, still keep the item
36
36
  * @returns Result
37
37
  */
38
- function dataValueAs<T extends {}>(source: IFormData | {}, templateValue: T, keepSource?: boolean): Partial<T>;
38
+ function dataValueAs<T extends object>(source: IFormData | object, templateValue: T, keepSource?: boolean): Partial<T>;
39
39
  /**
40
40
  * Current detected country
41
41
  */
@@ -67,7 +67,7 @@ export declare namespace DomUtils {
67
67
  * @param items Available cultures
68
68
  * @param culture Detected culture
69
69
  */
70
- const getCulture: <T extends {}>(items: Readonly<{
70
+ const getCulture: <T extends DataTypes.StringRecord>(items: Readonly<{
71
71
  name: string;
72
72
  label: string;
73
73
  resources: T;
@@ -117,5 +117,5 @@ export declare namespace DomUtils {
117
117
  * @param name Element name or first collection item
118
118
  * @param container Container, limits the element range
119
119
  */
120
- function setFocus(name: string | {}, container?: HTMLElement): void;
120
+ function setFocus(name: string | object, container?: HTMLElement): void;
121
121
  }
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get local storage object data
31
31
  * @param key Key name
32
32
  */
33
- function getLocalObject<T extends {}>(key: string): T | undefined;
33
+ function getLocalObject<T extends object>(key: string): T | undefined;
34
34
  /**
35
35
  * Get session storage data
36
36
  * @param key Key name
@@ -46,5 +46,5 @@ export declare namespace StorageUtils {
46
46
  * Get session storage object data
47
47
  * @param key Key name
48
48
  */
49
- function getSessionObject<T extends {}>(key: string): T | undefined;
49
+ function getSessionObject<T extends object>(key: string): T | undefined;
50
50
  }
@@ -53,7 +53,7 @@ export declare namespace Utils {
53
53
  * @param labelField Label field, default is label
54
54
  * @param blankLabel Blank label, default is ---
55
55
  */
56
- function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
56
+ function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
57
57
  /**
58
58
  * Base64 chars to number
59
59
  * @param base64Chars Base64 chars
@@ -65,7 +65,7 @@ export declare namespace Utils {
65
65
  * @param input Input object
66
66
  * @param fields Fields to correct
67
67
  */
68
- function correctTypes<T extends {}, F extends {
68
+ function correctTypes<T extends object, F extends {
69
69
  [P in keyof T]: DataTypes.BasicNames;
70
70
  }>(input: T, fields: F): void;
71
71
  /**
@@ -95,7 +95,7 @@ export declare namespace Utils {
95
95
  * @param ignoreFields Ignore fields
96
96
  * @returns
97
97
  */
98
- function getDataChanges(input: {}, initData: {}, ignoreFields?: string[]): string[];
98
+ function getDataChanges(input: object, initData: object, ignoreFields?: string[]): string[];
99
99
  /**
100
100
  * Get input function or value result
101
101
  * @param input Input function or value
@@ -150,7 +150,7 @@ export declare namespace Utils {
150
150
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
151
151
  * @returns Result
152
152
  */
153
- function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
153
+ function objectEqual(obj1: object, obj2: object, ignoreFields?: string[], strict?: number): boolean;
154
154
  /**
155
155
  * Get two object's unqiue properties
156
156
  * @param obj1 Object 1
@@ -158,7 +158,7 @@ export declare namespace Utils {
158
158
  * @param ignoreFields Ignored fields
159
159
  * @returns Unique properties
160
160
  */
161
- function objectKeys(obj1: {}, obj2: {}, ignoreFields?: string[]): Set<string>;
161
+ function objectKeys(obj1: object, obj2: object, ignoreFields?: string[]): Set<string>;
162
162
  /**
163
163
  * Get the new object's updated fields contrast to the previous object
164
164
  * @param objNew New object
@@ -167,7 +167,7 @@ export declare namespace Utils {
167
167
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
168
168
  * @returns Updated fields
169
169
  */
170
- function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
170
+ function objectUpdated(objNew: object, objPrev: object, ignoreFields?: string[], strict?: number): string[];
171
171
  /**
172
172
  * Parse string (JSON) to specific type, no type conversion
173
173
  * For type conversion, please use DataTypes.convert
@@ -202,7 +202,7 @@ export declare namespace Utils {
202
202
  * @param labels Labels
203
203
  * @param reference Key reference dictionary
204
204
  */
205
- const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
205
+ const setLabels: (source: DataTypes.StringRecord, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
206
206
  /**
207
207
  * Snake name to works, 'snake_name' to 'Snake Name'
208
208
  * @param name Name text
@@ -9,7 +9,6 @@ export declare class NodeStorage {
9
9
  source: [string, string][];
10
10
  /**
11
11
  * Constructor
12
- * @param session Is session storage
13
12
  */
14
13
  constructor();
15
14
  /**
@@ -5,7 +5,6 @@
5
5
  export class NodeStorage {
6
6
  /**
7
7
  * Constructor
8
- * @param session Is session storage
9
8
  */
10
9
  constructor() {
11
10
  /**
@@ -46,12 +46,12 @@ export interface IStorage {
46
46
  * Get object data
47
47
  * @param key Key name
48
48
  */
49
- getObject<T extends {}>(key: string): T | undefined;
49
+ getObject<T extends object>(key: string): T | undefined;
50
50
  /**
51
51
  * Get persisted object data
52
52
  * @param key Key name
53
53
  */
54
- getPersistedObject<T extends {}>(key: string): T | undefined;
54
+ getPersistedObject<T extends object>(key: string): T | undefined;
55
55
  /**
56
56
  * Set data
57
57
  * @param key Key name
@@ -48,12 +48,12 @@ export declare class WindowStorage implements IStorage {
48
48
  * Get object data
49
49
  * @param key Key name
50
50
  */
51
- getObject<T extends {}>(key: string): T | undefined;
51
+ getObject<T extends object>(key: string): T | undefined;
52
52
  /**
53
53
  * Get persisted object data
54
54
  * @param key Key name
55
55
  */
56
- getPersistedObject<T extends {}>(key: string): T | undefined;
56
+ getPersistedObject<T extends object>(key: string): T | undefined;
57
57
  /**
58
58
  * Set data
59
59
  * @param key Key name
@@ -41,7 +41,7 @@ interface EventOptions {
41
41
  once?: boolean;
42
42
  }
43
43
  declare type EventClassDef = {
44
- [key: string]: {};
44
+ [key: string]: object;
45
45
  };
46
46
  /**
47
47
  * Event class
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.45",
3
+ "version": "1.1.48",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,15 +55,15 @@
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "dependencies": {},
57
57
  "devDependencies": {
58
- "@types/jest": "^28.1.6",
59
- "@typescript-eslint/eslint-plugin": "^5.33.0",
60
- "@typescript-eslint/parser": "^5.33.0",
58
+ "@types/jest": "^28.1.8",
59
+ "@typescript-eslint/eslint-plugin": "^5.35.1",
60
+ "@typescript-eslint/parser": "^5.35.1",
61
61
  "eslint": "^8.22.0",
62
62
  "eslint-config-airbnb-base": "^15.0.0",
63
63
  "eslint-plugin-import": "^2.26.0",
64
64
  "jest": "^28.1.3",
65
65
  "jest-environment-jsdom": "^28.1.3",
66
- "ts-jest": "^28.0.7",
66
+ "ts-jest": "^28.0.8",
67
67
  "typescript": "^4.7.4"
68
68
  }
69
69
  }
package/src/DataTypes.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Generic object type
3
+ * Narrow case, uses StringRecord
4
+ * Before was wrong with {}, from 4.8 unknown = {} | null | undefined
5
+ */
6
+
1
7
  /**
2
8
  * Interface data types
3
9
  */
@@ -159,8 +165,8 @@ export namespace DataTypes {
159
165
  /**
160
166
  * Key collection, like { key1: {}, key2: {} }
161
167
  */
162
- export type KeyCollection<K extends readonly string[], I extends {}> = {
163
- [index in K[number]]: I;
168
+ export type KeyCollection<K extends readonly string[], I extends object> = {
169
+ [P in K[number]]: I;
164
170
  };
165
171
 
166
172
  /**
@@ -213,6 +219,15 @@ export namespace DataTypes {
213
219
  label: string;
214
220
  };
215
221
 
222
+ /**
223
+ * Item with id and label dynamic type
224
+ */
225
+ export type IdLabelType<
226
+ I extends string,
227
+ L extends string,
228
+ D extends IdType = number
229
+ > = DIS<I, D> & DIS<L, string>;
230
+
216
231
  /**
217
232
  * Get specific type keys
218
233
  */
@@ -223,27 +238,38 @@ export namespace DataTypes {
223
238
  /**
224
239
  * Culture definiton
225
240
  */
226
- export type CultureDefinition<T extends {} = StringRecord> = Readonly<{
227
- /**
228
- * Name, like zh-CN
229
- */
230
- name: string;
241
+ export type CultureDefinition<T extends StringRecord = StringRecord> =
242
+ Readonly<{
243
+ /**
244
+ * Name, like zh-CN
245
+ */
246
+ name: string;
231
247
 
232
- /**
233
- * Label for description, like Simplifined Chinese
234
- */
235
- label: string;
248
+ /**
249
+ * Label for description, like Simplifined Chinese
250
+ */
251
+ label: string;
236
252
 
237
- /**
238
- * Resources
239
- */
240
- resources: T;
253
+ /**
254
+ * Resources
255
+ */
256
+ resources: T;
241
257
 
242
- /**
243
- * Compatible names
244
- */
245
- compatibleName?: string[];
246
- }>;
258
+ /**
259
+ * Compatible names
260
+ */
261
+ compatibleName?: string[];
262
+ }>;
263
+
264
+ /**
265
+ * Dynamic interface with multiple properties
266
+ */
267
+ export type DI<K extends readonly string[], T> = { [P in K[number]]: T };
268
+
269
+ /**
270
+ * Dynamic interface with single property
271
+ */
272
+ export type DIS<K extends string, T> = { [P in K]: T };
247
273
 
248
274
  /**
249
275
  * Convert value to target type
@@ -504,16 +530,14 @@ export namespace DataTypes {
504
530
  * @param key Property name
505
531
  * @returns Value
506
532
  */
507
- export function getValue<T extends {}>(
533
+ export function getValue<T extends object, K extends keyof T | string>(
508
534
  data: T | undefined | null,
509
- key: keyof T | string
510
- ): IdType | undefined | null {
511
- if (data == null) return null;
512
- const value =
513
- typeof key === 'string' ? Reflect.get(data, key) : data[key];
514
- if (value == null) return null;
515
- if (typeof value === 'number') return value;
516
- return convert(value, 'string');
535
+ key: K
536
+ ): K extends keyof T ? T[K] : undefined {
537
+ if (data != null && typeof key === 'string' && key in data) {
538
+ return Reflect.get(data, key);
539
+ }
540
+ return undefined as any;
517
541
  }
518
542
 
519
543
  /**
@@ -523,25 +547,42 @@ export namespace DataTypes {
523
547
  * @returns Id value
524
548
  */
525
549
  export function getIdValue<
526
- T extends {},
550
+ T extends object,
527
551
  K extends Keys<T, string | number>
528
552
  >(data: T, key: K): T[K] {
529
553
  return data[key];
530
554
  }
531
555
 
556
+ /**
557
+ * Get object id field value 1
558
+ * @param data Data
559
+ * @param key Property name
560
+ * @returns Id value
561
+ */
562
+ export function getIdValue1<T extends object, K extends keyof T | string>(
563
+ data: T | undefined | null,
564
+ key: K
565
+ ): K extends keyof T ? (T[K] extends number ? number : string) : undefined {
566
+ const value = getValue(data, key);
567
+ if (value == null) return undefined as any;
568
+ if (typeof value === 'number') return value as any;
569
+ return `${value}` as any;
570
+ }
571
+
532
572
  /**
533
573
  * Get object string field value
534
574
  * @param data Data
535
575
  * @param key Property name
536
576
  * @returns String value
537
577
  */
538
- export function getStringValue<T extends {}>(
578
+ export function getStringValue<T extends object>(
539
579
  data: T | undefined | null,
540
580
  key: keyof T | string
541
- ): string | undefined | null {
581
+ ): string | undefined {
542
582
  const value = getValue(data, key);
543
- if (typeof value === 'number') return value.toString();
544
- return value;
583
+ if (value == null) return undefined;
584
+ if (typeof value === 'string') return value;
585
+ return `${value}`;
545
586
  }
546
587
 
547
588
  /**
package/src/DomUtils.ts CHANGED
@@ -30,7 +30,7 @@ export namespace DomUtils {
30
30
  */
31
31
  export function clearFormData(
32
32
  data: IFormData,
33
- source?: {},
33
+ source?: object,
34
34
  keepFields?: string[]
35
35
  ) {
36
36
  // Unique keys, FormData may have same name keys
@@ -98,9 +98,9 @@ export namespace DomUtils {
98
98
  }
99
99
 
100
100
  function dataAsTraveller(
101
- source: IFormData | {},
102
- data: {},
103
- template: {},
101
+ source: IFormData | object,
102
+ data: object,
103
+ template: object,
104
104
  keepSource: boolean,
105
105
  isValue: boolean
106
106
  ) {
@@ -183,8 +183,8 @@ export namespace DomUtils {
183
183
  * @param keepSource Means even the template does not include the definition, still keep the item
184
184
  * @returns Result
185
185
  */
186
- export function dataValueAs<T extends {}>(
187
- source: IFormData | {},
186
+ export function dataValueAs<T extends object>(
187
+ source: IFormData | object,
188
188
  templateValue: T,
189
189
  keepSource: boolean = false
190
190
  ): Partial<T> {
@@ -312,7 +312,7 @@ export namespace DomUtils {
312
312
  * @param items Available cultures
313
313
  * @param culture Detected culture
314
314
  */
315
- export const getCulture = <T extends {}>(
315
+ export const getCulture = <T extends DataTypes.StringRecord>(
316
316
  items: DataTypes.CultureDefinition<T>[],
317
317
  culture: string
318
318
  ) => {
@@ -453,7 +453,7 @@ export namespace DomUtils {
453
453
  * @param name Element name or first collection item
454
454
  * @param container Container, limits the element range
455
455
  */
456
- export function setFocus(name: string | {}, container?: HTMLElement) {
456
+ export function setFocus(name: string | object, container?: HTMLElement) {
457
457
  const elementName =
458
458
  typeof name === 'string' ? name : Object.keys(name)[0];
459
459
 
@@ -80,7 +80,7 @@ export namespace StorageUtils {
80
80
  * Get local storage object data
81
81
  * @param key Key name
82
82
  */
83
- export function getLocalObject<T extends {}>(key: string) {
83
+ export function getLocalObject<T extends object>(key: string) {
84
84
  // Get storage
85
85
  const data = localStorage.getItem(key);
86
86
  if (data == null) return undefined;
@@ -123,7 +123,7 @@ export namespace StorageUtils {
123
123
  * Get session storage object data
124
124
  * @param key Key name
125
125
  */
126
- export function getSessionObject<T extends {}>(key: string) {
126
+ export function getSessionObject<T extends object>(key: string) {
127
127
  // Get storage
128
128
  const data = sessionStorage.getItem(key);
129
129
  if (data == null) return undefined;
package/src/Utils.ts CHANGED
@@ -119,7 +119,7 @@ export namespace Utils {
119
119
  * @param labelField Label field, default is label
120
120
  * @param blankLabel Blank label, default is ---
121
121
  */
122
- export function addBlankItem<T extends {}>(
122
+ export function addBlankItem<T extends object>(
123
123
  options: T[],
124
124
  idField?: string | keyof T,
125
125
  labelField?: unknown,
@@ -155,7 +155,7 @@ export namespace Utils {
155
155
  * @param fields Fields to correct
156
156
  */
157
157
  export function correctTypes<
158
- T extends {},
158
+ T extends object,
159
159
  F extends { [P in keyof T]: DataTypes.BasicNames }
160
160
  >(input: T, fields: F) {
161
161
  for (const field in fields) {
@@ -218,8 +218,8 @@ export namespace Utils {
218
218
  * @returns
219
219
  */
220
220
  export function getDataChanges(
221
- input: {},
222
- initData: {},
221
+ input: object,
222
+ initData: object,
223
223
  ignoreFields: string[] = ['id']
224
224
  ): string[] {
225
225
  // Changed fields
@@ -375,8 +375,8 @@ export namespace Utils {
375
375
  * @returns Result
376
376
  */
377
377
  export function objectEqual(
378
- obj1: {},
379
- obj2: {},
378
+ obj1: object,
379
+ obj2: object,
380
380
  ignoreFields: string[] = [],
381
381
  strict = 1
382
382
  ) {
@@ -402,8 +402,8 @@ export namespace Utils {
402
402
  * @returns Unique properties
403
403
  */
404
404
  export function objectKeys(
405
- obj1: {},
406
- obj2: {},
405
+ obj1: object,
406
+ obj2: object,
407
407
  ignoreFields: string[] = []
408
408
  ) {
409
409
  // All keys
@@ -424,8 +424,8 @@ export namespace Utils {
424
424
  * @returns Updated fields
425
425
  */
426
426
  export function objectUpdated(
427
- objNew: {},
428
- objPrev: {},
427
+ objNew: object,
428
+ objPrev: object,
429
429
  ignoreFields: string[] = [],
430
430
  strict = 1
431
431
  ) {
@@ -537,7 +537,7 @@ export namespace Utils {
537
537
  * @param reference Key reference dictionary
538
538
  */
539
539
  export const setLabels = (
540
- source: {},
540
+ source: DataTypes.StringRecord,
541
541
  labels: DataTypes.StringRecord,
542
542
  reference?: Readonly<DataTypes.StringDictionary>
543
543
  ) => {
@@ -10,7 +10,6 @@ export class NodeStorage {
10
10
 
11
11
  /**
12
12
  * Constructor
13
- * @param session Is session storage
14
13
  */
15
14
  constructor() {}
16
15
 
@@ -53,13 +53,13 @@ export interface IStorage {
53
53
  * Get object data
54
54
  * @param key Key name
55
55
  */
56
- getObject<T extends {}>(key: string): T | undefined;
56
+ getObject<T extends object>(key: string): T | undefined;
57
57
 
58
58
  /**
59
59
  * Get persisted object data
60
60
  * @param key Key name
61
61
  */
62
- getPersistedObject<T extends {}>(key: string): T | undefined;
62
+ getPersistedObject<T extends object>(key: string): T | undefined;
63
63
 
64
64
  /**
65
65
  * Set data
@@ -105,7 +105,7 @@ export class WindowStorage implements IStorage {
105
105
  * Get object data
106
106
  * @param key Key name
107
107
  */
108
- getObject<T extends {}>(key: string) {
108
+ getObject<T extends object>(key: string) {
109
109
  // Get storage
110
110
  const data = sessionStorage.getItem(key);
111
111
 
@@ -118,7 +118,7 @@ export class WindowStorage implements IStorage {
118
118
  * Get persisted object data
119
119
  * @param key Key name
120
120
  */
121
- getPersistedObject<T extends {}>(key: string) {
121
+ getPersistedObject<T extends object>(key: string) {
122
122
  // Get storage
123
123
  const data = localStorage.getItem(key);
124
124
 
@@ -55,7 +55,7 @@ interface EventOptions {
55
55
  once?: boolean;
56
56
  }
57
57
 
58
- type EventClassDef = { [key: string]: {} };
58
+ type EventClassDef = { [key: string]: object };
59
59
 
60
60
  /**
61
61
  * Event class