Overview
MrT is a tool for making simple and complex chained interfaces on javascript libraries.
The resulting syntax is easy-to-read while still being flexible and 100% vanilla-js.
// Example interface for a web serverconst webServer =publicactionaccountControllerlistactionaccountControllershowauthenticatedactionaccountControllerupdateactionaccountControllercreateactionaccountControllerdelete;
Installation
The easiest and fastest way to install MrT is through the node package manager
:
$ npm install mrt --save
API Guide
.initialize(...constructorArguments)
Whenever one constructor extends another, .super
must be called in the extended object's constructor before this
can be called.
This can be a gotcha for many developers, so MrT has a built-in pseudo-constructor called .initialize
that can be used by extended objects without the need to call .super
.
;// Without using initialize{supername;this;thisnamename;}// Using initialize{this;thisnamename;}
.properties(...propertyNames)
Define a simple chainable property that sets and gets a raw value:
{this;}const person = ;personname"Jana";personname; // "Jana"
.multi
{thismulti;}const person = ;person;person; // ["Jana Banana", "Jananana"]
.aggregate
{thisaggregate;}const person = ;person;person;person; // ["Jana Banana", "Jananana"]
.multi.aggregate
{thismultiaggregate;}const person = ;person;person;person; // [["Toledo", "OH"], ["Colorado Springs", "CO"]]
.multi.aggregate.flat
{thismultiaggregateflat;}const person = ;person;person;person; // [ "one", "two", "three", "four" ]
.merged
{thismerged;}const person = ;person;person;person; // {"CO": "Boulder", "KS": "Wichita"}
.filter(filterFunction)
{this;}{const newValue = ;if newValuereturn newValue;elsereturn value;}const person = ;person;person; // 1
.then(callback)
Call a synchronous callback each time a new value is set on the property
{this;}{// No returns. Just a simple synchronous callback.}const person = ;person;person; // 1
.link(linkName, linkConstructor)
A link creates a method which returns a new instance of the provided linkConstructor
.
The new instance is given a copy of all methods from the parent link. This is what enables MrT to chain multiple tiers.
;{thislink"wheel" Wheel;}{this;this;}const car = ;const wheel1 = car;car;
.getter
;{thislink"wheel" Wheelgetter;}{}const car = ;const wheel1 = carwheel;carwheelwheelwheel;
.inherit(...propertyNames)
;{this;thislink"door" Door;}{}const car = ;carcolor"Red";const door = cardoor;doorcolor; // "Red"
.into(collectionName)
;{this;thislink"door" Door;}{}const car = ;cardoordoor;cardoorslength; // 2
.into(collectionName).key(keyName)
;{this;thislink"door" Door;}{this;this;}const car = ;car;cardoorsleft; // left door objectcardoorsright; // right door object
.then(thenFunction)
;{this;thislink"door" Door;}{}const car = ;car;
.apply(...passedArguments)
;{this;thislink"door" Door;}{car; // Automatically passed by .arguments(this) in Carcolor; // "green"}const car = ;car;