vfile
utility to rename a file.
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Contribute
- License
This package is a utility to add different data-driven ways to rename files.
This package is mostly useful when creating higher level tools that include support for renaming files to end users.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install vfile-rename
In Deno with esm.sh
:
import {rename} from 'https://esm.sh/vfile-rename@3'
In browsers with esm.sh
:
<script type="module">
import {rename} from 'https://esm.sh/vfile-rename@3?bundle'
</script>
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
This package exports the identifiers convert
and
rename
.
There is no default export.
Create a function (the move) from renames
, that when given a file changes
its path properties.
renames
(Renames
, optional) — rename instructions
A move (Move
).
Rename a file.
When given something, returns a vfile from that, and changes its path properties.
file
(VFile
) — file to renamerenames
(Renames
, optional) — rename instructions
Nothing (undefined
).
Move (TypeScript type).
file
(VFile
) — file to change
Nothing (undefined
).
Rename instructions (TypeScript type).
- if the bound rename is a normal string starting with a dot (
.
), setsfile.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
type Renames = Array<Move | Spec | string> | Move | Spec | string
An object describing path properties to values (TypeScript type).
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.
basename
(SpecAffix
orstring
, optional) — change basename ('index.min.js'
).dirname
(SpecAffix
orstring
, optional) — change dirname ('~'
).extname
(SpecAffix
orstring
, optional) — change extname ('.js'
).path
(SpecAffix
,URL
, orstring
, optional) — change file path ('~/index.min.js'
).stem
(SpecAffix
orstring
, optional) — change stem ('index.min'
).
Define prepending and/or appending (TypeScript type).
prefix
(string
, optional) — substring to prepend before the fieldsuffix
(string
, optional) — substring to append after the field.
This package is fully typed with TypeScript.
It exports the types Move
, Renames
,
Spec
, and SpecAffix
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, vfile-rename@^3
,
compatible with Node.js 16.
Use of vfile-rename
is safe.
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.