@etsoo/shared 1.0.99 → 1.1.3

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
@@ -36,6 +36,7 @@ Data type definitions and type safe functions
36
36
  |EnumBase|Enum base type|
37
37
  |EnumValue|Enum value type|
38
38
  |ExtendedEnum|Extended type enum|
39
+ |Func<R>|Function type, R is return type|
39
40
  |HAlign|Horizontal align|
40
41
  |HAlignEnum|Horizontal align enum|
41
42
  |IdType|Number and string combination id type|
@@ -62,6 +63,7 @@ Data type definitions and type safe functions
62
63
  |getIdValue|Get object id field value|
63
64
  |getItemId|Get item id|
64
65
  |getItemLabel|Get item label|
66
+ |getResult|Get input function or value result|
65
67
  |getStringValue|Get object string field value|
66
68
  |isBasicName|Check the type is a basic type or not (type guard)|
67
69
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
@@ -133,3 +133,18 @@ 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
+ const result = Utils.getResult(input, true);
145
+ expect(result).toBe(1);
146
+ expect(Utils.getResult(input, false)).toBe(0);
147
+
148
+ const valueResult = Utils.getResult(inputNumber);
149
+ expect(valueResult).toBe(5);
150
+ });
@@ -117,9 +117,9 @@ export declare namespace DataTypes {
117
117
  * Add or edit conditional type for same data model
118
118
  * Dynamic add changedFields for editing case
119
119
  */
120
- type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T & {
120
+ type AddOrEditType<T, Editing extends boolean> = (Editing extends true ? T : Partial<T>) & {
121
121
  changedFields?: string[];
122
- } : Partial<T>;
122
+ };
123
123
  /**
124
124
  * Enum value type
125
125
  */
@@ -128,6 +128,10 @@ export declare namespace DataTypes {
128
128
  * Enum base type
129
129
  */
130
130
  type EnumBase = Record<string, EnumValue>;
131
+ /**
132
+ * Function type
133
+ */
134
+ type Func<R> = (...args: any[]) => R;
131
135
  /**
132
136
  * String key, unknown value Record
133
137
  */
@@ -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, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> : never) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
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
@@ -117,9 +117,9 @@ export declare namespace DataTypes {
117
117
  * Add or edit conditional type for same data model
118
118
  * Dynamic add changedFields for editing case
119
119
  */
120
- type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T & {
120
+ type AddOrEditType<T, Editing extends boolean> = (Editing extends true ? T : Partial<T>) & {
121
121
  changedFields?: string[];
122
- } : Partial<T>;
122
+ };
123
123
  /**
124
124
  * Enum value type
125
125
  */
@@ -128,6 +128,10 @@ export declare namespace DataTypes {
128
128
  * Enum base type
129
129
  */
130
130
  type EnumBase = Record<string, EnumValue>;
131
+ /**
132
+ * Function type
133
+ */
134
+ type Func<R> = (...args: any[]) => R;
131
135
  /**
132
136
  * String key, unknown value Record
133
137
  */
@@ -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, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> : never) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
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.99",
3
+ "version": "1.1.3",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,12 +55,12 @@
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.4.0",
58
- "@typescript-eslint/eslint-plugin": "^5.8.0",
59
- "@typescript-eslint/parser": "^5.8.0",
60
- "eslint": "^8.5.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
@@ -151,9 +151,10 @@ export namespace DataTypes {
151
151
  * Add or edit conditional type for same data model
152
152
  * Dynamic add changedFields for editing case
153
153
  */
154
- export type AddOrEditType<T, Editing extends boolean> = Editing extends true
155
- ? T & { changedFields?: string[] }
156
- : Partial<T>;
154
+ export type AddOrEditType<
155
+ T,
156
+ Editing extends boolean
157
+ > = (Editing extends true ? T : Partial<T>) & { changedFields?: string[] };
157
158
 
158
159
  /**
159
160
  * Enum value type
@@ -165,6 +166,11 @@ export namespace DataTypes {
165
166
  */
166
167
  export type EnumBase = Record<string, EnumValue>;
167
168
 
169
+ /**
170
+ * Function type
171
+ */
172
+ export type Func<R> = (...args: any[]) => R;
173
+
168
174
  /**
169
175
  * String key, unknown value Record
170
176
  */
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, T = DataTypes.Func<R> | R>(
197
+ input: T,
198
+ ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never
199
+ ): T extends DataTypes.Func<R> ? ReturnType<T> : T => {
200
+ return typeof input === 'function' ? input(...args) : input;
201
+ };
202
+
190
203
  /**
191
204
  * Get time zone
192
205
  * @returns Timezone