@bemoje/fn 0.0.2 → 1.0.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/index.d.ts +50 -145
- package/index.mjs +74 -96
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -14,35 +14,18 @@ type Increment<T extends number> = Subtract<T, -1>;
|
|
|
14
14
|
|
|
15
15
|
type RemoveArrayElement<T extends any[], Index extends number> = ArraySplice<T, Index, 1, []>;
|
|
16
16
|
|
|
17
|
-
type ParametersWithout<T extends (...args: any[]) => any, Index extends number> = RemoveArrayElement<
|
|
18
|
-
Parameters<T>,
|
|
19
|
-
Index
|
|
20
|
-
>;
|
|
17
|
+
type ParametersWithout<T extends (...args: any[]) => any, Index extends number> = RemoveArrayElement<Parameters<T>, Index>;
|
|
21
18
|
|
|
22
19
|
/**
|
|
23
20
|
* Removes elements from an array at specified indices.
|
|
24
21
|
*/
|
|
25
|
-
type RemoveArrayElements<T extends any[], Indices extends number[], Offset extends number = 0> = Indices extends [
|
|
26
|
-
infer First extends number,
|
|
27
|
-
...infer Rest extends number[],
|
|
28
|
-
]
|
|
29
|
-
? RemoveArrayElements<
|
|
30
|
-
RemoveArrayElement<T, Subtract<First, Offset>>,
|
|
31
|
-
Rest,
|
|
32
|
-
Offset extends number ? Increment<Offset> : never
|
|
33
|
-
>
|
|
34
|
-
: T;
|
|
22
|
+
type RemoveArrayElements<T extends any[], Indices extends number[], Offset extends number = 0> = Indices extends [infer First extends number, ...infer Rest extends number[]] ? RemoveArrayElements<RemoveArrayElement<T, Subtract<First, Offset>>, Rest, Offset extends number ? Increment<Offset> : never> : T;
|
|
35
23
|
|
|
36
24
|
/**
|
|
37
25
|
* Binds a specified argument to the provided function, returning a new function that requires
|
|
38
26
|
* only the remaining arguments at call time.
|
|
39
27
|
*/
|
|
40
|
-
declare function bindArg<
|
|
41
|
-
Ret extends (...args: ParametersWithout<T, Index>) => ReturnType<T>,
|
|
42
|
-
Index extends number = number,
|
|
43
|
-
Value = unknown,
|
|
44
|
-
T extends (...args: any[]) => any = (...args: any[]) => any,
|
|
45
|
-
>(fn: T, boundArgIndex: Index, boundArgValue: Value): Ret;
|
|
28
|
+
declare function bindArg<Ret extends (...args: ParametersWithout<T, Index>) => ReturnType<T>, Index extends number = number, Value = unknown, T extends (...args: any[]) => any = (...args: any[]) => any>(fn: T, boundArgIndex: Index, boundArgValue: Value): Ret;
|
|
46
29
|
|
|
47
30
|
/**
|
|
48
31
|
* Binds specified arguments to the provided function, returning a new function that requires
|
|
@@ -56,21 +39,13 @@ declare function bindArg<
|
|
|
56
39
|
*
|
|
57
40
|
* @returns A function that, when invoked, applies both bound and unbound arguments in the correct order.
|
|
58
41
|
*/
|
|
59
|
-
declare function bindArgs<
|
|
60
|
-
T extends (...args: Any[]) => Any,
|
|
61
|
-
BoundArgs extends Partial<TupleToObject<Parameters<T>>>,
|
|
62
|
-
>(
|
|
63
|
-
fn: T,
|
|
64
|
-
boundArgs: BoundArgs,
|
|
65
|
-
): (...args: RemoveArrayElements<Parameters<T>, UnionToTuple<Extract<keyof BoundArgs, number>>>) => ReturnType<T>;
|
|
42
|
+
declare function bindArgs<T extends (...args: Any[]) => Any, BoundArgs extends Partial<TupleToObject<Parameters<T>>>>(fn: T, boundArgs: BoundArgs): (...args: RemoveArrayElements<Parameters<T>, UnionToTuple<Extract<keyof BoundArgs, number>>>) => ReturnType<T>;
|
|
66
43
|
|
|
67
44
|
/**
|
|
68
45
|
* Converts a function from a class method by by making the first argument take the place of the 'this' context.
|
|
69
46
|
* The reverse of @see thisify.
|
|
70
47
|
*/
|
|
71
|
-
declare function dethisify<T extends object, Args extends Any[], Ret>(
|
|
72
|
-
fn: (this: T, ...args: Args) => Ret,
|
|
73
|
-
): (target: T, ...args: Args) => Ret;
|
|
48
|
+
declare function dethisify<T extends object, Args extends Any[], Ret>(fn: (this: T, ...args: Args) => Ret): (target: T, ...args: Args) => Ret;
|
|
74
49
|
|
|
75
50
|
/**
|
|
76
51
|
* Wraps a function so that the given @see IFunctionSpyStrategy will be applied.
|
|
@@ -78,165 +53,95 @@ declare function dethisify<T extends object, Args extends Any[], Ret>(
|
|
|
78
53
|
* @param spy The strategy to apply.
|
|
79
54
|
* @param options Options.
|
|
80
55
|
*/
|
|
81
|
-
declare function functionSpy<T, Data>(
|
|
82
|
-
func: TFunction,
|
|
83
|
-
spy: IFunctionSpyStrategy<T, Data>,
|
|
84
|
-
options?: FunctionSpyOptions<T, Data>,
|
|
85
|
-
): TFunction;
|
|
56
|
+
declare function functionSpy<T, Data>(func: TFunction, spy: IFunctionSpyStrategy<T, Data>, options?: FunctionSpyOptions<T, Data>): TFunction;
|
|
86
57
|
interface FunctionSpyOptions<T, Data> {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Whether the function is async.
|
|
60
|
+
* If provided, execution is a bit faster.
|
|
61
|
+
*/
|
|
62
|
+
async?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Predicate function that determines whether to skip the spy's actions for a given function call.
|
|
65
|
+
*/
|
|
66
|
+
ignore?: IgnorePredicate<T, Data>;
|
|
96
67
|
}
|
|
97
68
|
/**
|
|
98
69
|
* @see functionSpy
|
|
99
70
|
*/
|
|
100
71
|
interface IFunctionSpyStrategy<T, Data> {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Action to perform before the function is invoked.
|
|
74
|
+
* @param thisContext The context in which the function is invoked.
|
|
75
|
+
* @param args The arguments passed to the function. Modify this array to change the arguments passed to the function.
|
|
76
|
+
* @returns Data to be passed to @see onReturn.
|
|
77
|
+
*/
|
|
78
|
+
onInvoke<Args extends Any[]>(thisContext: T, args: Args): Data;
|
|
79
|
+
/**
|
|
80
|
+
* Action to perform before the function returns.
|
|
81
|
+
* @param data The data returned by @see onInvoke.
|
|
82
|
+
* @param retval The return value of the function.
|
|
83
|
+
* @returns The value to be returned by the wrapped function.
|
|
84
|
+
*/
|
|
85
|
+
onReturn<Ret>(data: Data, retval: Ret): Ret;
|
|
115
86
|
}
|
|
116
87
|
type IgnorePredicate<T, Data> = (func: TFunction, spy: IFunctionSpyStrategy<T, Data>, thisContext?: T) => boolean;
|
|
117
88
|
|
|
118
89
|
/**
|
|
119
90
|
* Preserves the name and length of a function or class constructor
|
|
120
91
|
*/
|
|
121
|
-
declare function preserveNameAndLength<
|
|
122
|
-
S extends ((...args: Any[]) => Any) | (new (...args: Any[]) => Any),
|
|
123
|
-
F extends ((...args: Any[]) => Any) | (new (...args: Any[]) => Any),
|
|
124
|
-
>(source: S, target: F, adjustLengthBy?: number): F;
|
|
92
|
+
declare function preserveNameAndLength<S extends ((...args: Any[]) => Any) | (new (...args: Any[]) => Any), F extends ((...args: Any[]) => Any) | (new (...args: Any[]) => Any)>(source: S, target: F, adjustLengthBy?: number): F;
|
|
125
93
|
|
|
126
94
|
/**
|
|
127
95
|
* Set the length of a function.
|
|
128
96
|
*/
|
|
129
|
-
declare function setLength<T extends object>(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
| {
|
|
133
|
-
length: number;
|
|
134
|
-
},
|
|
135
|
-
target: T,
|
|
136
|
-
): T;
|
|
97
|
+
declare function setLength<T extends object>(length: number | {
|
|
98
|
+
length: number;
|
|
99
|
+
}, target: T): T;
|
|
137
100
|
|
|
138
101
|
/**
|
|
139
102
|
* Set the name of a function.
|
|
140
103
|
*/
|
|
141
|
-
declare function setName<T extends object>(
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
| {
|
|
145
|
-
name: string;
|
|
146
|
-
},
|
|
147
|
-
target: T,
|
|
148
|
-
): T;
|
|
104
|
+
declare function setName<T extends object>(name: string | {
|
|
105
|
+
name: string;
|
|
106
|
+
}, target: T): T;
|
|
149
107
|
|
|
150
108
|
/**
|
|
151
109
|
* Returns a function that redirects or 'proxies' the 'this' context of the input function
|
|
152
110
|
* to a property of a given key.
|
|
153
111
|
*/
|
|
154
|
-
declare function thisProxy<ThisTarget extends This, This extends object, Args extends AnyArgs, Ret>(
|
|
155
|
-
fn: (this: This, ...args: Args) => Ret,
|
|
156
|
-
proxy: PropertyKey,
|
|
157
|
-
): (this: ThisTarget, ...args: Args) => Ret;
|
|
112
|
+
declare function thisProxy<ThisTarget extends This, This extends object, Args extends AnyArgs, Ret>(fn: (this: This, ...args: Args) => Ret, proxy: PropertyKey): (this: ThisTarget, ...args: Args) => Ret;
|
|
158
113
|
/**
|
|
159
114
|
* Returns a function that redirects or 'proxies' the 'this' context of the input function
|
|
160
115
|
* to a callback that returns the target object.
|
|
161
116
|
*/
|
|
162
|
-
declare function thisProxy<ThisTarget extends This, This extends object, Args extends AnyArgs, Ret>(
|
|
163
|
-
fn: (this: This, ...args: Args) => Ret,
|
|
164
|
-
proxy: (object: ThisTarget) => This,
|
|
165
|
-
): (this: ThisTarget, ...args: Args) => Ret;
|
|
117
|
+
declare function thisProxy<ThisTarget extends This, This extends object, Args extends AnyArgs, Ret>(fn: (this: This, ...args: Args) => Ret, proxy: (object: ThisTarget) => This): (this: ThisTarget, ...args: Args) => Ret;
|
|
166
118
|
type AnyArgs = any[];
|
|
167
119
|
|
|
168
120
|
/**
|
|
169
121
|
* Converts a function to a class method by making the 'this' context the first argument.
|
|
170
122
|
*/
|
|
171
|
-
declare function thisify<T, Args extends Any[], Ret>(
|
|
172
|
-
|
|
173
|
-
): (this: T, ...args: Args) => Ret;
|
|
174
|
-
type Thisify<T extends object, F extends (target: T, ...args: any[]) => Any> = (
|
|
175
|
-
this: T,
|
|
176
|
-
...args: ArraySlice<Parameters<F>, 1>
|
|
177
|
-
) => ReturnType<F>;
|
|
123
|
+
declare function thisify<T, Args extends Any[], Ret>(fn: (target: T, ...args: Args) => Ret): (this: T, ...args: Args) => Ret;
|
|
124
|
+
type Thisify<T extends object, F extends (target: T, ...args: any[]) => Any> = (this: T, ...args: ArraySlice<Parameters<F>, 1>) => ReturnType<F>;
|
|
178
125
|
|
|
179
126
|
/**
|
|
180
127
|
* Wraps a function to transform its return value using a transform function.
|
|
181
128
|
*/
|
|
182
|
-
declare function transformReturnValue<T, Args extends any[], Ret, NewRet>(
|
|
183
|
-
fn: (this: T, ...args: Args) => Ret,
|
|
184
|
-
transform: (value: Ret) => NewRet,
|
|
185
|
-
): (this: T, ...args: Args) => NewRet;
|
|
129
|
+
declare function transformReturnValue<T, Args extends any[], Ret, NewRet>(fn: (this: T, ...args: Args) => Ret, transform: (value: Ret) => NewRet): (this: T, ...args: Args) => NewRet;
|
|
186
130
|
|
|
187
131
|
/**
|
|
188
132
|
* Wrap methods, getters and setters of an object with custom logic.
|
|
189
133
|
*/
|
|
190
134
|
declare function wrapMethods<T extends object>(target: T, strat: WrapMethodsStrategy<T>): T;
|
|
191
135
|
type DescriptorMethodType = 'get' | 'set' | 'method';
|
|
192
|
-
type MethodFilter<T extends object = object> = (
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
descriptor: PropertyDescriptor,
|
|
197
|
-
) => boolean;
|
|
198
|
-
type MethodWrapper<T extends object = object> = (
|
|
199
|
-
target: T,
|
|
200
|
-
key: string | symbol,
|
|
201
|
-
method: AnyFunction,
|
|
202
|
-
) => AnyFunction | undefined;
|
|
203
|
-
type GetterWrapper<T extends object = object> = (
|
|
204
|
-
target: T,
|
|
205
|
-
key: string | symbol,
|
|
206
|
-
getter: AnyGetter,
|
|
207
|
-
) => AnyGetter | undefined;
|
|
208
|
-
type SetterWrapper<T extends object = object> = (
|
|
209
|
-
target: T,
|
|
210
|
-
key: string | symbol,
|
|
211
|
-
setter: AnySetter,
|
|
212
|
-
) => AnySetter | undefined;
|
|
136
|
+
type MethodFilter<T extends object = object> = (target: T, key: string | symbol, type: DescriptorMethodType, descriptor: PropertyDescriptor) => boolean;
|
|
137
|
+
type MethodWrapper<T extends object = object> = (target: T, key: string | symbol, method: AnyFunction) => AnyFunction | undefined;
|
|
138
|
+
type GetterWrapper<T extends object = object> = (target: T, key: string | symbol, getter: AnyGetter) => AnyGetter | undefined;
|
|
139
|
+
type SetterWrapper<T extends object = object> = (target: T, key: string | symbol, setter: AnySetter) => AnySetter | undefined;
|
|
213
140
|
interface WrapMethodsStrategy<T extends object = object> {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
141
|
+
filter?: MethodFilter<T>;
|
|
142
|
+
onGetter?: GetterWrapper<T>;
|
|
143
|
+
onSetter?: SetterWrapper<T>;
|
|
144
|
+
onMethod?: MethodWrapper<T>;
|
|
218
145
|
}
|
|
219
146
|
|
|
220
|
-
export {
|
|
221
|
-
type DescriptorMethodType,
|
|
222
|
-
type FunctionSpyOptions,
|
|
223
|
-
type GetterWrapper,
|
|
224
|
-
type IFunctionSpyStrategy,
|
|
225
|
-
type IgnorePredicate,
|
|
226
|
-
type MethodFilter,
|
|
227
|
-
type MethodWrapper,
|
|
228
|
-
type SetterWrapper,
|
|
229
|
-
type Thisify,
|
|
230
|
-
type WrapMethodsStrategy,
|
|
231
|
-
bindArg,
|
|
232
|
-
bindArgs,
|
|
233
|
-
dethisify,
|
|
234
|
-
functionSpy,
|
|
235
|
-
preserveNameAndLength,
|
|
236
|
-
setLength,
|
|
237
|
-
setName,
|
|
238
|
-
thisProxy,
|
|
239
|
-
thisify,
|
|
240
|
-
transformReturnValue,
|
|
241
|
-
wrapMethods,
|
|
242
|
-
};
|
|
147
|
+
export { type DescriptorMethodType, type FunctionSpyOptions, type GetterWrapper, type IFunctionSpyStrategy, type IgnorePredicate, type MethodFilter, type MethodWrapper, type SetterWrapper, type Thisify, type WrapMethodsStrategy, bindArg, bindArgs, dethisify, functionSpy, preserveNameAndLength, setLength, setName, thisProxy, thisify, transformReturnValue, wrapMethods };
|
package/index.mjs
CHANGED
|
@@ -3,36 +3,30 @@ import { isAsyncFunction } from 'util/types';
|
|
|
3
3
|
import { isFunction } from 'es-toolkit/predicate';
|
|
4
4
|
|
|
5
5
|
var __defProp = Object.defineProperty;
|
|
6
|
-
var __name = (target, value) => {
|
|
7
|
-
return __defProp(target, 'name', { value, configurable: true });
|
|
8
|
-
};
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
7
|
|
|
10
8
|
// src/preserveNameAndLength.ts
|
|
11
9
|
function preserveNameAndLength(source, target, adjustLengthBy = 0) {
|
|
12
10
|
return Object.defineProperties(target, {
|
|
13
11
|
name: {
|
|
14
12
|
configurable: true,
|
|
15
|
-
value: source.name
|
|
13
|
+
value: source.name
|
|
16
14
|
},
|
|
17
15
|
length: {
|
|
18
16
|
configurable: true,
|
|
19
|
-
value: source.length + adjustLengthBy
|
|
20
|
-
}
|
|
17
|
+
value: source.length + adjustLengthBy
|
|
18
|
+
}
|
|
21
19
|
});
|
|
22
20
|
}
|
|
23
|
-
__name(preserveNameAndLength,
|
|
21
|
+
__name(preserveNameAndLength, "preserveNameAndLength");
|
|
24
22
|
|
|
25
23
|
// src/bindArg.ts
|
|
26
24
|
function bindArg(fn, boundArgIndex, boundArgValue) {
|
|
27
|
-
return preserveNameAndLength(
|
|
28
|
-
fn,
|
|
29
|
-
|
|
30
|
-
return fn(...args.slice(0, boundArgIndex), boundArgValue, ...args.slice(boundArgIndex));
|
|
31
|
-
},
|
|
32
|
-
-1,
|
|
33
|
-
);
|
|
25
|
+
return preserveNameAndLength(fn, (...args) => {
|
|
26
|
+
return fn(...args.slice(0, boundArgIndex), boundArgValue, ...args.slice(boundArgIndex));
|
|
27
|
+
}, -1);
|
|
34
28
|
}
|
|
35
|
-
__name(bindArg,
|
|
29
|
+
__name(bindArg, "bindArg");
|
|
36
30
|
function bindArgs(fn, boundArgs) {
|
|
37
31
|
const boundIndices = Object.keys(boundArgs).map(Number);
|
|
38
32
|
const lastIndex = Math.max(fn.length - 1, ...boundIndices);
|
|
@@ -42,39 +36,27 @@ function bindArgs(fn, boundArgs) {
|
|
|
42
36
|
const isBound = Object.hasOwn(boundArgs, index);
|
|
43
37
|
const boundIndex = index;
|
|
44
38
|
const unboundIndex = index - (isBound ? offset++ : offset);
|
|
45
|
-
return isBound
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return args[unboundIndex];
|
|
51
|
-
};
|
|
39
|
+
return isBound ? () => {
|
|
40
|
+
return boundArgs[boundIndex];
|
|
41
|
+
} : (args) => {
|
|
42
|
+
return args[unboundIndex];
|
|
43
|
+
};
|
|
52
44
|
});
|
|
53
|
-
return preserveNameAndLength(
|
|
54
|
-
fn
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return get(args);
|
|
59
|
-
}),
|
|
60
|
-
);
|
|
61
|
-
},
|
|
62
|
-
boundIndices.length * -1,
|
|
63
|
-
);
|
|
45
|
+
return preserveNameAndLength(fn, (...args) => {
|
|
46
|
+
return fn(...argGetters.map((get) => {
|
|
47
|
+
return get(args);
|
|
48
|
+
}));
|
|
49
|
+
}, boundIndices.length * -1);
|
|
64
50
|
}
|
|
65
|
-
__name(bindArgs,
|
|
51
|
+
__name(bindArgs, "bindArgs");
|
|
66
52
|
|
|
67
53
|
// src/dethisify.ts
|
|
68
54
|
function dethisify(fn) {
|
|
69
|
-
return preserveNameAndLength(
|
|
70
|
-
fn,
|
|
71
|
-
|
|
72
|
-
return fn.apply(target, args);
|
|
73
|
-
},
|
|
74
|
-
1,
|
|
75
|
-
);
|
|
55
|
+
return preserveNameAndLength(fn, function(target, ...args) {
|
|
56
|
+
return fn.apply(target, args);
|
|
57
|
+
}, 1);
|
|
76
58
|
}
|
|
77
|
-
__name(dethisify,
|
|
59
|
+
__name(dethisify, "dethisify");
|
|
78
60
|
function functionSpy(func, spy, options = {}) {
|
|
79
61
|
if (options.async === false) {
|
|
80
62
|
return wrapSync(func, spy, options);
|
|
@@ -84,13 +66,13 @@ function functionSpy(func, spy, options = {}) {
|
|
|
84
66
|
return wrapMaybeAsync(func, spy, options);
|
|
85
67
|
}
|
|
86
68
|
}
|
|
87
|
-
__name(functionSpy,
|
|
69
|
+
__name(functionSpy, "functionSpy");
|
|
88
70
|
function wrapMaybeAsync(func, spy, options = {}) {
|
|
89
71
|
const ignore = options.ignore;
|
|
90
72
|
if (ignore && ignore(func, spy)) {
|
|
91
73
|
return func;
|
|
92
74
|
}
|
|
93
|
-
return preserveNameAndLength(func, function
|
|
75
|
+
return preserveNameAndLength(func, function(...args) {
|
|
94
76
|
if (ignore && ignore(func, spy, this)) {
|
|
95
77
|
return func.apply(this, args);
|
|
96
78
|
}
|
|
@@ -105,13 +87,13 @@ function wrapMaybeAsync(func, spy, options = {}) {
|
|
|
105
87
|
}
|
|
106
88
|
});
|
|
107
89
|
}
|
|
108
|
-
__name(wrapMaybeAsync,
|
|
90
|
+
__name(wrapMaybeAsync, "wrapMaybeAsync");
|
|
109
91
|
function wrapSync(func, spy, options = {}) {
|
|
110
92
|
const ignore = options.ignore;
|
|
111
93
|
if (ignore && ignore(func, spy)) {
|
|
112
94
|
return func;
|
|
113
95
|
}
|
|
114
|
-
return preserveNameAndLength(func, function
|
|
96
|
+
return preserveNameAndLength(func, function(...args) {
|
|
115
97
|
if (ignore && ignore(func, spy, this)) {
|
|
116
98
|
return func.apply(this, args);
|
|
117
99
|
}
|
|
@@ -120,13 +102,13 @@ function wrapSync(func, spy, options = {}) {
|
|
|
120
102
|
return spy.onReturn(data, retval);
|
|
121
103
|
});
|
|
122
104
|
}
|
|
123
|
-
__name(wrapSync,
|
|
105
|
+
__name(wrapSync, "wrapSync");
|
|
124
106
|
function wrapAsync(func, spy, options = {}) {
|
|
125
107
|
const ignore = options.ignore;
|
|
126
108
|
if (ignore && ignore(func, spy)) {
|
|
127
109
|
return func;
|
|
128
110
|
}
|
|
129
|
-
return preserveNameAndLength(func, async function
|
|
111
|
+
return preserveNameAndLength(func, async function(...args) {
|
|
130
112
|
if (ignore && ignore(func, spy, this)) {
|
|
131
113
|
return await func.apply(this, args);
|
|
132
114
|
}
|
|
@@ -135,72 +117,68 @@ function wrapAsync(func, spy, options = {}) {
|
|
|
135
117
|
return spy.onReturn(data, retval);
|
|
136
118
|
});
|
|
137
119
|
}
|
|
138
|
-
__name(wrapAsync,
|
|
120
|
+
__name(wrapAsync, "wrapAsync");
|
|
139
121
|
|
|
140
122
|
// src/setLength.ts
|
|
141
123
|
function setLength(length, target) {
|
|
142
|
-
return Object.defineProperty(target,
|
|
143
|
-
value: typeof length ===
|
|
124
|
+
return Object.defineProperty(target, "length", {
|
|
125
|
+
value: typeof length === "number" ? length : length.length,
|
|
144
126
|
enumerable: false,
|
|
145
|
-
configurable: true
|
|
127
|
+
configurable: true
|
|
146
128
|
});
|
|
147
129
|
}
|
|
148
|
-
__name(setLength,
|
|
130
|
+
__name(setLength, "setLength");
|
|
149
131
|
|
|
150
132
|
// src/setName.ts
|
|
151
133
|
function setName(name, target) {
|
|
152
|
-
return Object.defineProperty(target,
|
|
153
|
-
value: typeof name ===
|
|
134
|
+
return Object.defineProperty(target, "name", {
|
|
135
|
+
value: typeof name === "string" ? name : name.name,
|
|
154
136
|
configurable: true,
|
|
155
|
-
enumerable: false
|
|
137
|
+
enumerable: false
|
|
156
138
|
});
|
|
157
139
|
}
|
|
158
|
-
__name(setName,
|
|
140
|
+
__name(setName, "setName");
|
|
159
141
|
function thisProxy(fn, proxy) {
|
|
160
142
|
return isFunction(proxy) ? thisProxyCallback(fn, proxy) : thisProxyKey(fn, proxy);
|
|
161
143
|
}
|
|
162
|
-
__name(thisProxy,
|
|
144
|
+
__name(thisProxy, "thisProxy");
|
|
163
145
|
function thisProxyKey(fn, proxy) {
|
|
164
|
-
return preserveNameAndLength(fn, function
|
|
146
|
+
return preserveNameAndLength(fn, function(...args) {
|
|
165
147
|
const thisArg = this[proxy];
|
|
166
148
|
return fn.apply(thisArg, args);
|
|
167
149
|
});
|
|
168
150
|
}
|
|
169
|
-
__name(thisProxyKey,
|
|
151
|
+
__name(thisProxyKey, "thisProxyKey");
|
|
170
152
|
function thisProxyCallback(fn, proxy) {
|
|
171
|
-
return preserveNameAndLength(fn, function
|
|
153
|
+
return preserveNameAndLength(fn, function(...args) {
|
|
172
154
|
const thisArg = proxy(this);
|
|
173
155
|
return fn.apply(thisArg, args);
|
|
174
156
|
});
|
|
175
157
|
}
|
|
176
|
-
__name(thisProxyCallback,
|
|
158
|
+
__name(thisProxyCallback, "thisProxyCallback");
|
|
177
159
|
|
|
178
160
|
// src/thisify.ts
|
|
179
161
|
function thisify(fn) {
|
|
180
|
-
return preserveNameAndLength(
|
|
181
|
-
fn,
|
|
182
|
-
|
|
183
|
-
return fn(this, ...args);
|
|
184
|
-
}, 'f'),
|
|
185
|
-
-1,
|
|
186
|
-
);
|
|
162
|
+
return preserveNameAndLength(fn, /* @__PURE__ */ __name(function f(...args) {
|
|
163
|
+
return fn(this, ...args);
|
|
164
|
+
}, "f"), -1);
|
|
187
165
|
}
|
|
188
|
-
__name(thisify,
|
|
166
|
+
__name(thisify, "thisify");
|
|
189
167
|
|
|
190
168
|
// src/transformReturnValue.ts
|
|
191
169
|
function transformReturnValue(fn, transform) {
|
|
192
|
-
return preserveNameAndLength(fn, function
|
|
170
|
+
return preserveNameAndLength(fn, function(...args) {
|
|
193
171
|
const result = fn.apply(this, args);
|
|
194
172
|
return transform(result);
|
|
195
173
|
});
|
|
196
174
|
}
|
|
197
|
-
__name(transformReturnValue,
|
|
175
|
+
__name(transformReturnValue, "transformReturnValue");
|
|
198
176
|
function wrapMethods(target, strat) {
|
|
199
177
|
for (const [key, type, des] of iterateMethods(target)) {
|
|
200
178
|
if (strat.filter && !strat.filter(target, key, type, des)) {
|
|
201
179
|
continue;
|
|
202
180
|
}
|
|
203
|
-
if (type ===
|
|
181
|
+
if (type === "method") {
|
|
204
182
|
if (!strat.onMethod) {
|
|
205
183
|
continue;
|
|
206
184
|
}
|
|
@@ -212,9 +190,9 @@ function wrapMethods(target, strat) {
|
|
|
212
190
|
Object.defineProperty(target, key, {
|
|
213
191
|
value: preserveNameAndLength(orig, wrapped),
|
|
214
192
|
configurable: des.configurable,
|
|
215
|
-
enumerable: des.enumerable
|
|
193
|
+
enumerable: des.enumerable
|
|
216
194
|
});
|
|
217
|
-
} else if (type ===
|
|
195
|
+
} else if (type === "get") {
|
|
218
196
|
if (!strat.onGetter) {
|
|
219
197
|
continue;
|
|
220
198
|
}
|
|
@@ -226,9 +204,9 @@ function wrapMethods(target, strat) {
|
|
|
226
204
|
Object.defineProperty(target, key, {
|
|
227
205
|
get: preserveNameAndLength(orig, wrapped),
|
|
228
206
|
configurable: des.configurable,
|
|
229
|
-
enumerable: des.enumerable
|
|
207
|
+
enumerable: des.enumerable
|
|
230
208
|
});
|
|
231
|
-
} else if (type ===
|
|
209
|
+
} else if (type === "set") {
|
|
232
210
|
if (!strat.onSetter) {
|
|
233
211
|
continue;
|
|
234
212
|
}
|
|
@@ -240,13 +218,13 @@ function wrapMethods(target, strat) {
|
|
|
240
218
|
Object.defineProperty(target, key, {
|
|
241
219
|
set: preserveNameAndLength(orig, wrapped),
|
|
242
220
|
configurable: des.configurable,
|
|
243
|
-
enumerable: des.enumerable
|
|
221
|
+
enumerable: des.enumerable
|
|
244
222
|
});
|
|
245
223
|
}
|
|
246
224
|
}
|
|
247
225
|
return target;
|
|
248
226
|
}
|
|
249
|
-
__name(wrapMethods,
|
|
227
|
+
__name(wrapMethods, "wrapMethods");
|
|
250
228
|
function* iterateMethods(target) {
|
|
251
229
|
for (const key of Reflect.ownKeys(target)) {
|
|
252
230
|
const des = Object.getOwnPropertyDescriptor(target, key);
|
|
@@ -254,29 +232,29 @@ function* iterateMethods(target) {
|
|
|
254
232
|
continue;
|
|
255
233
|
}
|
|
256
234
|
if (isFunction(des.value)) {
|
|
257
|
-
yield [
|
|
235
|
+
yield [
|
|
236
|
+
key,
|
|
237
|
+
"method",
|
|
238
|
+
des
|
|
239
|
+
];
|
|
258
240
|
} else {
|
|
259
241
|
if (isFunction(des.get)) {
|
|
260
|
-
yield [
|
|
242
|
+
yield [
|
|
243
|
+
key,
|
|
244
|
+
"get",
|
|
245
|
+
des
|
|
246
|
+
];
|
|
261
247
|
}
|
|
262
248
|
if (isFunction(des.set)) {
|
|
263
|
-
yield [
|
|
249
|
+
yield [
|
|
250
|
+
key,
|
|
251
|
+
"set",
|
|
252
|
+
des
|
|
253
|
+
];
|
|
264
254
|
}
|
|
265
255
|
}
|
|
266
256
|
}
|
|
267
257
|
}
|
|
268
|
-
__name(iterateMethods,
|
|
258
|
+
__name(iterateMethods, "iterateMethods");
|
|
269
259
|
|
|
270
|
-
export {
|
|
271
|
-
bindArg,
|
|
272
|
-
bindArgs,
|
|
273
|
-
dethisify,
|
|
274
|
-
functionSpy,
|
|
275
|
-
preserveNameAndLength,
|
|
276
|
-
setLength,
|
|
277
|
-
setName,
|
|
278
|
-
thisProxy,
|
|
279
|
-
thisify,
|
|
280
|
-
transformReturnValue,
|
|
281
|
-
wrapMethods,
|
|
282
|
-
};
|
|
260
|
+
export { bindArg, bindArgs, dethisify, functionSpy, preserveNameAndLength, setLength, setName, thisProxy, thisify, transformReturnValue, wrapMethods };
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/fn",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "fn",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"sideEffects": false,
|
|
6
7
|
"keywords": [
|
|
@@ -15,8 +16,7 @@
|
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"es-toolkit": "^1.44.0",
|
|
18
|
-
"type-fest": "^5.4.4"
|
|
19
|
-
"@types/node": "^24.3.1"
|
|
19
|
+
"type-fest": "^5.4.4"
|
|
20
20
|
},
|
|
21
21
|
"publishConfig": {
|
|
22
22
|
"access": "public"
|