vfile-rename

vfile utility to rename the path parts of a file

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
vfile-rename
202.0.19 months ago3 years agoMinified + gzip package size for vfile-rename in KB

Readme

vfile-rename
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
vfilevfile utility to rename a file.

Contents

*   [`rename(file, renames)`](#renamefile-renames)
*   [`convert(renames)`](#convertrenames)
*   [`Spec`](#spec)

What is this?

This package is a utility to add different data-driven ways to rename files.

When should I use this?

This package is mostly useful when creating higher level tools that include support for renaming files to end users.

Install

This package is ESM onlyesm. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:
npm install vfile-rename

In Deno with esm.shesmsh:
import {rename} from 'https://esm.sh/vfile-rename@2'

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

Use

import {VFile} from 'vfile'
import {rename} from 'vfile-rename'

const file = new VFile({path: 'readme.md'})
file.path // => readme.md

// Set extname:
rename(file, '.html')
file.path // => readme.html

// Set basename:
rename(file, 'main.md')
file.path // => main.md

// Set path properties:
rename(file, {stem: 'index'})
file.path // => index.md

// Change path properties:
rename(file, {stem: {suffix: '.bak'}})
file.path // => index.bak.md

// All together:
rename(file, ['readme.md', '.htm', {stem: 'index', extname: {suffix: 'l'}}])
file.path // => index.html

API

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

rename(file, renames)

Rename a file. Calls convert and then the resulting movemove internally.
Overview
  • '.md' (string starting w/ dot) — change extension
  • 'example.md' (string not starting w/ dot) — change basename
  • {stem: 'readme'} (Record<Field, string>) — change a field (supports
`'path'`, `'basename'`, `'stem'`, `'extname'`, and/or `'dirname'`)
  • {basename: {suffix: '-2'}} (Record<Field, SpecAffix>) — prepend
(`prefix`) or append (`suffix`) to a field
Parameters
  • file (VFile) — any value accepted by vfile()
  • renames (string, Function, Spec, or Array<Rename>, optional)
Returns
The given file (VFile).

convert(renames)

Create a function (the movemove) from renames that when given a file changes its path properties.
Parameters
  • renames (string, Function, Spec, or Array<Rename>, optional)
Returns
A movemove.

move(file)

When given something, returns a vfile from that and changes its path properties.
  • if there is no bound rename (it’s null or undefined), makes sure file
is a `VFile`
  • otherwise, if the bound rename is a normal string starting with a dot (.),
sets `file.extname`
  • otherwise, if the bound rename is a normal string, sets file.basename
  • otherwise, if the bound test is an array, all renames in it are performed
  • otherwise, if the bound rename is an object, renames according to the
[`Spec`][spec]

Spec

An object describing path properties to values. For each property in spec, if its value is string, the value of the path property on the given file is set. If the value is object, it can have a prefix or suffix key, the value of the path property on the given file is prefixed and/or suffixed.
Note that only allowed path properties can be set, other properties are thrown for.

Types

This package is fully typed with TypeScript. The extra types SpecAffix, Spec, Move, and Renames 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+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Security

Use of vfile-rename is safe by default.

Contribute

See contributing.mdcontributing in vfile/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor