Created
May 12, 2023 09:00
-
-
Save TBXark/b53da5592281abff42a3b197345bb6e8 to your computer and use it in GitHub Desktop.
JSdoc生成器
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
function generateJSDoc(obj, prefix = '') { | |
const jsdocComment = (name, type) => { | |
return ` * @property {${type}} ${name}`; | |
}; | |
const walkObj = (childObj, childPrefix) => { | |
let jsdocStr = ''; | |
for (let key in childObj) { | |
if (!Object.prototype.hasOwnProperty.call(childObj, key)) { | |
continue; | |
} | |
const val = childObj[key]; | |
const propName = childPrefix + key; | |
if (val === null || val === undefined) { | |
jsdocStr += jsdocComment(propName, 'any') + '\n'; | |
} else if (typeof val === 'object') { | |
if (Array.isArray(val)) { | |
jsdocStr += jsdocComment(propName, 'any[]') + '\n'; | |
} else { | |
jsdocStr += walkObj(val, propName + '.'); | |
} | |
} else { | |
jsdocStr += jsdocComment(propName, typeof val) + '\n'; | |
} | |
} | |
return jsdocStr; | |
}; | |
const topLevelComment = `/**\n${walkObj(obj, prefix)}*/`; | |
return topLevelComment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment