I'm trying to write some helper functions for a library that will make it easier to use some Object functions. Here is some sample code of what I mean:
// ./utc-value.js
const makeEnumerable = require('./make-enumerable');
/* Data class to represent UTC values to be handled by an ExtendedDate class
* const dt = new Date();
* const xdt = new ExtendedDate(dt);
* xdt.utc === new UtcValue(dt); // true
* xdt.date === dt.getDate(); // true
* xdt.utc.date === dt.getUTCDate(); // true
*/
class UtcValue {
constructor(dt) {
this._dt = dt;
Object.defineProperty(this, '_dt', { enumerable: false, value: dt } );
}
get date() {
return this._dt.getUTCDate();
}
...
}
makeEnumerable(UtcValue.prototype, 'date');
module.exports = UtcValue;
While the makeEnumerable function looks like this:
/**
* Maps enumerable=true across all named properties
* @param prototype
* @param {string} properties
*/
function makeEnumerable(prototype, ...properties) {
const propertyDescriptors = Object.getOwnPropertyDescriptors(prototype);
for(const property of properties) {
const attributes = propertyDescriptors[property] || {};
attributes.enumerable = true;
Object.defineProperty(prototype, property, attributes);
}
}
module.exports = makeEnumerable;
What should the JSDoc signature of makeEnumerable look like, to indicate to a user that I'm expecting a prototype, such as Object.prototype or obj.constructor.prototype? I'm also writing the ./make-enumerable.d.ts file manually (project is JavaScript classic with plans to move to TypeScript in a future version), so the proper way to document the signature would be appreciated.
// ./make-enumerable.d.ts
export default function makeEnumerable(
prototype: typeof Object.prototype,
...properties: string[]): void;