@etsoo/shared 1.1.39 → 1.1.42

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|
@@ -121,6 +122,7 @@ Data type definitions and type safe functions
121
122
  |getItemLabel|Get item label|
122
123
  |getResult|Get input function or value result|
123
124
  |getStringValue|Get object string field value|
125
+ |getValue|Get object field value|
124
126
  |isBasicName|Check the type is a basic type or not (type guard)|
125
127
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
126
128
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -90,11 +90,17 @@ test('Tests for getItemId', () => {
90
90
  expect(DataTypes.getItemId(items[2])).toBe('f123');
91
91
  });
92
92
 
93
- test('Tests for getIdValue', () => {
93
+ test('Tests for getValue', () => {
94
94
  const data = { id: 1, flag: true };
95
+ expect(DataTypes.getValue(data, 'id')).toBe(1);
96
+ expect(DataTypes.getValue(data, 'flag')).toBe('true');
97
+ expect(DataTypes.getValue(data, 'unknown')).toBeNull();
98
+ });
99
+
100
+ test('Tests for getIdValue', () => {
101
+ const data = { id: 1, flag: true, field: 'string' };
95
102
  expect(DataTypes.getIdValue(data, 'id')).toBe(1);
96
- expect(DataTypes.getIdValue(data, 'flag')).toBe('true');
97
- expect(DataTypes.getIdValue(data, 'unknown')).toBeNull();
103
+ expect(DataTypes.getIdValue(data, 'field')).toBe('string');
98
104
  });
99
105
 
100
106
  test('Tests for getStringValue', () => {
@@ -168,6 +168,12 @@ export declare namespace DataTypes {
168
168
  */
169
169
  label: string | (() => string);
170
170
  };
171
+ /**
172
+ * Get specific type keys
173
+ */
174
+ type Keys<T, R = string | number> = {
175
+ [k in keyof T]: T[k] extends R ? k : never;
176
+ }[keyof T];
171
177
  /**
172
178
  * Get item id
173
179
  * @param item Item with id
@@ -262,13 +268,20 @@ export declare namespace DataTypes {
262
268
  * @returns Keys
263
269
  */
264
270
  function getEnumKeys<T extends EnumBase, K extends keyof T & string>(input: T): K[];
271
+ /**
272
+ * Get object field value
273
+ * @param data Data
274
+ * @param key Property name
275
+ * @returns Value
276
+ */
277
+ function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
265
278
  /**
266
279
  * Get object id field value
267
280
  * @param data Data
268
281
  * @param key Property name
269
282
  * @returns Id value
270
283
  */
271
- function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
284
+ function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
272
285
  /**
273
286
  * Get object string field value
274
287
  * @param data Data
@@ -306,12 +306,12 @@ var DataTypes;
306
306
  }
307
307
  DataTypes.getEnumKeys = getEnumKeys;
308
308
  /**
309
- * Get object id field value
309
+ * Get object field value
310
310
  * @param data Data
311
311
  * @param key Property name
312
- * @returns Id value
312
+ * @returns Value
313
313
  */
314
- function getIdValue(data, key) {
314
+ function getValue(data, key) {
315
315
  if (data == null)
316
316
  return null;
317
317
  const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
@@ -321,6 +321,16 @@ var DataTypes;
321
321
  return value;
322
322
  return convert(value, 'string');
323
323
  }
324
+ DataTypes.getValue = getValue;
325
+ /**
326
+ * Get object id field value
327
+ * @param data Data
328
+ * @param key Property name
329
+ * @returns Id value
330
+ */
331
+ function getIdValue(data, key) {
332
+ return data[key];
333
+ }
324
334
  DataTypes.getIdValue = getIdValue;
325
335
  /**
326
336
  * Get object string field value
@@ -329,7 +339,7 @@ var DataTypes;
329
339
  * @returns String value
330
340
  */
331
341
  function getStringValue(data, key) {
332
- const value = getIdValue(data, key);
342
+ const value = getValue(data, key);
333
343
  if (typeof value === 'number')
334
344
  return value.toString();
335
345
  return value;
@@ -114,13 +114,13 @@ export declare namespace Utils {
114
114
  * @param minLength Minimum length
115
115
  * @returns Result
116
116
  */
117
- const isDigits: (input?: string | undefined, minLength?: number | undefined) => boolean;
117
+ const isDigits: (input?: string, minLength?: number) => boolean;
118
118
  /**
119
119
  * Is email string
120
120
  * @param input Input string
121
121
  * @returns Result
122
122
  */
123
- const isEmail: (input?: string | undefined) => boolean;
123
+ const isEmail: (input?: string) => boolean;
124
124
  /**
125
125
  * Join items as a string
126
126
  * @param items Items
@@ -188,7 +188,7 @@ export declare namespace Utils {
188
188
  * @param input Input string
189
189
  * @returns Result
190
190
  */
191
- const removeNonLetters: (input?: string | undefined) => string | undefined;
191
+ const removeNonLetters: (input?: string) => string | undefined;
192
192
  /**
193
193
  * Replace null or empty with default value
194
194
  * @param input Input string
@@ -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> | undefined) => void;
205
+ const setLabels: (source: {}, 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
@@ -168,6 +168,12 @@ export declare namespace DataTypes {
168
168
  */
169
169
  label: string | (() => string);
170
170
  };
171
+ /**
172
+ * Get specific type keys
173
+ */
174
+ type Keys<T, R = string | number> = {
175
+ [k in keyof T]: T[k] extends R ? k : never;
176
+ }[keyof T];
171
177
  /**
172
178
  * Get item id
173
179
  * @param item Item with id
@@ -262,13 +268,20 @@ export declare namespace DataTypes {
262
268
  * @returns Keys
263
269
  */
264
270
  function getEnumKeys<T extends EnumBase, K extends keyof T & string>(input: T): K[];
271
+ /**
272
+ * Get object field value
273
+ * @param data Data
274
+ * @param key Property name
275
+ * @returns Value
276
+ */
277
+ function getValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
265
278
  /**
266
279
  * Get object id field value
267
280
  * @param data Data
268
281
  * @param key Property name
269
282
  * @returns Id value
270
283
  */
271
- function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
284
+ function getIdValue<T extends {}, K extends Keys<T, string | number>>(data: T, key: K): T[K];
272
285
  /**
273
286
  * Get object string field value
274
287
  * @param data Data
@@ -303,12 +303,12 @@ export var DataTypes;
303
303
  }
304
304
  DataTypes.getEnumKeys = getEnumKeys;
305
305
  /**
306
- * Get object id field value
306
+ * Get object field value
307
307
  * @param data Data
308
308
  * @param key Property name
309
- * @returns Id value
309
+ * @returns Value
310
310
  */
311
- function getIdValue(data, key) {
311
+ function getValue(data, key) {
312
312
  if (data == null)
313
313
  return null;
314
314
  const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
@@ -318,6 +318,16 @@ export var DataTypes;
318
318
  return value;
319
319
  return convert(value, 'string');
320
320
  }
321
+ DataTypes.getValue = getValue;
322
+ /**
323
+ * Get object id field value
324
+ * @param data Data
325
+ * @param key Property name
326
+ * @returns Id value
327
+ */
328
+ function getIdValue(data, key) {
329
+ return data[key];
330
+ }
321
331
  DataTypes.getIdValue = getIdValue;
322
332
  /**
323
333
  * Get object string field value
@@ -326,7 +336,7 @@ export var DataTypes;
326
336
  * @returns String value
327
337
  */
328
338
  function getStringValue(data, key) {
329
- const value = getIdValue(data, key);
339
+ const value = getValue(data, key);
330
340
  if (typeof value === 'number')
331
341
  return value.toString();
332
342
  return value;
@@ -114,13 +114,13 @@ export declare namespace Utils {
114
114
  * @param minLength Minimum length
115
115
  * @returns Result
116
116
  */
117
- const isDigits: (input?: string | undefined, minLength?: number | undefined) => boolean;
117
+ const isDigits: (input?: string, minLength?: number) => boolean;
118
118
  /**
119
119
  * Is email string
120
120
  * @param input Input string
121
121
  * @returns Result
122
122
  */
123
- const isEmail: (input?: string | undefined) => boolean;
123
+ const isEmail: (input?: string) => boolean;
124
124
  /**
125
125
  * Join items as a string
126
126
  * @param items Items
@@ -188,7 +188,7 @@ export declare namespace Utils {
188
188
  * @param input Input string
189
189
  * @returns Result
190
190
  */
191
- const removeNonLetters: (input?: string | undefined) => string | undefined;
191
+ const removeNonLetters: (input?: string) => string | undefined;
192
192
  /**
193
193
  * Replace null or empty with default value
194
194
  * @param input Input string
@@ -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> | undefined) => void;
205
+ const setLabels: (source: {}, 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.39",
3
+ "version": "1.1.42",
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": "^27.5.1",
59
- "@typescript-eslint/eslint-plugin": "^5.25.0",
60
- "@typescript-eslint/parser": "^5.25.0",
61
- "eslint": "^8.16.0",
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",
62
62
  "eslint-config-airbnb-base": "^15.0.0",
63
63
  "eslint-plugin-import": "^2.26.0",
64
- "jest": "^28.1.0",
65
- "jest-environment-jsdom": "^28.1.0",
66
- "ts-jest": "^28.0.2",
67
- "typescript": "^4.6.4"
64
+ "jest": "^28.1.3",
65
+ "jest-environment-jsdom": "^28.1.3",
66
+ "ts-jest": "^28.0.7",
67
+ "typescript": "^4.7.4"
68
68
  }
69
69
  }
package/src/DataTypes.ts CHANGED
@@ -213,6 +213,13 @@ export namespace DataTypes {
213
213
  label: string | (() => string);
214
214
  };
215
215
 
216
+ /**
217
+ * Get specific type keys
218
+ */
219
+ export type Keys<T, R = string | number> = {
220
+ [k in keyof T]: T[k] extends R ? k : never;
221
+ }[keyof T];
222
+
216
223
  /**
217
224
  * Get item id
218
225
  * @param item Item with id
@@ -513,12 +520,12 @@ export namespace DataTypes {
513
520
  }
514
521
 
515
522
  /**
516
- * Get object id field value
523
+ * Get object field value
517
524
  * @param data Data
518
525
  * @param key Property name
519
- * @returns Id value
526
+ * @returns Value
520
527
  */
521
- export function getIdValue<T extends {}>(
528
+ export function getValue<T extends {}>(
522
529
  data: T | undefined | null,
523
530
  key: keyof T | string
524
531
  ): IdType | undefined | null {
@@ -530,6 +537,19 @@ export namespace DataTypes {
530
537
  return convert(value, 'string');
531
538
  }
532
539
 
540
+ /**
541
+ * Get object id field value
542
+ * @param data Data
543
+ * @param key Property name
544
+ * @returns Id value
545
+ */
546
+ export function getIdValue<
547
+ T extends {},
548
+ K extends Keys<T, string | number>
549
+ >(data: T, key: K): T[K] {
550
+ return data[key];
551
+ }
552
+
533
553
  /**
534
554
  * Get object string field value
535
555
  * @param data Data
@@ -540,7 +560,7 @@ export namespace DataTypes {
540
560
  data: T | undefined | null,
541
561
  key: keyof T | string
542
562
  ): string | undefined | null {
543
- const value = getIdValue(data, key);
563
+ const value = getValue(data, key);
544
564
  if (typeof value === 'number') return value.toString();
545
565
  return value;
546
566
  }