1.0.2 • Published 2 years ago
scope-extensions v1.0.2
Scope Extensions for JS
Note: this repository is entirely based on https://github.com/thedaviddelta/scope-extensions-js. I've just fixed to work in React and also to support modern module resolution from Typescript
Package for using Kotlin's Scope Function Extensions on JavaScript and TypeScript.
Installation
Just install the package
npm i --save scope-extensionsand import it directly to any file.
import "scope-extensions"; // for ESM
require("scope-extensions"); // for CJSUsage
Now your primitives have access to let, run, also, apply, takeIf and takeUnless, just use normally:
const obj = { name: "Carlos", age: 22 };
const name = obj
.let((it) => it.name) // Carlos
.let((it) => it.toUpperCase()) // CARLOS
.let((it) => it.split("").reverse().join("")); // SOLRACBeware that run and apply should be used with annoymous functions(function()) instead of arrow functions(() =>) to get the correctly typed this in the context, example:
const obj = { name: "Carlos", age: 22 };
obj.run(function () {
console.log(this.name);
});License
Copyright © 2020 TheDavidDelta.
This project is MIT licensed.