vfile-sort

4.0.0 · active · verified Sun Apr 19

vfile-sort is a utility package within the unified/vfile ecosystem designed for ordering `VFile` objects and `VFileMessage` objects. Its current stable version is 4.0.0. The package primarily exposes comparator functions, `compareFile` and `compareMessage`, which can be used with array sorting methods (e.g., `Array.prototype.sort` or `toSorted`) to arrange files or messages by properties like line, column, severity, and other metadata. It follows the Node.js LTS release cycle for compatibility and ships with TypeScript types, making it suitable for modern JavaScript and TypeScript projects. Key differentiators include its tight integration with the `vfile` specification and its focus on providing precise, consistent sorting logic for diagnostics and file-related metadata, ensuring human-readable reports.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates sorting `VFileMessage` and `VFile` instances using `compareMessage` and `compareFile` respectively.

import { VFile } from 'vfile';
import { VFileMessage } from 'vfile-message';
import { compareFile, compareMessage } from 'vfile-sort';

// Sort VFileMessages
const messages = [
  new VFileMessage('Error: Missing semicolon.', { place: { line: 3, column: 1 } }),
  new VFileMessage('Warning: Unused variable.', { place: { line: 2, column: 5 } }),
  new VFileMessage('Info: Deprecated API.', { place: { line: 3, column: 10 } })
];

const sortedMessages = messages.toSorted(compareMessage).map(String);
console.log('Sorted Messages:', sortedMessages);
// Expected: ['2:5: Warning: Unused variable.', '3:1: Error: Missing semicolon.', '3:10: Info: Deprecated API.']

// Sort VFiles (example using dummy paths for demonstration)
const fileA = new VFile({ path: '/project/src/components/button.ts' });
const fileB = new VFile({ path: '/project/src/utils/helpers.ts' });
const fileC = new VFile({ path: '/project/index.ts' });

const files = [fileA, fileB, fileC];
const sortedFiles = files.toSorted(compareFile).map((d) => d.path);
console.log('Sorted Files:', sortedFiles);
// Expected: ['/project/index.ts', '/project/src/components/button.ts', '/project/src/utils/helpers.ts']

view raw JSON →