@etsoo/shared 1.0.95 → 1.0.99

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
@@ -24,6 +24,7 @@ Data type definitions and type safe functions
24
24
  |Name|Description|
25
25
  |---:|---|
26
26
  |DataType|Data type enum|
27
+ |AddOrEditType|Add or edit conditional type for same data model|
27
28
  |Basic|Basic types, includes number, bigint, Date, boolean, string|
28
29
  |BasicArray|Basic type name array|
29
30
  |BasicConditional|Conditional type based on BasicNames|
@@ -58,8 +59,10 @@ Data type definitions and type safe functions
58
59
  |getEnumByValue|Get enum item from value|
59
60
  |getEnumKey|Get enum string literal type value|
60
61
  |getEnumKeys|Get Enum keys|
62
+ |getIdValue|Get object id field value|
61
63
  |getItemId|Get item id|
62
64
  |getItemLabel|Get item label|
65
+ |getStringValue|Get object string field value|
63
66
  |isBasicName|Check the type is a basic type or not (type guard)|
64
67
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
65
68
  |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[] = [
@@ -113,6 +113,13 @@ export declare namespace DataTypes {
113
113
  * Number and string combination id type
114
114
  */
115
115
  type IdType = number | string;
116
+ /**
117
+ * Add or edit conditional type for same data model
118
+ * Dynamic add changedFields for editing case
119
+ */
120
+ type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T & {
121
+ changedFields?: string[];
122
+ } : Partial<T>;
116
123
  /**
117
124
  * Enum value type
118
125
  */
@@ -245,6 +252,20 @@ export declare namespace DataTypes {
245
252
  * @returns Keys
246
253
  */
247
254
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
255
+ /**
256
+ * Get object id field value
257
+ * @param data Data
258
+ * @param key Property name
259
+ * @returns Id value
260
+ */
261
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
262
+ /**
263
+ * Get object string field value
264
+ * @param data Data
265
+ * @param key Property name
266
+ * @returns String value
267
+ */
268
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
248
269
  /**
249
270
  * Check the type is a basic type or not (type guard)
250
271
  * @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
@@ -113,6 +113,13 @@ export declare namespace DataTypes {
113
113
  * Number and string combination id type
114
114
  */
115
115
  type IdType = number | string;
116
+ /**
117
+ * Add or edit conditional type for same data model
118
+ * Dynamic add changedFields for editing case
119
+ */
120
+ type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T & {
121
+ changedFields?: string[];
122
+ } : Partial<T>;
116
123
  /**
117
124
  * Enum value type
118
125
  */
@@ -245,6 +252,20 @@ export declare namespace DataTypes {
245
252
  * @returns Keys
246
253
  */
247
254
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
255
+ /**
256
+ * Get object id field value
257
+ * @param data Data
258
+ * @param key Property name
259
+ * @returns Id value
260
+ */
261
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
262
+ /**
263
+ * Get object string field value
264
+ * @param data Data
265
+ * @param key Property name
266
+ * @returns String value
267
+ */
268
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
248
269
  /**
249
270
  * Check the type is a basic type or not (type guard)
250
271
  * @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.99",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,7 +54,7 @@
54
54
  "homepage": "https://github.com/ETSOO/Shared#readme",
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
- "@types/jest": "^27.0.3",
57
+ "@types/jest": "^27.4.0",
58
58
  "@typescript-eslint/eslint-plugin": "^5.8.0",
59
59
  "@typescript-eslint/parser": "^5.8.0",
60
60
  "eslint": "^8.5.0",
package/src/DataTypes.ts CHANGED
@@ -147,6 +147,14 @@ export namespace DataTypes {
147
147
  */
148
148
  export type IdType = number | string;
149
149
 
150
+ /**
151
+ * Add or edit conditional type for same data model
152
+ * Dynamic add changedFields for editing case
153
+ */
154
+ export type AddOrEditType<T, Editing extends boolean> = Editing extends true
155
+ ? T & { changedFields?: string[] }
156
+ : Partial<T>;
157
+
150
158
  /**
151
159
  * Enum value type
152
160
  */
@@ -491,6 +499,39 @@ export namespace DataTypes {
491
499
  .map((item) => <K>item);
492
500
  }
493
501
 
502
+ /**
503
+ * Get object id field value
504
+ * @param data Data
505
+ * @param key Property name
506
+ * @returns Id value
507
+ */
508
+ export function getIdValue<T extends {}>(
509
+ data: T | undefined | null,
510
+ key: keyof T | string
511
+ ): IdType | undefined | null {
512
+ if (data == null) return null;
513
+ const value =
514
+ typeof key === 'string' ? Reflect.get(data, key) : data[key];
515
+ if (value == null) return null;
516
+ if (typeof value === 'number') return value;
517
+ return convert(value, 'string');
518
+ }
519
+
520
+ /**
521
+ * Get object string field value
522
+ * @param data Data
523
+ * @param key Property name
524
+ * @returns String value
525
+ */
526
+ export function getStringValue<T extends {}>(
527
+ data: T | undefined | null,
528
+ key: keyof T | string
529
+ ): string | undefined | null {
530
+ const value = getIdValue(data, key);
531
+ if (typeof value === 'number') return value.toString();
532
+ return value;
533
+ }
534
+
494
535
  /**
495
536
  * Check the type is a basic type or not (type guard)
496
537
  * @param name Type name