@etsoo/shared 1.1.41 → 1.1.44

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.
@@ -28,7 +28,7 @@ jobs:
28
28
  # Setup .npmrc file to publish to npm
29
29
  - uses: actions/setup-node@v1
30
30
  with:
31
- node-version: '14.18'
31
+ node-version: '18.x'
32
32
  registry-url: 'https://registry.npmjs.org'
33
33
 
34
34
  # Named after Continuous Integration, installs dependencies directly from package-lock.json
package/README.md CHANGED
@@ -98,6 +98,7 @@ Data type definitions and type safe functions
98
98
  |IdItem|Item with id or id generator|
99
99
  |IdLabelItem|Item with id and label|
100
100
  |KeyCollection|Key collection, like { key1: {}, key2: {} }|
101
+ |Keys|Get specific type keys|
101
102
  |Simple|Basic or basic array type|
102
103
  |SimpleEnum|Simple type enum|
103
104
  |SimpleNames|Simple type names|
@@ -117,10 +118,9 @@ Data type definitions and type safe functions
117
118
  |getEnumKey|Get enum string literal type value|
118
119
  |getEnumKeys|Get Enum keys|
119
120
  |getIdValue|Get object id field value|
120
- |getItemId|Get item id|
121
- |getItemLabel|Get item label|
122
121
  |getResult|Get input function or value result|
123
122
  |getStringValue|Get object string field value|
123
+ |getValue|Get object field value|
124
124
  |isBasicName|Check the type is a basic type or not (type guard)|
125
125
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
126
126
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -77,24 +77,29 @@ test('Tests for getEnumKeys', () => {
77
77
  expect(keys).toContainEqual('Unkwown');
78
78
  });
79
79
 
80
- test('Tests for getItemId', () => {
80
+ test('Tests for IdLabelItem', () => {
81
81
  // Arrange
82
- const items: DataTypes.IdItem[] = [
83
- { id: 1 },
84
- { id: '123' },
85
- { id: () => 'f123' }
82
+ const items: DataTypes.IdLabelItem[] = [
83
+ { id: 1, label: 'Item 1' },
84
+ { id: 2, label: 'Item 2' }
86
85
  ];
87
86
 
88
87
  // Assert
89
- expect(DataTypes.getItemId(items[0])).toBe('1');
90
- expect(DataTypes.getItemId(items[2])).toBe('f123');
88
+ expect(items[0].id).toBe(1);
89
+ expect(items[1].label).toBe('Item 2');
91
90
  });
92
91
 
93
- test('Tests for getIdValue', () => {
92
+ test('Tests for getValue', () => {
94
93
  const data = { id: 1, flag: true };
94
+ expect(DataTypes.getValue(data, 'id')).toBe(1);
95
+ expect(DataTypes.getValue(data, 'flag')).toBe('true');
96
+ expect(DataTypes.getValue(data, 'unknown')).toBeNull();
97
+ });
98
+
99
+ test('Tests for getIdValue', () => {
100
+ const data = { id: 1, flag: true, field: 'string' };
95
101
  expect(DataTypes.getIdValue(data, 'id')).toBe(1);
96
- expect(DataTypes.getIdValue(data, 'flag')).toBe('true');
97
- expect(DataTypes.getIdValue(data, 'unknown')).toBeNull();
102
+ expect(DataTypes.getIdValue(data, 'field')).toBe('string');
98
103
  });
99
104
 
100
105
  test('Tests for getStringValue', () => {
@@ -103,19 +108,6 @@ test('Tests for getStringValue', () => {
103
108
  expect(DataTypes.getStringValue(data, 'flag')).toBe('true');
104
109
  });
105
110
 
106
- test('Tests for getItemLabel', () => {
107
- // Arrange
108
- const items: DataTypes.IdLabelItem[] = [
109
- { id: 1, label: '123' },
110
- { id: '123', label: () => 'f123' },
111
- { id: () => 'f123', label: 'l123' }
112
- ];
113
-
114
- // Assert
115
- expect(DataTypes.getItemLabel(items[0])).toBe('123');
116
- expect(DataTypes.getItemLabel(items[1])).toBe('f123');
117
- });
118
-
119
111
  test('Tests for isSimpleType', () => {
120
112
  expect(DataTypes.isSimpleType(1)).toBeTruthy();
121
113
  expect(DataTypes.isSimpleType(new Date())).toBeTruthy();
@@ -133,7 +133,7 @@ export declare namespace DataTypes {
133
133
  /**
134
134
  * Enum base type
135
135
  */
136
- type EnumBase = Record<string, EnumValue>;
136
+ type EnumBase<T extends EnumValue = EnumValue> = Record<string, T>;
137
137
  /**
138
138
  * Function type
139
139
  */
@@ -153,33 +153,27 @@ export declare namespace DataTypes {
153
153
  /**
154
154
  * Item with id property
155
155
  */
156
- type IdItem = {
156
+ type IdItem<T extends IdType = number> = {
157
157
  /**
158
- * Id field or generator
158
+ * Id field
159
159
  */
160
- id: IdType | (() => string);
160
+ id: T;
161
161
  };
162
162
  /**
163
163
  * Item with id and label property
164
164
  */
165
- type IdLabelItem = IdItem & {
165
+ type IdLabelItem<T extends IdType = number> = IdItem<T> & {
166
166
  /**
167
- * label field or generator
167
+ * label field
168
168
  */
169
- label: string | (() => string);
169
+ label: string;
170
170
  };
171
171
  /**
172
- * Get item id
173
- * @param item Item with id
174
- * @returns string id
175
- */
176
- const getItemId: (item: IdItem) => string;
177
- /**
178
- * Get item label
179
- * @param item Item with id
180
- * @returns string id
172
+ * Get specific type keys
181
173
  */
182
- const getItemLabel: (item: IdLabelItem) => string;
174
+ type Keys<T, R = string | number> = {
175
+ [k in keyof T]: T[k] extends R ? k : never;
176
+ }[keyof T];
183
177
  /**
184
178
  * Culture definiton
185
179
  */
@@ -262,13 +256,20 @@ export declare namespace DataTypes {
262
256
  * @returns Keys
263
257
  */
264
258
  function getEnumKeys<T extends EnumBase, K extends keyof T & string>(input: T): K[];
259
+ /**
260
+ * Get object field value
261
+ * @param data Data
262
+ * @param key Property name
263
+ * @returns Value
264
+ */
265
+ function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
265
266
  /**
266
267
  * Get object id field value
267
268
  * @param data Data
268
269
  * @param key Property name
269
270
  * @returns Id value
270
271
  */
271
- function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
272
+ function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
272
273
  /**
273
274
  * Get object string field value
274
275
  * @param data Data
@@ -71,28 +71,6 @@ var DataTypes;
71
71
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
72
72
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
73
73
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
74
- /**
75
- * Get item id
76
- * @param item Item with id
77
- * @returns string id
78
- */
79
- DataTypes.getItemId = (item) => {
80
- if (typeof item.id === 'function')
81
- return item.id();
82
- if (typeof item.id === 'number')
83
- return item.id.toString();
84
- return item.id;
85
- };
86
- /**
87
- * Get item label
88
- * @param item Item with id
89
- * @returns string id
90
- */
91
- DataTypes.getItemLabel = (item) => {
92
- if (typeof item.label === 'function')
93
- return item.label();
94
- return item.label;
95
- };
96
74
  /**
97
75
  * Convert value to target type
98
76
  * @param input Input value
@@ -306,12 +284,12 @@ var DataTypes;
306
284
  }
307
285
  DataTypes.getEnumKeys = getEnumKeys;
308
286
  /**
309
- * Get object id field value
287
+ * Get object field value
310
288
  * @param data Data
311
289
  * @param key Property name
312
- * @returns Id value
290
+ * @returns Value
313
291
  */
314
- function getIdValue(data, key) {
292
+ function getValue(data, key) {
315
293
  if (data == null)
316
294
  return null;
317
295
  const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
@@ -321,6 +299,16 @@ var DataTypes;
321
299
  return value;
322
300
  return convert(value, 'string');
323
301
  }
302
+ DataTypes.getValue = getValue;
303
+ /**
304
+ * Get object id field value
305
+ * @param data Data
306
+ * @param key Property name
307
+ * @returns Id value
308
+ */
309
+ function getIdValue(data, key) {
310
+ return data[key];
311
+ }
324
312
  DataTypes.getIdValue = getIdValue;
325
313
  /**
326
314
  * Get object string field value
@@ -329,7 +317,7 @@ var DataTypes;
329
317
  * @returns String value
330
318
  */
331
319
  function getStringValue(data, key) {
332
- const value = getIdValue(data, key);
320
+ const value = getValue(data, key);
333
321
  if (typeof value === 'number')
334
322
  return value.toString();
335
323
  return value;
@@ -133,7 +133,7 @@ export declare namespace DataTypes {
133
133
  /**
134
134
  * Enum base type
135
135
  */
136
- type EnumBase = Record<string, EnumValue>;
136
+ type EnumBase<T extends EnumValue = EnumValue> = Record<string, T>;
137
137
  /**
138
138
  * Function type
139
139
  */
@@ -153,33 +153,27 @@ export declare namespace DataTypes {
153
153
  /**
154
154
  * Item with id property
155
155
  */
156
- type IdItem = {
156
+ type IdItem<T extends IdType = number> = {
157
157
  /**
158
- * Id field or generator
158
+ * Id field
159
159
  */
160
- id: IdType | (() => string);
160
+ id: T;
161
161
  };
162
162
  /**
163
163
  * Item with id and label property
164
164
  */
165
- type IdLabelItem = IdItem & {
165
+ type IdLabelItem<T extends IdType = number> = IdItem<T> & {
166
166
  /**
167
- * label field or generator
167
+ * label field
168
168
  */
169
- label: string | (() => string);
169
+ label: string;
170
170
  };
171
171
  /**
172
- * Get item id
173
- * @param item Item with id
174
- * @returns string id
175
- */
176
- const getItemId: (item: IdItem) => string;
177
- /**
178
- * Get item label
179
- * @param item Item with id
180
- * @returns string id
172
+ * Get specific type keys
181
173
  */
182
- const getItemLabel: (item: IdLabelItem) => string;
174
+ type Keys<T, R = string | number> = {
175
+ [k in keyof T]: T[k] extends R ? k : never;
176
+ }[keyof T];
183
177
  /**
184
178
  * Culture definiton
185
179
  */
@@ -262,13 +256,20 @@ export declare namespace DataTypes {
262
256
  * @returns Keys
263
257
  */
264
258
  function getEnumKeys<T extends EnumBase, K extends keyof T & string>(input: T): K[];
259
+ /**
260
+ * Get object field value
261
+ * @param data Data
262
+ * @param key Property name
263
+ * @returns Value
264
+ */
265
+ function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
265
266
  /**
266
267
  * Get object id field value
267
268
  * @param data Data
268
269
  * @param key Property name
269
270
  * @returns Id value
270
271
  */
271
- function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
272
+ function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
272
273
  /**
273
274
  * Get object string field value
274
275
  * @param data Data
@@ -68,28 +68,6 @@ export var DataTypes;
68
68
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
69
69
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
70
70
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
71
- /**
72
- * Get item id
73
- * @param item Item with id
74
- * @returns string id
75
- */
76
- DataTypes.getItemId = (item) => {
77
- if (typeof item.id === 'function')
78
- return item.id();
79
- if (typeof item.id === 'number')
80
- return item.id.toString();
81
- return item.id;
82
- };
83
- /**
84
- * Get item label
85
- * @param item Item with id
86
- * @returns string id
87
- */
88
- DataTypes.getItemLabel = (item) => {
89
- if (typeof item.label === 'function')
90
- return item.label();
91
- return item.label;
92
- };
93
71
  /**
94
72
  * Convert value to target type
95
73
  * @param input Input value
@@ -303,12 +281,12 @@ export var DataTypes;
303
281
  }
304
282
  DataTypes.getEnumKeys = getEnumKeys;
305
283
  /**
306
- * Get object id field value
284
+ * Get object field value
307
285
  * @param data Data
308
286
  * @param key Property name
309
- * @returns Id value
287
+ * @returns Value
310
288
  */
311
- function getIdValue(data, key) {
289
+ function getValue(data, key) {
312
290
  if (data == null)
313
291
  return null;
314
292
  const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
@@ -318,6 +296,16 @@ export var DataTypes;
318
296
  return value;
319
297
  return convert(value, 'string');
320
298
  }
299
+ DataTypes.getValue = getValue;
300
+ /**
301
+ * Get object id field value
302
+ * @param data Data
303
+ * @param key Property name
304
+ * @returns Id value
305
+ */
306
+ function getIdValue(data, key) {
307
+ return data[key];
308
+ }
321
309
  DataTypes.getIdValue = getIdValue;
322
310
  /**
323
311
  * Get object string field value
@@ -326,7 +314,7 @@ export var DataTypes;
326
314
  * @returns String value
327
315
  */
328
316
  function getStringValue(data, key) {
329
- const value = getIdValue(data, key);
317
+ const value = getValue(data, key);
330
318
  if (typeof value === 'number')
331
319
  return value.toString();
332
320
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.41",
3
+ "version": "1.1.44",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -56,9 +56,9 @@
56
56
  "dependencies": {},
57
57
  "devDependencies": {
58
58
  "@types/jest": "^28.1.6",
59
- "@typescript-eslint/eslint-plugin": "^5.32.0",
60
- "@typescript-eslint/parser": "^5.32.0",
61
- "eslint": "^8.21.0",
59
+ "@typescript-eslint/eslint-plugin": "^5.33.0",
60
+ "@typescript-eslint/parser": "^5.33.0",
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",
package/src/DataTypes.ts CHANGED
@@ -171,7 +171,7 @@ export namespace DataTypes {
171
171
  /**
172
172
  * Enum base type
173
173
  */
174
- export type EnumBase = Record<string, EnumValue>;
174
+ export type EnumBase<T extends EnumValue = EnumValue> = Record<string, T>;
175
175
 
176
176
  /**
177
177
  * Function type
@@ -196,43 +196,29 @@ export namespace DataTypes {
196
196
  /**
197
197
  * Item with id property
198
198
  */
199
- export type IdItem = {
199
+ export type IdItem<T extends IdType = number> = {
200
200
  /**
201
- * Id field or generator
201
+ * Id field
202
202
  */
203
- id: IdType | (() => string);
203
+ id: T;
204
204
  };
205
205
 
206
206
  /**
207
207
  * Item with id and label property
208
208
  */
209
- export type IdLabelItem = IdItem & {
209
+ export type IdLabelItem<T extends IdType = number> = IdItem<T> & {
210
210
  /**
211
- * label field or generator
211
+ * label field
212
212
  */
213
- label: string | (() => string);
214
- };
215
-
216
- /**
217
- * Get item id
218
- * @param item Item with id
219
- * @returns string id
220
- */
221
- export const getItemId = (item: IdItem) => {
222
- if (typeof item.id === 'function') return item.id();
223
- if (typeof item.id === 'number') return item.id.toString();
224
- return item.id;
213
+ label: string;
225
214
  };
226
215
 
227
216
  /**
228
- * Get item label
229
- * @param item Item with id
230
- * @returns string id
217
+ * Get specific type keys
231
218
  */
232
- export const getItemLabel = (item: IdLabelItem) => {
233
- if (typeof item.label === 'function') return item.label();
234
- return item.label;
235
- };
219
+ export type Keys<T, R = string | number> = {
220
+ [k in keyof T]: T[k] extends R ? k : never;
221
+ }[keyof T];
236
222
 
237
223
  /**
238
224
  * Culture definiton
@@ -513,12 +499,12 @@ export namespace DataTypes {
513
499
  }
514
500
 
515
501
  /**
516
- * Get object id field value
502
+ * Get object field value
517
503
  * @param data Data
518
504
  * @param key Property name
519
- * @returns Id value
505
+ * @returns Value
520
506
  */
521
- export function getIdValue<T extends {}>(
507
+ export function getValue<T extends {}>(
522
508
  data: T | undefined | null,
523
509
  key: keyof T | string
524
510
  ): IdType | undefined | null {
@@ -530,6 +516,19 @@ export namespace DataTypes {
530
516
  return convert(value, 'string');
531
517
  }
532
518
 
519
+ /**
520
+ * Get object id field value
521
+ * @param data Data
522
+ * @param key Property name
523
+ * @returns Id value
524
+ */
525
+ export function getIdValue<
526
+ T extends {},
527
+ K extends Keys<T, string | number>
528
+ >(data: T, key: K): T[K] {
529
+ return data[key];
530
+ }
531
+
533
532
  /**
534
533
  * Get object string field value
535
534
  * @param data Data
@@ -540,7 +539,7 @@ export namespace DataTypes {
540
539
  data: T | undefined | null,
541
540
  key: keyof T | string
542
541
  ): string | undefined | null {
543
- const value = getIdValue(data, key);
542
+ const value = getValue(data, key);
544
543
  if (typeof value === 'number') return value.toString();
545
544
  return value;
546
545
  }