笑料块
jest-docblock 是一个可以提取和解析文件顶端被称作"docblock"的特殊格式注释的软件包.
像这样的医生区:
/**
* Stuff goes here!
*/
Docblocks可以包含pradmas,它们是由 @数字 :
/**
* Pragma incoming!
*
* @flow
*/
Pragmas 也可以提出论据:
/**
* Check this out:
*
* @myPragma it is so cool
*/
jest-docblock 能够:
- 从一些代码取出 docblock 作为字符串
- 将多克块字符串的平板块分析成对象
- 将对象和一些注释打印回字符串
安装
# with yarn
$ yarn add jest-docblock
# with npm
$ npm install jest-docblock
使用量
const code = `
/**
* Everything is awesome!
*
* @everything is:awesome
* @flow
*/
export const everything = Object.create(null);
export default function isAwesome(something) {
return something === everything;
}
`;
const {
extract,
strip,
parse,
parseWithComments,
print,
} = require('jest-docblock');
const docblock = extract(code);
console.log(docblock); // "/**\n * Everything is awesome!\n * \n * @everything is:awesome\n * @flow\n */"
const stripped = strip(code);
console.log(stripped); // "export const everything = Object.create(null);\n export default function isAwesome(something) {\n return something === everything;\n }"
const pragmas = parse(docblock);
console.log(pragmas); // { everything: "is:awesome", flow: "" }
const parsed = parseWithComments(docblock);
console.log(parsed); // { comments: "Everything is awesome!", pragmas: { everything: "is:awesome", flow: "" } }
console.log(print({pragmas, comments: 'hi!'})); // /**\n * hi!\n *\n * @everything is:awesome\n * @flow\n */;
API 文档
extract(contents: string): string
从文件内容中提取一个 docblock 。 返回包含在 contents如果 contents 没有包含 docblock, 它会返回空字符串("") (中文(简体) ).
strip(contents: string): string
从文件中去掉顶端的 docblock 并返回结果。 如果一个文件在顶端没有 docblock,那么返回文件时不变.
parse(docblock: string): {[key: string]: string | string[] }
将多克块字符串中的 pragma 解为对象,其键为 pragma 标记,其值为这些 pragma 的参数.
parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string | string[]} }
类似 parse 除此方法还返回 Docblock 的注释外。 使用时有用 print()。 。 。 .
print({ comments?: string, pragmas?: {[key: string]: string | string[]} }): string
将密钥值对对象打印回多克区块。 若为 comments 他们将被安排在医生的顶部.