UNPKG

1.4 kBJavaScriptView Raw
1export const CallableInstance =
2 /**
3 * @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result}
4 */
5 (
6 /** @type {unknown} */
7 (
8 /**
9 * @this {Function}
10 * @param {string | symbol} property
11 * @returns {(...parameters: Array<unknown>) => unknown}
12 */
13 function (property) {
14 const self = this
15 const constr = self.constructor
16 const proto = /** @type {Record<string | symbol, Function>} */ (
17 // Prototypes do exist.
18 // type-coverage:ignore-next-line
19 constr.prototype
20 )
21 const value = proto[property]
22 /** @type {(...parameters: Array<unknown>) => unknown} */
23 const apply = function () {
24 return value.apply(apply, arguments)
25 }
26
27 Object.setPrototypeOf(apply, proto)
28
29 // Not needed for us in `unified`: we only call this on the `copy`
30 // function,
31 // and we don't need to add its fields (`length`, `name`)
32 // over.
33 // See also: GH-246.
34 // const names = Object.getOwnPropertyNames(value)
35 //
36 // for (const p of names) {
37 // const descriptor = Object.getOwnPropertyDescriptor(value, p)
38 // if (descriptor) Object.defineProperty(apply, p, descriptor)
39 // }
40
41 return apply
42 }
43 )
44 )