Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 6, 2023
1 parent 1a8629a commit 0a92fae
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 84 deletions.
86 changes: 2 additions & 84 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,5 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('./lib/index.js').Statistics} Statistics
*/

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

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

if (value) {
if (Array.isArray(value)) {
list(value)
} else {
one(value)
}
}

return {
fatal: result.true,
nonfatal: result.false + result.null,
warn: result.false,
info: result.null,
total: result.true + result.false + result.null
}

/**
* Count a list.
*
* @param {Array<VFile | VFileMessage>} value
* List.
* @returns {void}
* Nothing.
*/
function list(value) {
let index = -1

while (++index < value.length) {
one(value[index])
}
}

/**
* Count a value.
*
* @param {VFile | VFileMessage} value
* Value.
* @returns {void}
* Nothing.
*/
function one(value) {
if ('messages' in value) return list(value.messages)

const field = /** @type {Field} */ (
String(
value.fatal === undefined || value.fatal === null
? null
: Boolean(value.fatal)
)
)

result[field]++
}
}
export {statistics} from './lib/index.js'
87 changes: 87 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*/

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

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

if (value) {
if (Array.isArray(value)) {
list(value)
} else {
one(value)
}
}

return {
fatal: result.true,
nonfatal: result.false + result.null,
warn: result.false,
info: result.null,
total: result.true + result.false + result.null
}

/**
* Count a list.
*
* @param {Array<VFile | VFileMessage>} value
* List.
* @returns {void}
* Nothing.
*/
function list(value) {
let index = -1

while (++index < value.length) {
one(value[index])
}
}

/**
* Count a value.
*
* @param {VFile | VFileMessage} value
* Value.
* @returns {void}
* Nothing.
*/
function one(value) {
if ('messages' in value) return list(value.messages)

const field = /** @type {Field} */ (
String(
value.fatal === undefined || value.fatal === null
? null
: Boolean(value.fatal)
)
)

result[field]++
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 0a92fae

Please sign in to comment.