-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor some more code to use JSDoc
- Loading branch information
Showing
5 changed files
with
46 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,3 @@ node_modules/ | |
*.log | ||
yarn.lock | ||
!/index.d.ts | ||
!/lib/callable-instance.d.ts |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
/** | ||
* @param {string} property | ||
*/ | ||
export function CallableInstance(property) { | ||
/** @type {Function} */ | ||
const self = this | ||
const constr = self.constructor | ||
// Prototypes do exist. | ||
// type-coverage:ignore-next-line | ||
const proto = /** @type {Record<string, Function>} */ (constr.prototype) | ||
const func = proto[property] | ||
const apply = function () { | ||
return func.apply(apply, arguments) | ||
} | ||
export const CallableInstance = | ||
/** | ||
* @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result} | ||
*/ | ||
( | ||
/** @type {unknown} */ | ||
( | ||
/** | ||
* @this {Function} | ||
* @param {string | symbol} property | ||
* @returns {(...parameters: Array<unknown>) => unknown} | ||
*/ | ||
function (property) { | ||
const self = this | ||
const constr = self.constructor | ||
const proto = /** @type {Record<string | symbol, Function>} */ ( | ||
// Prototypes do exist. | ||
// type-coverage:ignore-next-line | ||
constr.prototype | ||
) | ||
const func = proto[property] | ||
/** @type {(...parameters: Array<unknown>) => unknown} */ | ||
const apply = function () { | ||
return func.apply(apply, arguments) | ||
} | ||
|
||
Object.setPrototypeOf(apply, proto) | ||
Object.setPrototypeOf(apply, proto) | ||
|
||
const names = Object.getOwnPropertyNames(func) | ||
const names = Object.getOwnPropertyNames(func) | ||
|
||
for (const p of names) { | ||
const descriptor = Object.getOwnPropertyDescriptor(func, p) | ||
if (descriptor) Object.defineProperty(apply, p, descriptor) | ||
} | ||
for (const p of names) { | ||
const descriptor = Object.getOwnPropertyDescriptor(func, p) | ||
if (descriptor) Object.defineProperty(apply, p, descriptor) | ||
} | ||
|
||
return apply | ||
} | ||
|
||
// Prototypes do exist. | ||
// type-coverage:ignore-next-line | ||
CallableInstance.prototype = Object.create(Function.prototype) | ||
return apply | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters