From 31083e89edede7fd9b29af5564a9fe3601b4d6e8 Mon Sep 17 00:00:00 2001 From: Laode Muhammad Al Fatih Date: Sun, 15 Dec 2024 22:16:25 +0700 Subject: [PATCH] release: v9.9.9 --- index.d.ts | 3 ++ index.js | 1 + license | 13 ++++++ package.json | 20 +++++++++ readme.md | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 license create mode 100644 package.json create mode 100644 readme.md diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..2bf3d98 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +declare module 'only-make' { + export const make: (makeValue: T | (() => T)) => T +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..49fb888 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +export const make = (makeValue) => typeof makeValue === 'function' ? makeValue() : makeValue diff --git a/license b/license new file mode 100644 index 0000000..acfe1e2 --- /dev/null +++ b/license @@ -0,0 +1,13 @@ +DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + +Copyright (C) 2025 La Ode Muhammad Al Fatih + +Everyone is permitted to copy and distribute verbatim or modified +copies of this license document, and changing it is allowed as long +as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f88a808 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "only-make", + "description": "✨ One-liner helper to initialize complex local dependent variable.", + "version": "9.9.9", + "license": "WTFPL", + "types": "index.d.ts", + "keywords": [ + "only", + "make" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/lamualfa/only-make.git" + }, + "author": { + "name": "Laode Muhammad Al Fatih", + "email": "lamualfa@gmail.com", + "url": "https://github.com/lamualfa" + } +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..15b8112 --- /dev/null +++ b/readme.md @@ -0,0 +1,119 @@ +

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` + }) + } +} +```