An open API service indexing awesome lists of open source software.

https://github.com/lamualfa/only-make

✨ One-liner helper to initialize complex local dependent variable.
https://github.com/lamualfa/only-make

make only

Last synced: 10 months ago
JSON representation

✨ One-liner helper to initialize complex local dependent variable.

Awesome Lists containing this project

README

          

only-make

✨ One-liner helper to initialize complex local dependent variable.



NPM version
License


Inspired by Rust's Block Expressions.


| Before | After |
| ------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------- |
| ![before](https://github.com/user-attachments/assets/cf22a531-7a7f-4ec8-aba1-1ca93c6a465d) | ![after](https://github.com/user-attachments/assets/afeca08d-74e2-4abf-aa27-16ba4b702262) |


![hr](https://user-images.githubusercontent.com/39755201/159233055-3bd55a37-7284-46ad-b759-5ab0c13b3828.png)

## Features

- 🔥 Zero dependencies
- ⚡ Only 1 line of source code
- 🚀 Supports on all Browser & Node.js versions
- ✅ Fully typed

## Installation

```bash
npm install only-make
```

## Recipes

- [Features](#features)
- [Installation](#installation)
- [Recipes](#recipes)
- [Basic](#basic)
- [Asynchronous](#asynchronous)
- [Golang Like Error Handling](#golang-like-error-handling)
- [Synchronously](#synchronously)
- [Asynchronously](#asynchronously)
- [Access `this`](#access-this)

### Basic

```js
import { make } from 'only-make'

const value = make(() => {
// Make & return the value
})
```

### Asynchronous

```js
import { make } from 'only-make'

const value = await make(async () => {
// Make & return the value
})
```

### Golang Like Error Handling

###### Synchronously

```js
import { make } from 'only-make'

const [value, error] = make(() => {
// If success
return [new_value, null]

// If error
return [null, new_error]
})

if (!error) {
// Handle `error`
}

// Use `value` safely
```

###### Asynchronously

```js
import { make } from 'only-make'

const [value, error] = await make(async () => {
// If success
return [new_value, null]

// If error
return [null, new_error]
})

if (!error) {
// Handle `error`
}

// Use `value` safely
```

### Access `this`

```js
import { make } from 'only-make'

class MyClass {
doSomething() {
const value = make(() => {
// Use `this`
})
}
}
```