@etsoo/shared 1.0.95 → 1.0.96

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
@@ -58,8 +58,10 @@ Data type definitions and type safe functions
58
58
  |getEnumByValue|Get enum item from value|
59
59
  |getEnumKey|Get enum string literal type value|
60
60
  |getEnumKeys|Get Enum keys|
61
+ |getIdValue|Get object id field value|
61
62
  |getItemId|Get item id|
62
63
  |getItemLabel|Get item label|
64
+ |getStringValue|Get object string field value|
63
65
  |isBasicName|Check the type is a basic type or not (type guard)|
64
66
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
65
67
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -87,6 +87,19 @@ test('Tests for getItemId', () => {
87
87
  expect(DataTypes.getItemId(items[2])).toBe('f123');
88
88
  });
89
89
 
90
+ test('Tests for getIdValue', () => {
91
+ const data = { id: 1, flag: true };
92
+ expect(DataTypes.getIdValue(data, 'id')).toBe(1);
93
+ expect(DataTypes.getIdValue(data, 'flag')).toBe('true');
94
+ expect(DataTypes.getIdValue(data, 'unknown')).toBeNull();
95
+ });
96
+
97
+ test('Tests for getStringValue', () => {
98
+ const data = { id: 1, flag: true };
99
+ expect(DataTypes.getStringValue(data, 'id')).toBe('1');
100
+ expect(DataTypes.getStringValue(data, 'flag')).toBe('true');
101
+ });
102
+
90
103
  test('Tests for getItemLabel', () => {
91
104
  // Arrange
92
105
  const items: DataTypes.IdLabelItem[] = [
@@ -245,6 +245,20 @@ export declare namespace DataTypes {
245
245
  * @returns Keys
246
246
  */
247
247
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
248
+ /**
249
+ * Get object id field value
250
+ * @param data Data
251
+ * @param key Property name
252
+ * @returns Id value
253
+ */
254
+ function getIdValue<T extends {}>(data: T, key: keyof T | string): IdType | undefined | null;
255
+ /**
256
+ * Get object string field value
257
+ * @param data Data
258
+ * @param key Property name
259
+ * @returns String value
260
+ */
261
+ function getStringValue<T extends {}>(data: T, key: keyof T | string): string | undefined | null;
248
262
  /**
249
263
  * Check the type is a basic type or not (type guard)
250
264
  * @param name Type name
@@ -308,6 +308,36 @@ var DataTypes;
308
308
  .map((item) => item);
309
309
  }
310
310
  DataTypes.getEnumKeys = getEnumKeys;
311
+ /**
312
+ * Get object id field value
313
+ * @param data Data
314
+ * @param key Property name
315
+ * @returns Id value
316
+ */
317
+ function getIdValue(data, key) {
318
+ if (data == null)
319
+ return null;
320
+ const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
321
+ if (value == null)
322
+ return null;
323
+ if (typeof value === 'number')
324
+ return value;
325
+ return convert(value, 'string');
326
+ }
327
+ DataTypes.getIdValue = getIdValue;
328
+ /**
329
+ * Get object string field value
330
+ * @param data Data
331
+ * @param key Property name
332
+ * @returns String value
333
+ */
334
+ function getStringValue(data, key) {
335
+ const value = getIdValue(data, key);
336
+ if (typeof value === 'number')
337
+ return value.toString();
338
+ return value;
339
+ }
340
+ DataTypes.getStringValue = getStringValue;
311
341
  /**
312
342
  * Check the type is a basic type or not (type guard)
313
343
  * @param name Type name
@@ -245,6 +245,20 @@ export declare namespace DataTypes {
245
245
  * @returns Keys
246
246
  */
247
247
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
248
+ /**
249
+ * Get object id field value
250
+ * @param data Data
251
+ * @param key Property name
252
+ * @returns Id value
253
+ */
254
+ function getIdValue<T extends {}>(data: T, key: keyof T | string): IdType | undefined | null;
255
+ /**
256
+ * Get object string field value
257
+ * @param data Data
258
+ * @param key Property name
259
+ * @returns String value
260
+ */
261
+ function getStringValue<T extends {}>(data: T, key: keyof T | string): string | undefined | null;
248
262
  /**
249
263
  * Check the type is a basic type or not (type guard)
250
264
  * @param name Type name
@@ -305,6 +305,36 @@ export var DataTypes;
305
305
  .map((item) => item);
306
306
  }
307
307
  DataTypes.getEnumKeys = getEnumKeys;
308
+ /**
309
+ * Get object id field value
310
+ * @param data Data
311
+ * @param key Property name
312
+ * @returns Id value
313
+ */
314
+ function getIdValue(data, key) {
315
+ if (data == null)
316
+ return null;
317
+ const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
318
+ if (value == null)
319
+ return null;
320
+ if (typeof value === 'number')
321
+ return value;
322
+ return convert(value, 'string');
323
+ }
324
+ DataTypes.getIdValue = getIdValue;
325
+ /**
326
+ * Get object string field value
327
+ * @param data Data
328
+ * @param key Property name
329
+ * @returns String value
330
+ */
331
+ function getStringValue(data, key) {
332
+ const value = getIdValue(data, key);
333
+ if (typeof value === 'number')
334
+ return value.toString();
335
+ return value;
336
+ }
337
+ DataTypes.getStringValue = getStringValue;
308
338
  /**
309
339
  * Check the type is a basic type or not (type guard)
310
340
  * @param name Type name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.95",
3
+ "version": "1.0.96",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DataTypes.ts CHANGED
@@ -491,6 +491,39 @@ export namespace DataTypes {
491
491
  .map((item) => <K>item);
492
492
  }
493
493
 
494
+ /**
495
+ * Get object id field value
496
+ * @param data Data
497
+ * @param key Property name
498
+ * @returns Id value
499
+ */
500
+ export function getIdValue<T extends {}>(
501
+ data: T,
502
+ key: keyof T | string
503
+ ): IdType | undefined | null {
504
+ if (data == null) return null;
505
+ const value =
506
+ typeof key === 'string' ? Reflect.get(data, key) : data[key];
507
+ if (value == null) return null;
508
+ if (typeof value === 'number') return value;
509
+ return convert(value, 'string');
510
+ }
511
+
512
+ /**
513
+ * Get object string field value
514
+ * @param data Data
515
+ * @param key Property name
516
+ * @returns String value
517
+ */
518
+ export function getStringValue<T extends {}>(
519
+ data: T,
520
+ key: keyof T | string
521
+ ): string | undefined | null {
522
+ const value = getIdValue(data, key);
523
+ if (typeof value === 'number') return value.toString();
524
+ return value;
525
+ }
526
+
494
527
  /**
495
528
  * Check the type is a basic type or not (type guard)
496
529
  * @param name Type name