Bringing Kotlin's scope functions to JavaScript / TypeScript (and maybe adding a few more nice-to-haves).
- Kotlin's scope functions adapted for JavaScript / TypeScript
.let()
.with()
.run()
.apply()
.also()
.takeIf()
.takeUnless()
- Async / Await Support
- Unscoped
run()
- Unscoped
run()
aliased toreturnOf()
# pnpm
pnpm add --save scope-utilities
# npm
npm install --save scope-utilities
import { scope } from 'scope-utilities';
- Returns the result of the function you pass in.
- First argument of the function is the object you pass in
scope()
const sameTimeTomorrow: string = scope(new Date()).let((it: Date) => {
it.setDate(it.getDate() + 1);
return it.toISOString();
});
- Returns the result of the function you pass in.
this
inside of the function is the object you pass inscope()
const sameTimeTomorrow: string = scope(new Date()).let(function () {
this.setDate(this.getDate() + 1);
return this.toISOString();
});
Note: Does not work with arrow functions () => {}
as the
context is being re-bound.
Exactly the same as .with()
as JavaScript / TypeScript does
not have the concept of extension functions.
- Returns the object you pass in
scope()
this
inside of the function is the object you pass inscope()
const sameTimeTomorrow: Date = scope(new Date()).apply(() => {
this.setDate(this.getDate() + 1);
});
- Returns the object you pass in
scope()
- First argument of the function is the object you pass in
scope()
const sameTimeTomorrow: Date = scope(new Date()).also((it: Date) => {
it.setDate(it.getDate() + 1);
});
- Returns the object you pass in
scope()
if the function you pass in returnstrue
- Returns null if the function you pass in returns
false
- First argument of the function is the object you pass in
scope()
const red: string | null = scope("red").takeIf((it: string) => {
return it === "red";
});
red === "red"; // true
const blue: string | null = scope("blue").takeIf((it: string) => {
return it === "red";
});
blue === null; // true
- Returns the object you pass in
scope()
if the function you pass in returnsfalse
- Returns null if the function you pass in returns
true
- First argument of the function is the object you pass in
scope()
const red: string | null = scope("red").takeUnless((it: string) => {
return it === "red";
});
red === null; // true
const blue: string | null = scope("blue").takeUnless((it: string) => {
return it === "red";
});
blue === "blue"; // true
A lot of the method names are reserved keywords. Usualy transpilers, runtimes, and bundlers are smart enough to handle this based on context, but if for some reason you don't want to use reserved keywords, the following aliases are available:
const scoped = scope(null);
scoped.let === scoped.letFunc;
scoped.with === scoped.withFunc;
scoped.run === scoped.runFunc;
scoped.apply === scoped.applyFunc;
scoped.also === scoped.alsoFunc;
scoped.takeIf === scoped.takeIfFunc;
scoped.takeUnless === scoped.takeUnlessFunc;
import { run } from 'scope-utilities';
const sameTimeTomorrow: Date = run(() => {
const date = new Date();
date.setDate(date.getDate() + 1);
return date;
});
import { returnOf } from 'scope-utilities';
const sameTimeTomorrow: Date = returnOf(() => {
const date = new Date();
date.setDate(date.getDate() + 1);
return date;
});
All the function work it async / await or promise based functions. Just make sure to also await on the return value.
import { scope } from 'scope-utilities';
const sameTimeTomorrow: string = await scope(new Date()).let(async (it: Date) => {
it.setDate(it.getDate() + 1);
return it.toISOString();
});
MIT