@etsoo/shared 1.2.62 → 1.2.63
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 +1 -1
- package/__tests__/ExtendUtils.ts +30 -0
- package/__tests__/Utils.ts +4 -0
- package/lib/cjs/ExtendUtils.d.ts +2 -1
- package/lib/cjs/ExtendUtils.js +6 -4
- package/lib/cjs/Utils.d.ts +1 -1
- package/lib/cjs/Utils.js +3 -1
- package/lib/mjs/ExtendUtils.d.ts +2 -1
- package/lib/mjs/ExtendUtils.js +6 -4
- package/lib/mjs/Utils.d.ts +1 -1
- package/lib/mjs/Utils.js +3 -1
- package/package.json +3 -3
- package/src/ExtendUtils.ts +16 -4
- package/src/Utils.ts +4 -2
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2004-
|
|
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/__tests__/ExtendUtils.ts
CHANGED
|
@@ -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();
|
package/__tests__/Utils.ts
CHANGED
|
@@ -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
|
});
|
package/lib/cjs/ExtendUtils.d.ts
CHANGED
|
@@ -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
|
package/lib/cjs/ExtendUtils.js
CHANGED
|
@@ -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
|
-
|
|
21
|
-
derivedCtor.prototype
|
|
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
|
});
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -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 =
|
|
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"
|
|
342
|
+
return typeof input === "function"
|
|
343
|
+
? input(...args)
|
|
344
|
+
: input;
|
|
343
345
|
};
|
|
344
346
|
/**
|
|
345
347
|
* Get time zone
|
package/lib/mjs/ExtendUtils.d.ts
CHANGED
|
@@ -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
|
package/lib/mjs/ExtendUtils.js
CHANGED
|
@@ -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
|
-
|
|
18
|
-
derivedCtor.prototype
|
|
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
|
});
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -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 =
|
|
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"
|
|
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.
|
|
3
|
+
"version": "1.2.63",
|
|
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.
|
|
43
|
-
"vitest": "^3.0.
|
|
42
|
+
"typescript": "^5.8.2",
|
|
43
|
+
"vitest": "^3.0.9"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"lodash.isequal": "^4.5.0"
|
package/src/ExtendUtils.ts
CHANGED
|
@@ -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(
|
|
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 (
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
):
|
|
502
|
-
return typeof input === "function"
|
|
501
|
+
): R => {
|
|
502
|
+
return typeof input === "function"
|
|
503
|
+
? input(...args)
|
|
504
|
+
: (input as unknown as R);
|
|
503
505
|
};
|
|
504
506
|
|
|
505
507
|
/**
|