# rename

> Rename files using some transformers.

Latest version **1.0.4** (published 2016-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install rename
pnpm add rename
yarn add rename
bun add rename
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2016-12-27 |
| First published | 2014-04-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rename) |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | popomore |
| Maintainers | hgpa, hparra, popomore |

## Links

- npm: https://www.npmjs.com/package/rename
- Repository: https://github.com/popomore/rename
- Issues: https://github.com/popomore/rename/issues
- npm.io page: https://npm.io/package/rename

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^2.5.2

## Recent versions

- 1.0.4 (latest) — 2016-12-27
- 1.0.3 — 2015-02-15
- 1.0.2 — 2015-02-15
- 1.0.1 — 2015-02-13
- 1.0.0 — 2015-02-12
- 0.2.3 — 2014-04-09
- 0.2.2 — 2014-04-09
- 0.2.1 — 2014-04-09
- 0.2.0 — 2014-04-09

## README

# rename

Rename files using some transformers.

---

[![NPM version](https://img.shields.io/npm/v/rename.svg?style=flat)](https://npmjs.org/package/rename)
[![Build Status](https://img.shields.io/travis/popomore/rename.svg?style=flat)](https://travis-ci.org/popomore/rename)
[![Build Status](https://img.shields.io/coveralls/popomore/rename.svg?style=flat)](https://coveralls.io/r/popomore/rename)
[![NPM downloads](http://img.shields.io/npm/dm/rename.svg?style=flat)](https://npmjs.org/package/rename)

**NOTE: Rename < 0.2.0 is not the same lib, you can see [renamer](https://www.npmjs.org/package/renamer). 1.0.0 have a big change.**

## Install

```
$ npm install rename -g
```

## Usage

Rename file using another filepath.

```js
rename('a.js', 'b.js');
// => b.js
```

Rename file using a _transform object_ that contain some property.

```
{
  dirname: "replace dirname",
  prefix: "add prefix before basename",
  basename: "replace dirname",
  suffix: "add prefix after basename",
  extname: "replace extname"
}
```

Add `-debug` suffix

```js
rename('a.js', {suffix: '-debug'});
// => a-debug.js
```

Also can use a _transform function_ that could return a _transform object_.

```js
rename('a.js', function() {
  return {suffix: '-debug'};
});
// => a-debug.js
```

## API

### rename(filepath, transformer)

#### filepath

Filepath can be a string or object. Parse the filepath to _file object_ if it's a string.

_File object_ should contain property dirname, basename and extname.

```js
rename({
  basename: 'a',
  extname: '.js'
}, 'b.js');
// => b.js
```

#### transformer

Transformer can be a function, string or object.

Simple example about _transform function_ using custom property.

```js
function transformer(fileObj) {
  return {
    suffix: fileObj.hash || '-debug'
  };
}

rename({
  basename: 'a',
  extname: '.js'
}, transformer);
// => a-debug.js

rename({
  basename: 'a',
  extname: '.js',
  hash: '-123'
}, transformer);
// => a-123.js
```

The value of _transform object_ can be template that parsed from _file object_.

```
rename({
  basename: 'c',
  extname: '.js',
  hash: '111'
}, {
  suffix: '-${hash}',
}).should.eql('c-111.js');
```

### rename.parse

Generate a _file object_ from a string or object.

### rename.stringify

Generate a filepath from _file object_.

## LISENCE

Copyright (c) 2017 popomore. Licensed under the MIT license.

---
_Source: https://npm.io/package/rename · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
