unified

Project: vfile/vfile-is

Package: vfile-is@2.0.0

  1. Dependents: 0
  2. vfile utility to check if a vfile passes a test
  1. util 147
  2. utility 143
  3. file 34
  4. virtual 19
  5. text 19
  6. vfile 17
  7. vfile-util 15
  8. check 11
  9. processing 3
  10. is 3
  11. matches 2

vfile-is

Build Coverage Downloads Size Sponsors Backers Chat

vfile utility to check if a file passes a test.

Contents

What is this?

This package is a small utility that checks that a file is a certain file.

When should I use this?

Use this small utility if you find yourself repeating code for checking files.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:

npm install vfile-is

In Deno with esm.sh:

import {is} from 'https://esm.sh/vfile-is@2'

In browsers with esm.sh:

<script type="module">
  import {is} from 'https://esm.sh/vfile-is@2?bundle'
</script>

Use

import {toVFile} from 'to-vfile'
import {is} from 'vfile-is'

is(null, '.js') // => false
is({}, '.js') // => false

is(toVFile('index.js'), '.js') // => true
is(toVFile('index.js'), '.md') // => false
is(toVFile('index.js'), 'index.js') // => true
is(toVFile('index.js'), 'readme.md') // => false
is(toVFile('index.js'), '*.js') // => true
is(toVFile('index.js'), '*.md') // => false

is(toVFile('index.js'), {stem: 'index'}) // => true
is(toVFile('index.js'), {stem: 'readme'}) // => false

is(toVFile('index.js'), {stem: {prefix: 'in'}}) // => true
is(toVFile('index.js'), {stem: {prefix: 're'}}) // => false
is(toVFile('index.js'), {stem: {suffix: 'ex'}}) // => true
is(toVFile('index.js'), {stem: {suffix: 'me'}}) // => false

API

This package exports the identifiers is and convert. There is no default export.

is(file, test?)

Check if file passes the given test.

Checks if file is a vfile, converts test to an assertion, and calls that assertion with file. If you’re doing a lot of checks, use convert.

convert(test)

Create a function (the assertion) from test, that when given something, returns whether that value is a vfile and whether it passes the given test.

Parameters
Returns

An assertion.

assertion(file)

When given something, returns whether that value is a vfile and whether it passes the bound test.

Spec

A spec is an object describing fields to values. For each field in spec, if its value is string, there must be an equivalent field in the given file matching the value. If the value is object, it can have a prefix or suffix key, and the value in the given file must be a string, and it must start with prefix and/or end with suffix.

Types

This package is fully typed with TypeScript. The extra types Check, CheckFile, and Assert are exported.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Contribute

See contributing.md in vfile/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer