VFile Property Checker

3.0.0 · active · verified Sun Apr 19

vfile-is is a utility package within the unified/vfile ecosystem, designed to facilitate checking properties of `vfile` objects. It allows developers to assert whether a virtual file matches specific criteria, supporting a range of tests including glob patterns against `file.path`, direct string matches against `file.basename` or `file.extname`, custom predicate functions, or object-based comparisons against various `vfile` fields like `stem`, `extname`, or `basename`. The current stable version is 3.0.0, which mandates Node.js 16 or newer and is ESM-only. The package maintains a steady release cadence, often aligning with updates to the core `vfile` library. Its key differentiators include its integration with the `vfile` standard, offering a streamlined API (`is` and `convert`) for robust and composable file testing, which helps in building unified-based tools by providing a consistent way to filter or categorize files.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates various ways to check VFile properties using `is()`, including path, extension, globs, object-based field assertions, and how to create reusable assertion functions with `convert()`.

import {VFile} from 'to-vfile'
import {is, convert} from 'vfile-is'

// Basic checks for non-VFile inputs
console.log('Is undefined a JS file?', is(undefined, '.js'))
console.log('Is empty object a JS file?', is({}, '.js'))

// Create VFile instances for testing
const jsFile = new VFile({path: 'src/index.js', value: 'console.log("hello");'})
const mdFile = new VFile({path: 'docs/readme.md', value: '# Readme'})
const txtFile = new VFile({basename: 'notes.txt', value: 'Some notes.'})


// Checking path and extension
console.log('Is src/index.js a JS file?', is(jsFile, '.js'))
console.log('Is src/index.js an MD file?', is(jsFile, '.md'))
console.log('Does src/index.js match src/index.js?', is(jsFile, 'src/index.js'))

// Checking with globs
console.log('Does src/index.js match *.js?', is(jsFile, '*.js'))
console.log('Does src/index.js match src/*.js?', is(jsFile, 'src/*.js'))

// Checking with object fields
console.log('Does src/index.js have stem \'index\'?', is(jsFile, {stem: 'index'}))
console.log('Does readme.md have stem \'readme\'?', is(mdFile, {stem: 'readme'}))
console.log('Does notes.txt have extname \'.txt\'?', is(txtFile, {extname: '.txt'}))

// Checking with nested object fields (prefix/suffix)
console.log('Does index.js have stem starting with \'in\'?', is(jsFile, {stem: {prefix: 'in'}}))
console.log('Does index.js have stem ending with \'ex\'?', is(jsFile, {stem: {suffix: 'ex'}}))

// Combining multiple checks in an array (all must pass)
console.log('Is readme.md an MD file AND has stem \'readme\'?', is(mdFile, [{extname: '.md'}, {stem: 'readme'}]))
console.log('Is src/index.js a JS file AND in src/ directory?', is(jsFile, ['.js', 'src/*.js']))

// Using convert for reusable assertion functions
const isMdReadme = convert([{extname: '.md'}, {stem: 'readme'}])
console.log('Using converted assertion for readme.md:', isMdReadme(mdFile))
console.log('Using converted assertion for index.js:', isMdReadme(jsFile))

view raw JSON →