scope-methods
v1.1.0
Published
Kotlin-inspired scope methods using safe module augmentation
Readme
scope-methods
A collection of Kotlin-inspired scope methods for JavaScript/TypeScript, implemented using safe module augmentation. These methods enhance the readability and maintainability of your code by providing a more expressive way to work with objects and values within specific scopes.
Available Methods
pipe: Executes a provided function withthisvalue as its argument and returns its result.also: Executes a provided function withthisvalue as its argument and returnsthisvalue.keepIf: Returnsthisvalue if it satisfies the given predicate orundefinedif it does not.
Usage
import { also, keepIf, pipe } from "scope-methods";
({ a: 6, b: 7 })
[also](console.log) // Logs: { a: 6, b: 7 }
[pipe](({ a, b }) => (a * b))
[keepIf]((value) => value > 15); // Returns: 42Which is roughly equivalent to:
const input = { a: 6, b: 7 };
console.log(input); // Logs: { a: 6, b: 7 }
const { a, b } = input;
const value = a * b;
value > 15 ? value : undefined; // Returns: 42Note that with "scope-methods", the intermediate variables are avoided, leading to more concise and readable code.
