# @mrothnet/textfile

> Reading and writing UTF-8 encoded textfiles

Latest version **2.1.1** (published 2020-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mrothnet/textfile
pnpm add @mrothnet/textfile
yarn add @mrothnet/textfile
bun add @mrothnet/textfile
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2020-12-18 |
| First published | 2020-01-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Michael Roth |
| Maintainers | mrothnet |
| Keywords | utf-8, utf8, text, read, write, append, create, edit, file |

## Links

- npm: https://www.npmjs.com/package/@mrothnet/textfile
- Repository: https://github.com/mrothNET/textfile.js
- Issues: https://github.com/mrothNET/textfile.js/issues
- npm.io page: https://npm.io/package/@mrothnet/textfile

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 2.1.1 (latest) — 2020-12-18
- 2.1.0 — 2020-12-17
- 2.0.0 — 2020-03-07
- 1.0.1 — 2020-02-08
- 1.0.0 — 2020-02-08
- 0.2.0 — 2020-01-28
- 0.1.1 — 2020-01-26
- 0.1.0 — 2020-01-26

## README

# Reading and writing UTF-8 encoded text files

Makes reading, saving and editing text files simple and more descriptive:

- Encoding is always UTF-8.
- Synchronous and asynchronous API.
- Typescript supported.

Differentiates between:

Read
: File must exists to be read.

Create
: File to be written is not allowed to exists already.

Write
: File to be written may exists already and gets overwritten in case.

Append
: File to be written may exists already and content is appended in case.

## Install

|               npm                |             yarn              |
| :------------------------------: | :---------------------------: |
| `npm install @mrothnet/textfile` | `yarn add @mrothnet/textfile` |

## Usage

### Asynchronous API (Promise)

#### Reading

```javascript
const { readTextFile } = require("@mrothnet/textfile");

// Read UTF-8 encoded text file
const content = await readTextFile("path/filename.txt");
```

#### Writing

```javascript
const { createTextFile, writeTextFile, appendTextFile } = require("@mrothnet/textfile");

// Create new text file (fails if file already exists)
await createTextFile("path/filename.txt", "Hello, World!");

// Replace or create text file
await writeTextFile("path/filename.txt", "New file content");

// Append to existing file or create new one
await appendTextFile("path/filename.txt", "This line will be appended\n");
```

#### Editing

```javascript
const { editTextFile } = require("@mrothnet/textfile");

// Edit an existing file with synchronous function
await editTextFile("path/filename.txt", (text) => {
  return text.toUpperCase();
});

// Edit an existing file with asynchronous function (Promise)
await editTextFile("path/filename.txt", (text) => {
  return new Promise((resolve) => {
    resolve(text.toLowerCase());
  });
});
```

### Synchronous API

#### Reading

```javascript
const { readTextFileSync } = require("@mrothnet/textfile");

// Read UTF-8 encoded text file
const content = readTextFileSync("path/filename.txt");
```

#### Writing

```javascript
const { createTextFileSync, writeTextFileSync, appendTextFileSync } = require("@mrothnet/textfile");

// Create new text file (fails if file already exists)
createTextFileSync("path/filename.txt", "Hello, World!");

// Replace or create text file
writeTextFileSync("path/filename.txt", "New file content");

// Append to existing file or create new one
appendTextFileSync("path/filename.txt", "This line will be appended\n");
```

#### Editing

```javascript
const { editTextFileSync } = require("@mrothnet/textfile");

// Edit an existing file (only synchronous)
editTextFileSync("path/filename.txt", (text) => {
  return text.toUpperCase();
});
```

## Contributing

Pull requests, patches, emails, and issues are welcomed!

## License

This project is licensed under the MIT License - see [LICENSE](LICENSE) for details.

## Author

- [Michael Roth](https://mroth.net/) <[<mail@mroth.net>](mailto:mail@mroth.net)>

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