npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mrt

v0.4.7

Published

Helps build expressive chained interfaces.

Downloads

42

Readme

npm version license type Build Status Coverage Status Code Climate bitHound Score bitHound Dependencies npm downloads Source: ECMAScript 6

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 server

const webServer = new WebServer()

.public
  .get("/account")
    .action(accountController.list)
  .get("/account/:id")
    .action(accountController.show)

.authenticated
  .authorized("owner", "admin")
    .put("/account/:id")
      .action(accountController.update)
  .authorized("admin")
    .post("/account")
      .action(accountController.create)
    .delete("/account/:id")
      .action(accountController.delete)

.listen(3000);

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.

import Component from "mrt";

// Without using initialize
class Person extends Component {
  constructor(name) {
    super(name);
    this.properties("name");
    this.name(name);
  }
}

// Using initialize
class Person extends Component {
  initialize(name) {
    this.properties("name");
    this.name(name);
  }
}

.properties(...propertyNames)

Define a simple chainable property that sets and gets a raw value:

class Person extends Component {
  initialize() {
    this.properties("name");
  }
}

const person = new Person();

person.name("Jana");
person.name(); // "Jana"

.multi

class Person extends Component {
  initialize() {
    this.properties("nicknames").multi;
  }
}

const person = new Person();

person.nicknames("Jana Banana", "Jananana");
person.nicknames(); // ["Jana Banana", "Jananana"]

.aggregate

class Person extends Component {
  initialize() {
    this.properties("nicknames").aggregate;
  }
}

const person = new Person();

person.nicknames("Jana Banana");
person.nicknames("Jananana");
person.nicknames(); // ["Jana Banana", "Jananana"]

.multi.aggregate

class Person extends Component {
  initialize() {
    this.properties("citiesVisited").multi.aggregate;
  }
}

const person = new Person();

person.citiesVisited("Toledo", "OH");
person.citiesVisited("Colorado Springs", "CO");
person.citiesVisited(); // [["Toledo", "OH"], ["Colorado Springs", "CO"]]

.multi.aggregate.flat

class Person extends Component {
  initialize() {
    this.properties("values").multi.aggregate.flat;
  }
}

const person = new Person();

person.values("one", "two");
person.values("three", "four");
person.values(); // [ "one", "two", "three", "four" ]

.merged

class Person extends Component {
  initialize() {
    this.properties("favoriteCities").merged;
  }
}

const person = new Person();

person.mergedValues({"CO": "Colorado Springs", "KS": "Wichita"});
person.mergedValues({"CO": "Boulder"});
person.mergedValues(); // {"CO": "Boulder", "KS": "Wichita"}

.filter(filterFunction)

class Person extends Component {
  initialize() {
    this.properties("favoriteNumber").filter(this.castIntegers);
  }

  castIntegers(value) {
    const newValue = parseInt(value);
    if (newValue) {
      return newValue;
    } else {
      return value;
    }
  }
}

const person = new Person();

person.favoriteNumber("1");
person.favoriteNumber(); // 1

.then(callback)

Call a synchronous callback each time a new value is set on the property

class Person extends Component {
  initialize() {
    this.properties("favoriteNumber").then(this.doSomething);
  }

  doSomething(value) {
    // No returns. Just a simple synchronous callback.
  }
}

const person = new Person();

person.favoriteNumber("1");
person.favoriteNumber(); // 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.

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.link("wheel", Wheel);
  }
}

class Wheel extends Component {
  initialize(diameter) {
    this.properties("diameter");
    this.diameter(diameter);
  }
}

const car = new Car();

const wheel1 = car.wheel(10);

car
.wheel(10)
.wheel(10)
.wheel(10);

.getter

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.link("wheel", Wheel).getter;
  }
}

class Wheel extends Component {}

const car = new Car();

const wheel1 = car.wheel;

car
.wheel
.wheel
.wheel;

.inherit(...propertyNames)

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.properties("color");
    this.link("door", Door).inherit("color");
  }
}

class Door extends Component {}

const car = new Car();

car.color("Red");

const door = car.door;

door.color(); // "Red"

.into(collectionName)

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.properties("color");
    this.link("door", Door).into("doors");
  }
}

class Door extends Component {}

const car = new Car();

car
.door
.door;

car.doors.length; // 2

.into(collectionName).key(keyName)

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.properties("color");
    this.link("door", Door).into("doors").key("side");
  }
}

class Door extends Component {
  initialize(side) {
    this.properties("side");
    this.side(side);
  }
}

const car = new Car();

car
.door("left")
.door("right");

car.doors.left; // left door object
car.doors.right; // right door object

.then(thenFunction)

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.properties("color");
    this.link("door", Door).then(newDoor => {
      // Called each time a link is created
    });
  }
}

class Door extends Component {}

const car = new Car();

car
.door()
.door();

.apply(...passedArguments)

import Component from "mrt";

class Car extends Component {
  initialize() {
    this.properties("color");
    this.link("door", Door).apply(this);
  }
}

class Door extends Component {
  initialize(car, color) {
    car; // Automatically passed by .arguments(this) in Car
		color; // "green"
  }
}

const car = new Car();

car.door("green");