@etsoo/shared 1.0.96 → 1.1.0

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|
@@ -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 : Partial<T>) & {
121
+ changedFields?: string[];
122
+ };
116
123
  /**
117
124
  * Enum value type
118
125
  */
@@ -251,14 +258,14 @@ export declare namespace DataTypes {
251
258
  * @param key Property name
252
259
  * @returns Id value
253
260
  */
254
- function getIdValue<T extends {}>(data: T, key: keyof T | string): IdType | undefined | null;
261
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
255
262
  /**
256
263
  * Get object string field value
257
264
  * @param data Data
258
265
  * @param key Property name
259
266
  * @returns String value
260
267
  */
261
- function getStringValue<T extends {}>(data: T, key: keyof T | string): string | undefined | null;
268
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
262
269
  /**
263
270
  * Check the type is a basic type or not (type guard)
264
271
  * @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 : Partial<T>) & {
121
+ changedFields?: string[];
122
+ };
116
123
  /**
117
124
  * Enum value type
118
125
  */
@@ -251,14 +258,14 @@ export declare namespace DataTypes {
251
258
  * @param key Property name
252
259
  * @returns Id value
253
260
  */
254
- function getIdValue<T extends {}>(data: T, key: keyof T | string): IdType | undefined | null;
261
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
255
262
  /**
256
263
  * Get object string field value
257
264
  * @param data Data
258
265
  * @param key Property name
259
266
  * @returns String value
260
267
  */
261
- function getStringValue<T extends {}>(data: T, key: keyof T | string): string | undefined | null;
268
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
262
269
  /**
263
270
  * Check the type is a basic type or not (type guard)
264
271
  * @param name Type name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.96",
3
+ "version": "1.1.0",
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,15 @@ 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<
155
+ T,
156
+ Editing extends boolean
157
+ > = (Editing extends true ? T : Partial<T>) & { changedFields?: string[] };
158
+
150
159
  /**
151
160
  * Enum value type
152
161
  */
@@ -498,7 +507,7 @@ export namespace DataTypes {
498
507
  * @returns Id value
499
508
  */
500
509
  export function getIdValue<T extends {}>(
501
- data: T,
510
+ data: T | undefined | null,
502
511
  key: keyof T | string
503
512
  ): IdType | undefined | null {
504
513
  if (data == null) return null;
@@ -516,7 +525,7 @@ export namespace DataTypes {
516
525
  * @returns String value
517
526
  */
518
527
  export function getStringValue<T extends {}>(
519
- data: T,
528
+ data: T | undefined | null,
520
529
  key: keyof T | string
521
530
  ): string | undefined | null {
522
531
  const value = getIdValue(data, key);