https://github.com/fizzygalacticus/savethis
https://github.com/fizzygalacticus/savethis
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/fizzygalacticus/savethis
- Owner: FizzyGalacticus
- Created: 2019-08-31T01:26:04.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T13:08:37.000Z (over 4 years ago)
- Last Synced: 2025-02-03T09:55:29.349Z (over 1 year ago)
- Language: JavaScript
- Size: 131 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# savethis
A util to help save the return value of a function with minimal effort. Very helpful when debugging, especially with lots of async operations in between.
## Installation
Using `npm`:
```sh
npm i --save-dev @fizzygalacticus/savethis
```
Using `yarn`:
```sh
yarn add --dev @fizzygalacticus/savethis
```
## Usage
`savethis` is a decorator that can be used on both asynchronous and synchronous functions:
**synchronous:**
```js
// Saves "hello, world!" to test.txt
const res = savethis(() => 'hello, world!', 'test.txt', {/* ...options */})(/* ...params */);
console.log(res); // "hello, world!"
```
**asynchronous:**
```js
// Saves "hello, world!" to test.txt
const res = await savethis(() => Promise.resolve('hello, world!'), 'test.txt', {/* ...options */})(/* ...params */);
console.log(res); // "hello, world!"
```
### Options
* `save` - a function to save the data, with the signature `(path, data) => any`. For original functions that are synchronous (don't return a `Promise`), this is `fs.writeFileSync`. Otherwise the default is a `promisify`'d `fs.writeFile`.
* `format` - a function to specify the formatting prior to write. Default is `data => JSON.stringify(data, null, 4)`.
**notes:**
* If your formatting function returns a `Promise`, everything will still function as it should, although if your original function does not return a `Promise`, you will need to take care to catch any errors or rejections from the formatter.