Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
*   Add more docs to JSDoc
*   Add support for `null` in input of API types
  • Loading branch information
wooorm committed Feb 6, 2023
1 parent e8139b8 commit 1a8629a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*
*/

/**
* @typedef Statistics
* Statistics.
* @property {number} fatal
* Fatal errors (`fatal: true`)
* Fatal errors (`fatal: true`).
* @property {number} warn
* Warnings (`fatal: false`)
* Warnings (`fatal: false`).
* @property {number} info
* Informational messages (`fatal: null|undefined`)
* Informational messages (`fatal: null | undefined`).
* @property {number} nonfatal
* Warning + info
* Warning + info.
* @property {number} total
* Nonfatal + fatal
* Nonfatal + fatal.
*
* @typedef {'true' | 'false' | 'null'} Field
* @typedef {Record<Field, number>} Fields
* Fatal field.
*/

/**
* Get stats for a file, list of files, or list of messages.
*
* @param {Array<VFile|VFileMessage>|VFile|VFileMessage} [value]
* @param {VFile | VFileMessage | Array<VFile | VFileMessage> | null | undefined} [value]
* @returns {Statistics}
*/
export function statistics(value) {
/** @type {Fields} */
/** @type {Record<Field, number>} */
const result = {true: 0, false: 0, null: 0}

if (value) {
Expand All @@ -46,8 +48,12 @@ export function statistics(value) {
}

/**
* @param {Array<VFile|VFileMessage>} value
* Count a list.
*
* @param {Array<VFile | VFileMessage>} value
* List.
* @returns {void}
* Nothing.
*/
function list(value) {
let index = -1
Expand All @@ -58,8 +64,12 @@ export function statistics(value) {
}

/**
* @param {VFile|VFileMessage} value
* Count a value.
*
* @param {VFile | VFileMessage} value
* Value.
* @returns {void}
* Nothing.
*/
function one(value) {
if ('messages' in value) return list(value.messages)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"index.js"
],
"dependencies": {
"vfile": "^5.0.0",
"vfile-message": "^3.0.0"
},
"devDependencies": {
Expand All @@ -45,7 +46,6 @@
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"vfile": "^5.0.0",
"xo": "^0.53.0"
},
"scripts": {
Expand Down

0 comments on commit 1a8629a

Please sign in to comment.