VFile Property Checker
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
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/vfile-is/index.js from ... not supported.
cause Attempting to import `vfile-is` using CommonJS `require()` syntax in a non-ESM context.fixUpdate your import statements to use ES module syntax (e.g., `import { is } from 'vfile-is'`). Ensure your project's `package.json` includes `"type": "module"` if running in Node.js. -
ReferenceError: VFile is not defined
cause The `VFile` class, which is a core dependency type for `vfile-is`, was not imported or is not accessible within the current scope.fixImport `VFile` from `to-vfile` (e.g., `import { VFile } from 'to-vfile'`) or directly from `vfile` if `to-vfile` is not needed for file creation. -
TypeError: is is not a function
cause The `is` function was imported incorrectly, most commonly as a default import when it is a named export, or incorrectly destructured from a CommonJS `require` call.fixEnsure `is` is imported as a named export: `import { is } from 'vfile-is'`.
Warnings
- breaking Version 3.0.0 and newer requires Node.js 16 or later. Running in older Node.js environments will result in runtime errors.
- breaking Since version 2.0.0, vfile-is is an ESM-only package. Attempting to import it using CommonJS `require()` syntax will result in an `ERR_REQUIRE_ESM` error.
- breaking Version 3.0.0 introduced an `exports` map in its `package.json`. If you were previously using undocumented or non-standard internal import paths, they might now be inaccessible or have changed.
- gotcha The `check` parameter in `is()` and `convert()` is highly polymorphic, accepting various types like strings (for path, basename, extname, globs), functions, objects (for field-specific checks), or arrays of these types. Misunderstanding how different input types are interpreted can lead to unexpected filtering or incorrect matches.
Install
-
npm install vfile-is -
yarn add vfile-is -
pnpm add vfile-is
Imports
- is
const is = require('vfile-is').isimport { is } from 'vfile-is' - convert
import convert from 'vfile-is'
import { convert } from 'vfile-is' - VFile
import { VFile } from 'vfile'import { VFile } from 'to-vfile'
Quickstart
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))