@etsoo/shared 1.2.62 → 1.2.64

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2004-2024 ETSOO ® (亿速思维 ®), https://etsoo.com, https://etsoo.nz
3
+ Copyright (c) 2004-2025 ETSOO ® (亿速思维 ®), https://etsoo.com, https://etsoo.nz
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -130,8 +130,6 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
130
130
  | Name | Description |
131
131
  | ------------------: | ---------------------------------------------------------------------- |
132
132
  | DataType | Data type enum |
133
- | AddAndEditType | Add and edit data type |
134
- | AddOrEditType | Add or edit conditional type |
135
133
  | addUrlParam | Add parameter to URL |
136
134
  | addUrlParams | Add parameters to URL |
137
135
  | Basic | Basic types, includes number, bigint, Date, boolean, string |
@@ -144,6 +142,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
144
142
  | CultureDefinition | Culture definition |
145
143
  | DI | Dynamic interface with multiple properties |
146
144
  | DIS | Dynamic interface with single property |
145
+ | EditType | Create edit type from adding type |
147
146
  | EnumBase | Enum base type |
148
147
  | EnumValue | Enum value type |
149
148
  | ExtendedEnum | Extended type enum |
@@ -202,39 +202,18 @@ test("Tests for jsonReplacer", () => {
202
202
  expect(json2).toBe('{"c":{"c2":false}}');
203
203
  });
204
204
 
205
- test("Tests for AddAndEditType", () => {
206
- type Entity = DataTypes.AddAndEditType<{
207
- id: number;
208
- name: string;
209
- age?: number;
210
- }>;
211
-
212
- const data1: Entity = { id: 1, name: "hello", changedFields: ["name"] };
213
- const data2: Entity = { id: undefined, name: "hello" };
214
- const data3: Entity = { name: "hello" };
215
-
216
- expect(data1.name).toBe(data2.name);
217
- expect(data2.name).toBe(data3.name);
218
- });
219
-
220
- test("Tests for AddOrEditType", () => {
221
- type Entity = {
222
- id: number;
205
+ test("Tests for EditType", () => {
206
+ type AddEntity = {
223
207
  name: string;
224
208
  age?: number;
225
209
  };
226
- type AddEntity = DataTypes.AddOrEditType<Entity, false>;
227
- type EditEntity = DataTypes.AddOrEditType<Entity, true>;
228
210
 
229
- const data1: AddEntity = { id: 1, name: "hello" };
230
- const data2: AddEntity = { id: undefined, name: "hello" };
231
- const data3: AddEntity = { name: "hello" };
211
+ type EditType = DataTypes.EditType<AddEntity>;
232
212
 
233
- const data4: EditEntity = { id: 1, name: "hello", changedFields: ["name"] };
213
+ const data1: EditType = { id: 1, name: "hello", changedFields: ["name"] };
214
+ const data2: EditType = { id: 1, name: "hello", age: 1 };
234
215
 
235
216
  expect(data1.name).toBe(data2.name);
236
- expect(data2.name).toBe(data3.name);
237
- expect(data3.name).toBe(data4.name);
238
217
  });
239
218
 
240
219
  test("Tests for BasicTemplate", () => {
@@ -20,9 +20,39 @@ test("Tests for applyMixins", () => {
20
20
  interface a extends b, c {}
21
21
  ExtendUtils.applyMixins(a, [b, c]);
22
22
  const item = new a();
23
+ expect(item.m()).toBe(1);
23
24
  expect(item.m2()).toBe("hello");
24
25
  });
25
26
 
27
+ test("Tests for applyMixins with override", () => {
28
+ class a {
29
+ m() {
30
+ return 0;
31
+ }
32
+ get id() {
33
+ return 1;
34
+ }
35
+ }
36
+ class b {
37
+ m(id: number) {
38
+ return id;
39
+ }
40
+ get id() {
41
+ return 2;
42
+ }
43
+ }
44
+
45
+ interface a extends b {
46
+ m(id: number): number;
47
+ }
48
+ ExtendUtils.applyMixins(a, [b], true);
49
+ const item = new a();
50
+ expect(item.id).toBe(2);
51
+ // As the method is overridden, the return value should be undefined
52
+ expect(item.m()).toBe(undefined);
53
+ expect(item.m(3)).toBe(3);
54
+ });
55
+
26
56
  test("Tests for delayedExecutor", () => {
27
57
  // Arrange
28
58
  const f = vi.fn();
@@ -304,12 +304,16 @@ test("Tests for getResult", () => {
304
304
  type test = ((visible: boolean) => number) | number;
305
305
  const input: test = (visible) => (visible ? 1 : 0);
306
306
  const inputNumber: test = 5;
307
+ const inputAny: test = input as any;
307
308
 
308
309
  // Act & assert
309
310
  const result = Utils.getResult(input, true);
310
311
  expect(result).toBe(1);
311
312
  expect(Utils.getResult(input, false)).toBe(0);
312
313
 
314
+ const result1 = Utils.getResult(inputAny, false);
315
+ expect(result1).toBe(0);
316
+
313
317
  const valueResult = Utils.getResult(inputNumber);
314
318
  expect(valueResult).toBe(5);
315
319
  });
@@ -139,25 +139,10 @@ export declare namespace DataTypes {
139
139
  */
140
140
  type Placement = keyof typeof PlacementEnum;
141
141
  /**
142
- * Add and edit data type
143
- * ChangedFields for editing case
144
- */
145
- type AddAndEditType<T extends {
146
- [key in D]: IdType;
147
- }, D extends string = "id"> = (Omit<T, D> & {
148
- [key in D]?: undefined | never;
149
- }) | (Partial<T> & Readonly<Pick<T, D>> & {
150
- changedFields?: (keyof T & string)[];
151
- });
152
- /**
153
- * Add or edit conditional type
154
- * ChangedFields for editing case
142
+ * Edit type from adding type
155
143
  */
156
- type AddOrEditType<T extends {
157
- [key in D]: IdType;
158
- }, // Entity modal
159
- E extends boolean, // Editing or not
160
- D extends string = "id"> = E extends false ? Optional<T, D> : Partial<T> & Readonly<Pick<T, D>> & {
144
+ type EditType<T, I extends IdType = number> = Partial<T> & {
145
+ id: I;
161
146
  changedFields?: (keyof T & string)[];
162
147
  };
163
148
  /**
@@ -8,8 +8,9 @@ export declare namespace ExtendUtils {
8
8
  * https://www.typescriptlang.org/docs/handbook/mixins.html#understanding-the-sample
9
9
  * @param derivedCtor Mixin target class
10
10
  * @param baseCtors Mixin base classes
11
+ * @param override Override or not
11
12
  */
12
- function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ function applyMixins(derivedCtor: any, baseCtors: any[], override?: boolean): void;
13
14
  /**
14
15
  * Create delayed executor
15
16
  * @param func Function
@@ -12,13 +12,15 @@ var ExtendUtils;
12
12
  * https://www.typescriptlang.org/docs/handbook/mixins.html#understanding-the-sample
13
13
  * @param derivedCtor Mixin target class
14
14
  * @param baseCtors Mixin base classes
15
+ * @param override Override or not
15
16
  */
16
- function applyMixins(derivedCtor, baseCtors) {
17
+ function applyMixins(derivedCtor, baseCtors, override = false) {
17
18
  baseCtors.forEach((baseCtor) => {
18
19
  Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
19
- if (name !== "constructor") {
20
- // eslint-disable-next-line no-param-reassign
21
- derivedCtor.prototype[name] = baseCtor.prototype[name];
20
+ if (name !== "constructor" &&
21
+ (override || !derivedCtor.prototype[name])) {
22
+ Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
23
+ Object.create(null));
22
24
  }
23
25
  });
24
26
  });
@@ -173,7 +173,7 @@ export declare namespace Utils {
173
173
  * @param args Arguments
174
174
  * @returns Result
175
175
  */
176
- export const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never | []) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
176
+ export const getResult: <R, T = DataTypes.Func<R> | R>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never | []) => R;
177
177
  /**
178
178
  * Get time zone
179
179
  * @param tz Default timezone, default is UTC
package/lib/cjs/Utils.js CHANGED
@@ -339,7 +339,9 @@ var Utils;
339
339
  * @returns Result
340
340
  */
341
341
  Utils.getResult = (input, ...args) => {
342
- return typeof input === "function" ? input(...args) : input;
342
+ return typeof input === "function"
343
+ ? input(...args)
344
+ : input;
343
345
  };
344
346
  /**
345
347
  * Get time zone
@@ -139,25 +139,10 @@ export declare namespace DataTypes {
139
139
  */
140
140
  type Placement = keyof typeof PlacementEnum;
141
141
  /**
142
- * Add and edit data type
143
- * ChangedFields for editing case
144
- */
145
- type AddAndEditType<T extends {
146
- [key in D]: IdType;
147
- }, D extends string = "id"> = (Omit<T, D> & {
148
- [key in D]?: undefined | never;
149
- }) | (Partial<T> & Readonly<Pick<T, D>> & {
150
- changedFields?: (keyof T & string)[];
151
- });
152
- /**
153
- * Add or edit conditional type
154
- * ChangedFields for editing case
142
+ * Edit type from adding type
155
143
  */
156
- type AddOrEditType<T extends {
157
- [key in D]: IdType;
158
- }, // Entity modal
159
- E extends boolean, // Editing or not
160
- D extends string = "id"> = E extends false ? Optional<T, D> : Partial<T> & Readonly<Pick<T, D>> & {
144
+ type EditType<T, I extends IdType = number> = Partial<T> & {
145
+ id: I;
161
146
  changedFields?: (keyof T & string)[];
162
147
  };
163
148
  /**
@@ -8,8 +8,9 @@ export declare namespace ExtendUtils {
8
8
  * https://www.typescriptlang.org/docs/handbook/mixins.html#understanding-the-sample
9
9
  * @param derivedCtor Mixin target class
10
10
  * @param baseCtors Mixin base classes
11
+ * @param override Override or not
11
12
  */
12
- function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ function applyMixins(derivedCtor: any, baseCtors: any[], override?: boolean): void;
13
14
  /**
14
15
  * Create delayed executor
15
16
  * @param func Function
@@ -9,13 +9,15 @@ export var ExtendUtils;
9
9
  * https://www.typescriptlang.org/docs/handbook/mixins.html#understanding-the-sample
10
10
  * @param derivedCtor Mixin target class
11
11
  * @param baseCtors Mixin base classes
12
+ * @param override Override or not
12
13
  */
13
- function applyMixins(derivedCtor, baseCtors) {
14
+ function applyMixins(derivedCtor, baseCtors, override = false) {
14
15
  baseCtors.forEach((baseCtor) => {
15
16
  Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
16
- if (name !== "constructor") {
17
- // eslint-disable-next-line no-param-reassign
18
- derivedCtor.prototype[name] = baseCtor.prototype[name];
17
+ if (name !== "constructor" &&
18
+ (override || !derivedCtor.prototype[name])) {
19
+ Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
20
+ Object.create(null));
19
21
  }
20
22
  });
21
23
  });
@@ -173,7 +173,7 @@ export declare namespace Utils {
173
173
  * @param args Arguments
174
174
  * @returns Result
175
175
  */
176
- export const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never | []) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
176
+ export const getResult: <R, T = DataTypes.Func<R> | R>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never | []) => R;
177
177
  /**
178
178
  * Get time zone
179
179
  * @param tz Default timezone, default is UTC
package/lib/mjs/Utils.js CHANGED
@@ -333,7 +333,9 @@ export var Utils;
333
333
  * @returns Result
334
334
  */
335
335
  Utils.getResult = (input, ...args) => {
336
- return typeof input === "function" ? input(...args) : input;
336
+ return typeof input === "function"
337
+ ? input(...args)
338
+ : input;
337
339
  };
338
340
  /**
339
341
  * Get time zone
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.62",
3
+ "version": "1.2.64",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -39,8 +39,8 @@
39
39
  "@types/lodash.isequal": "^4.5.8",
40
40
  "@vitejs/plugin-react": "^4.3.4",
41
41
  "jsdom": "^26.0.0",
42
- "typescript": "^5.7.3",
43
- "vitest": "^3.0.7"
42
+ "typescript": "^5.8.2",
43
+ "vitest": "^3.1.1"
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash.isequal": "^4.5.0"
package/src/DataTypes.ts CHANGED
@@ -183,29 +183,12 @@ export namespace DataTypes {
183
183
  export type Placement = keyof typeof PlacementEnum;
184
184
 
185
185
  /**
186
- * Add and edit data type
187
- * ChangedFields for editing case
188
- */
189
- export type AddAndEditType<
190
- T extends { [key in D]: IdType },
191
- D extends string = "id"
192
- > =
193
- | (Omit<T, D> & { [key in D]?: undefined | never })
194
- | (Partial<T> &
195
- Readonly<Pick<T, D>> & { changedFields?: (keyof T & string)[] });
196
-
197
- /**
198
- * Add or edit conditional type
199
- * ChangedFields for editing case
200
- */
201
- export type AddOrEditType<
202
- T extends { [key in D]: IdType }, // Entity modal
203
- E extends boolean, // Editing or not
204
- D extends string = "id" // Default is 'id' field
205
- > = E extends false
206
- ? Optional<T, D>
207
- : Partial<T> &
208
- Readonly<Pick<T, D>> & { changedFields?: (keyof T & string)[] };
186
+ * Edit type from adding type
187
+ */
188
+ export type EditType<T, I extends IdType = number> = Partial<T> & {
189
+ id: I;
190
+ changedFields?: (keyof T & string)[];
191
+ };
209
192
 
210
193
  /**
211
194
  * Key collection, like { key1: {}, key2: {} }
@@ -11,13 +11,25 @@ export namespace ExtendUtils {
11
11
  * https://www.typescriptlang.org/docs/handbook/mixins.html#understanding-the-sample
12
12
  * @param derivedCtor Mixin target class
13
13
  * @param baseCtors Mixin base classes
14
+ * @param override Override or not
14
15
  */
15
- export function applyMixins(derivedCtor: any, baseCtors: any[]) {
16
+ export function applyMixins(
17
+ derivedCtor: any,
18
+ baseCtors: any[],
19
+ override = false
20
+ ) {
16
21
  baseCtors.forEach((baseCtor) => {
17
22
  Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
18
- if (name !== "constructor") {
19
- // eslint-disable-next-line no-param-reassign
20
- derivedCtor.prototype[name] = baseCtor.prototype[name];
23
+ if (
24
+ name !== "constructor" &&
25
+ (override || !derivedCtor.prototype[name])
26
+ ) {
27
+ Object.defineProperty(
28
+ derivedCtor.prototype,
29
+ name,
30
+ Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
31
+ Object.create(null)
32
+ );
21
33
  }
22
34
  });
23
35
  });
package/src/Utils.ts CHANGED
@@ -498,8 +498,10 @@ export namespace Utils {
498
498
  export const getResult = <R, T = DataTypes.Func<R> | R>(
499
499
  input: T,
500
500
  ...args: T extends DataTypes.Func<R> ? Parameters<typeof input> : never | []
501
- ): T extends DataTypes.Func<R> ? ReturnType<T> : T => {
502
- return typeof input === "function" ? input(...args) : input;
501
+ ): R => {
502
+ return typeof input === "function"
503
+ ? input(...args)
504
+ : (input as unknown as R);
503
505
  };
504
506
 
505
507
  /**