'jsdoc: how to document properties using bracket notation?
Given this rather silly function, how should documentation be written for "range" param?
/**
* @param {number} value
* @param {Object} range
* @param {number} range['<']
* @param {number} range['<=']
* @param {number} range['>']
* @param {number} range['>=']
*/
function testIsInRange(value, range) {
var threshold;
if ((threshold = range['<'], threshold !== undefined) && !(value < threshold)) {
fail('Value must be less than ' + threshold);
}
if ((threshold = range['<='], threshold !== undefined) && !(value <= threshold)) {
fail('Value must be less than or equal to ' + threshold);
}
if ((threshold = range['>'], threshold !== undefined) && !(value > threshold)) {
fail('Value must be greater than ' + threshold);
}
if ((threshold = range['>='], threshold !== undefined) && !(value >= threshold)) {
fail('Value must be greater than or equal to ' + threshold);
}
}
Neither dot nor bracket notation seems to work. Property types (Object and number) are recognized, however their names are not.
Solution 1:[1]
Types can be specified as complex objects:
/**
* @param {number} value
* @param {{'<': number, '<=': number, '>': number, '>=': number}} range
*/
function testIsInRange(value, range) {
...
}
Alternatively, destructuring can help, though it'll require renaming the parameters to non-symbolic names:
/**
* @param {number} value
* @param {number} lt
* @param {number} lte
* @param {number} gt
* @param {number} gte
*/
function testIsInRange(value, {'<':lt, '<=':lte, '>':gt, '>=':gte}) {
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Annonymus |
