# @taufik-nurrohman/file

> File system utility.

Latest version **1.0.0** (published 2022-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @taufik-nurrohman/file
pnpm add @taufik-nurrohman/file
yarn add @taufik-nurrohman/file
bun add @taufik-nurrohman/file
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-07-01 |
| First published | 2020-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Taufik Nurrohman |
| Maintainers | taufik-nurrohman |
| Keywords | file, helper, utility |

## Links

- npm: https://www.npmjs.com/package/@taufik-nurrohman/file
- Repository: https://github.com/taufik-nurrohman/file
- Homepage: https://taufik-nurrohman.js.org
- Issues: https://github.com/taufik-nurrohman/file/issues
- npm.io page: https://npm.io/package/@taufik-nurrohman/file

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-07-01
- 0.0.11 — 2021-05-28
- 0.0.10 — 2021-05-02
- 0.0.9 — 2021-03-30
- 0.0.8 — 2021-03-27
- 0.0.7 — 2020-12-31
- 0.0.6 — 2020-12-20
- 0.0.5 — 2020-12-18
- 0.0.4 — 2020-12-18
- 0.0.3 — 2020-12-18
- 0.0.2 — 2020-12-18
- 0.0.1 — 2020-12-18

## README

File Utility
============

Utility functions of native file system API in Node.js. Not to be used in the browser.

Usage
-----

### CommonJS

~~~ js
const {getContent, setContent} = require('@taufik-nurrohman/file');

let content = getContent('./package.json');

if (null !== content) {
    setContent('./package.json', content.replace(/"/g, "'"));
}
~~~

### ECMAScript

~~~ js
import {getContent, setContent} from '@taufik-nurrohman/file';

let content = getContent('./package.json');

if (null !== content) {
    setContent('./package.json', content.replace(/"/g, "'"));
}
~~~

Methods
-------

### copy(from, to, name)

Copy a file.

~~~ js
copy('./package.json', './foo/bar/baz');
copy('./package.json', './foo/bar/baz', 'package.json.bak');
~~~

### get(path)

Check if file/folder does exist.

~~~ js
if (false !== get('./package.json')) {
    // …
}
~~~

### getContent(path)

Get file content as string.

~~~ js
let content = getContent('./package.json');

if (null !== content) {
    // …
}
~~~

### isFile(path)

Check if path is a file.

~~~ js
if (false !== isFile('./foo/bar/baz.qux')) {
    // …
}
~~~

### move(from, to, name)

Delete or move a file.

~~~ js
// Delete
move('./package.json', false);

// Delete
move('./package.json');

// Move
move('./package.json', './foo/bar/baz');

// Move
move('./package.json', './foo/bar/baz', 'package.json.bak');
~~~

### name(path, x = false)

Get file name from file path.

~~~ js
console.log(name('./foo/bar/baz.qux'));
console.log(name('./foo/bar/baz.qux', true));
console.log(name('./foo/bar/baz.qux', 'asdf'));
~~~

### parent(path)

Get parent path from file path.

~~~ js
console.log(parent('./foo/bar/baz.qux'));
console.log(parent('./foo/bar/baz'));
console.log(parent('./'));
console.log(parent('.'));
console.log(parent(""));
~~~

### parseContent(content, data, pattern = '%\\\\((\\\\S+?)\\\\)', separator = '.')

Convert embedded variables with format such as `%(foo.bar)` in `content` with `data`.

~~~ js
let content = 'foo %(bar) %(baz.qux)';
let data = {
        bar: 'bar',
        baz: {
            qux: 'baz qux'
        }
    };

console.log(parseContent(content, data));
~~~

### set(path)

Create an empty file if it does not exist.

~~~ js
set('./foo/bar/baz.qux');
~~~

### setContent(path, content)

Create or overwrite a file.

~~~ js
setContent('./foo/bar/baz.qux', 'foo bar baz');
~~~

### x(path)

Get file extension from file path.

~~~ js
console.log(x('./foo/bar/baz.qux'));
console.log(x('./foo/bar/baz'));
console.log(x('./foo/bar/baz.'));
console.log(x('./foo/bar/.baz'));
console.log(x('./foo/bar/.baz.qux'));
console.log(x('./'));
console.log(x('.'));
console.log(x(""));
~~~

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