@etsoo/shared 1.0.97 → 1.1.1

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|
@@ -61,6 +62,7 @@ Data type definitions and type safe functions
61
62
  |getIdValue|Get object id field value|
62
63
  |getItemId|Get item id|
63
64
  |getItemLabel|Get item label|
65
+ |getResult|Get input function or value result|
64
66
  |getStringValue|Get object string field value|
65
67
  |isBasicName|Check the type is a basic type or not (type guard)|
66
68
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
@@ -133,3 +133,15 @@ test('Test for snakeNameToWord', () => {
133
133
  test('Tests for mergeClasses', () => {
134
134
  expect(Utils.mergeClasses('a', '', 'b ', undefined, 'c')).toBe('a b c');
135
135
  });
136
+
137
+ test('Tests for getResult', () => {
138
+ // Arrange
139
+ type test = ((visible: boolean) => number) | number;
140
+ const input: test = (visible) => (visible ? 1 : 0);
141
+ const inputNumber: test = 5;
142
+
143
+ // Act & assert
144
+ expect(Utils.getResult(input, true)).toBe(1);
145
+ expect(Utils.getResult(input, false)).toBe(0);
146
+ expect(Utils.getResult(inputNumber)).toBe(5);
147
+ });
@@ -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
  */
@@ -69,6 +69,13 @@ export declare namespace Utils {
69
69
  * @returns
70
70
  */
71
71
  function getDataChanges(input: {}, initData: {}, ignoreFields?: string[]): string[];
72
+ /**
73
+ * Get input function or value result
74
+ * @param input Input function or value
75
+ * @param args Arguments
76
+ * @returns Result
77
+ */
78
+ const getResult: <R, F extends R | ((...args: unknown[]) => R)>(input: F, ...args: unknown[]) => R;
72
79
  /**
73
80
  * Get time zone
74
81
  * @returns Timezone
package/lib/cjs/Utils.js CHANGED
@@ -125,6 +125,15 @@ var Utils;
125
125
  return changes;
126
126
  }
127
127
  Utils.getDataChanges = getDataChanges;
128
+ /**
129
+ * Get input function or value result
130
+ * @param input Input function or value
131
+ * @param args Arguments
132
+ * @returns Result
133
+ */
134
+ Utils.getResult = (input, ...args) => {
135
+ return typeof input === 'function' ? input(...args) : input;
136
+ };
128
137
  /**
129
138
  * Get time zone
130
139
  * @returns Timezone
@@ -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
  */
@@ -69,6 +69,13 @@ export declare namespace Utils {
69
69
  * @returns
70
70
  */
71
71
  function getDataChanges(input: {}, initData: {}, ignoreFields?: string[]): string[];
72
+ /**
73
+ * Get input function or value result
74
+ * @param input Input function or value
75
+ * @param args Arguments
76
+ * @returns Result
77
+ */
78
+ const getResult: <R, F extends R | ((...args: unknown[]) => R)>(input: F, ...args: unknown[]) => R;
72
79
  /**
73
80
  * Get time zone
74
81
  * @returns Timezone
package/lib/mjs/Utils.js CHANGED
@@ -122,6 +122,15 @@ export var Utils;
122
122
  return changes;
123
123
  }
124
124
  Utils.getDataChanges = getDataChanges;
125
+ /**
126
+ * Get input function or value result
127
+ * @param input Input function or value
128
+ * @param args Arguments
129
+ * @returns Result
130
+ */
131
+ Utils.getResult = (input, ...args) => {
132
+ return typeof input === 'function' ? input(...args) : input;
133
+ };
125
134
  /**
126
135
  * Get time zone
127
136
  * @returns Timezone
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.97",
3
+ "version": "1.1.1",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,13 +54,13 @@
54
54
  "homepage": "https://github.com/ETSOO/Shared#readme",
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
- "@types/jest": "^27.0.3",
58
- "@typescript-eslint/eslint-plugin": "^5.8.0",
59
- "@typescript-eslint/parser": "^5.8.0",
60
- "eslint": "^8.5.0",
57
+ "@types/jest": "^27.4.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.9.0",
59
+ "@typescript-eslint/parser": "^5.9.0",
60
+ "eslint": "^8.6.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
- "eslint-plugin-import": "^2.25.3",
63
- "jest": "^27.4.5",
62
+ "eslint-plugin-import": "^2.25.4",
63
+ "jest": "^27.4.7",
64
64
  "ts-jest": "^27.1.2",
65
65
  "typescript": "^4.5.4"
66
66
  }
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
  */
package/src/Utils.ts CHANGED
@@ -187,6 +187,19 @@ export namespace Utils {
187
187
  return changes;
188
188
  }
189
189
 
190
+ /**
191
+ * Get input function or value result
192
+ * @param input Input function or value
193
+ * @param args Arguments
194
+ * @returns Result
195
+ */
196
+ export const getResult = <R, F extends ((...args: unknown[]) => R) | R>(
197
+ input: F,
198
+ ...args: unknown[]
199
+ ): R => {
200
+ return typeof input === 'function' ? input(...args) : input;
201
+ };
202
+
190
203
  /**
191
204
  * Get time zone
192
205
  * @returns Timezone