vfile utility to rename the path parts of a file
npm install vfile-rename[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[vfile][vfile] utility to rename a file.
* What is this?
* When should I use this?
* Install
* Use
* API
* convert(renames)
* rename(file, renames)
* Move
* Renames
* Spec
* SpecAffix
* 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][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install vfile-rename
In Deno with [esm.sh][esmsh]:
`js`
import {rename} from 'https://esm.sh/vfile-rename@3'
In browsers with [esm.sh][esmsh]:
`html`
`js
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][api-convert] andrename
[][api-rename].
There is no default export.
Create a function (the move) from renames, that when given a file changes
its path properties.
###### Parameters
* renames ([Renames][api-renames], optional)
— rename instructions
###### Returns
A move ([Move][api-move]).
Rename a file.
When given something, returns a vfile from that, and changes its path
properties.
###### Parameters
* file (VFile)renames
— file to rename
* ([Renames][api-renames], optional)
— rename instructions
###### Returns
Nothing (undefined).
Move (TypeScript type).
###### Parameters
* file ([VFile][vfile])
— file to change
###### Returns
Nothing (undefined).
Rename instructions (TypeScript type).
* if the bound rename is a normal string starting with a dot (.), setsfile.extname
file.basename
* otherwise, if the bound rename is a normal string, sets Spec
* 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
[][api-spec]
###### Type
`ts`
type Renames = Array
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.
###### Fields
* basename ([SpecAffix][api-spec-affix] or string, optional)'index.min.js'
— change basename ().dirname
* ([SpecAffix][api-spec-affix] or string, optional)'~'
— change dirname ().extname
* ([SpecAffix][api-spec-affix] or string, optional)'.js'
— change extname ().path
* ([SpecAffix][api-spec-affix], URL, or string, optional)'~/index.min.js'
— change file path ().stem
* ([SpecAffix][api-spec-affix] or string, optional)'index.min'
— change stem ().
Define prepending and/or appending (TypeScript type).
###### Fields
* prefix (string, optional)suffix
— substring to prepend before the field
* (string, optional)
— substring to append after the field.
This package is fully typed with [TypeScript][].
It exports the types [Move][api-move], [Renames][api-renames],Spec
[][api-spec], and [SpecAffix][api-spec-affix].
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][contributing] in [vfile/.github][health] for ways tosupport.md`][support] for ways to get help.
get started.
See [
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/vfile/vfile-rename/workflows/main/badge.svg
[build]: https://github.com/vfile/vfile-rename/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-rename.svg
[coverage]: https://codecov.io/github/vfile/vfile-rename
[downloads-badge]: https://img.shields.io/npm/dm/vfile-rename.svg
[downloads]: https://www.npmjs.com/package/vfile-rename
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=vfile-rename
[size]: https://bundlejs.com/?q=vfile-rename
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/vfile/vfile/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[contributing]: https://github.com/vfile/.github/blob/main/contributing.md
[support]: https://github.com/vfile/.github/blob/main/support.md
[health]: https://github.com/vfile/.github
[coc]: https://github.com/vfile/.github/blob/main/code-of-conduct.md
[license]: license
[author]: https://wooorm.com
[vfile]: https://github.com/vfile/vfile
[api-convert]: #convertrenames
[api-rename]: #renamefile-renames
[api-move]: #move
[api-renames]: #renames
[api-spec]: #spec
[api-spec-affix]: #specaffix