Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robinweser/read-transform-write
Transform files with ease
https://github.com/robinweser/read-transform-write
fs readfile transforming-files utility writefile
Last synced: 23 days ago
JSON representation
Transform files with ease
- Host: GitHub
- URL: https://github.com/robinweser/read-transform-write
- Owner: robinweser
- License: mit
- Created: 2018-03-10T12:00:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-30T13:36:09.000Z (almost 6 years ago)
- Last Synced: 2024-05-02T00:01:25.447Z (6 months ago)
- Topics: fs, readfile, transforming-files, utility, writefile
- Language: JavaScript
- Homepage:
- Size: 58.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# read-transform-write
An easy-to-use utility to transform files with a single function.
It uses `fs.readFile` and `fs.writeFile` under the hood and throws an error if something goes wrong.
The `encoding` is set to `utf8`. If you need something else, please file an issue.## Support Me
If you're using [Robin Frischmann](https://rofrischmann.de)'s work, please consider supporting his [Open Source Projects](https://github.com/rofrischmann) on [**Patreon**](https://www.patreon.com/rofrischmann).## Installation
```sh
# yarn
yarn add read-transform-write# npm
npm i --save read-transform-write
```## Usage
### Parameter
| Parameter | Type | Description |
| --- | --- | --- |
| inputPath | (*string*) | The absolute path to the input file. |
| transform | (*Function*) | The transformation method.
It receives the input data and returns method receving a write-method that must be called with the transformed data and a output path. |
| callback | (*Function?*) | An optional callback that receives an object with the callback shape. |#### Callback Shape
```javascript
type Callback {
inputPath: string,
outputPath: string,
input: string,
output: string
}
```### Example
```javascript
import { join } from 'path'
import transformFile from 'read-transform-write'const input = join(__dirname, 'input.txt')
const output = join(__dirname, 'output.txt')const transform = data => write => write(
output,
data
.split('\n')
.map(val => parseInt(val, 10))
.map(Math.sqrt)
.join('\n')
)transformFile(
input,
transform,
({ output, outputPath }) => {
console.log(`Written ${output} to ${outputPath}.`)
}
)
```##### input.txt
```
49
36
25
16
```##### output.txt
```
7
6
5
4
```## License
read-transform-write is licensed under the [MIT License](http://opensource.org/licenses/MIT).
Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).
Created with ♥ by [@rofrischmann](http://rofrischmann.de).