This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
callbacks.ts
56 lines (49 loc) · 1.83 KB
/
callbacks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/** This module is browser compatible. */
/** Creates a function that returns the instance the method was called on. */
// deno-lint-ignore no-explicit-any
export function returnsThis<T>(): (...args: any[]) => ThisType<T> {
return function <T>(this: T): ThisType<T> {
return this;
};
}
/** Creates a function that returns one of its arguments. */
// deno-lint-ignore no-explicit-any
export function returnsArg(idx: number): (...args: any[]) => any {
// deno-lint-ignore no-explicit-any
return function (...args: any[]) {
return args[idx];
};
}
/** Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end. */
export function returnsArgs(
start = 0,
end?: number,
// deno-lint-ignore no-explicit-any
): (...args: any[]) => any {
// deno-lint-ignore no-explicit-any
return function (...args: any[]) {
return Array.prototype.slice.call(args, start, end);
};
}
/** Creates a function that throws a specific error. */
// deno-lint-ignore no-explicit-any
export function throws(error: any): (...args: any[]) => any {
return function () {
throw error;
};
}
/** Creates a function that returns a promise that will resolve a specific value. */
// deno-lint-ignore no-explicit-any
export function resolves(value: any): (...args: any[]) => Promise<any> {
return () => Promise.resolve(value);
}
/** Creates a function that returns a promise that will reject a specific error. */
// deno-lint-ignore no-explicit-any
export function rejects(error: any): (...args: any[]) => Promise<any> {
return () => Promise.reject(error);
}
/** Creates a function that returns the time difference between when it is called and when it was created. */
export function fromNow(): () => number {
const start: number = Date.now();
return () => Date.now() - start;
}