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 f32f679 commit 7f40717
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 75 deletions.
76 changes: 1 addition & 75 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*/

/** @type {Record<string, number>} */
const severities = {true: 2, false: 1, null: 0, undefined: 0}

/**
* Sort messages in the given vfile.
*
* @template {VFile} File
* File type.
* @param {File} file
* File to sort.
* @returns {File}
* Sorted file.
*/
// To do: next major: don’t return `file`.
export function sort(file) {
file.messages.sort(comparator)
return file
}

/**
* Compare a message.
*
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @returns {number}
* Order.
*/
function comparator(a, b) {
return (
check(a, b, 'line') ||
check(a, b, 'column') ||
severities[String(b.fatal)] - severities[String(a.fatal)] ||
compare(a, b, 'source') ||
compare(a, b, 'ruleId') ||
compare(a, b, 'reason') ||
0
)
}

/**
* Compare a numeric field.
*
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @param {'column' | 'line'} field
* Numeric field.
* @returns {number}
* Order.
*/
function check(a, b, field) {
return (a[field] || 0) - (b[field] || 0)
}

/**
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @param {'reason' | 'ruleId' | 'source'} field
* String field.
* @returns {number}
* Order.
*/
function compare(a, b, field) {
return String(a[field] || '').localeCompare(b[field] || '')
}
export {sort} from './lib/index.js'
75 changes: 75 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*/

/** @type {Record<string, number>} */
const severities = {true: 2, false: 1, null: 0, undefined: 0}

/**
* Sort messages in the given vfile.
*
* @template {VFile} File
* File type.
* @param {File} file
* File to sort.
* @returns {File}
* Sorted file.
*/
// To do: next major: don’t return `file`.
export function sort(file) {
file.messages.sort(comparator)
return file
}

/**
* Compare a message.
*
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @returns {number}
* Order.
*/
function comparator(a, b) {
return (
check(a, b, 'line') ||
check(a, b, 'column') ||
severities[String(b.fatal)] - severities[String(a.fatal)] ||
compare(a, b, 'source') ||
compare(a, b, 'ruleId') ||
compare(a, b, 'reason') ||
0
)
}

/**
* Compare a numeric field.
*
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @param {'column' | 'line'} field
* Numeric field.
* @returns {number}
* Order.
*/
function check(a, b, field) {
return (a[field] || 0) - (b[field] || 0)
}

/**
* @param {VFileMessage} a
* Left message.
* @param {VFileMessage} b
* Right message.
* @param {'reason' | 'ruleId' | 'source'} field
* String field.
* @returns {number}
* Order.
*/
function compare(a, b, field) {
return String(a[field] || '').localeCompare(b[field] || '')
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 7f40717

Please sign in to comment.