2.1.1 • Published 3 years ago

@mrothnet/textfile v2.1.1

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

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

npmyarn
npm install @mrothnet/textfileyarn add @mrothnet/textfile

Usage

Asynchronous API (Promise)

Reading

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

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

Writing

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

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

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

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

Writing

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

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 for details.

Author

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago