scope-extensions
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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-extensions

and import it directly to any file.

import "scope-extensions"; // for ESM
require("scope-extensions"); // for CJS

Usage

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("")); // SOLRAC

Beware 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.

Package Sidebar

Install

npm i scope-extensions

Weekly Downloads

22

Version

1.0.2

License

MIT

Unpacked Size

69.9 kB

Total Files

7

Last publish

Collaborators

  • ceopaludetto