Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chain-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chain-js

Super simple function chaining APIs

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

chain.js Build Status

chain.js is designed to make chaining-based APIs easy.

Usage

Here's an example:

var Ajax = Chain.with({
  dataType: 'txts',
  method: 'post',
  url: '/'
});

Ajax.flag('get', {method: 'get'});
Ajax.flag('json', {dataType: 'json'});

Ajax.def(function invoke() {
  var request = new XMLHttpRequest();

  return this.with('request', request).promise(function(resolve, reject) {
    request.onreadystatuschange = function() {
      if (request.readyState != XMLHttpRequest.DONE) return;

      if (request.status === 200) {
        resolve(request.responseText);
      } else {
        reject(new Error(request.statusText));
      }
    };

    request.open(this.method, this.url, true);
    request.send(this.data);
  }).then(function(result) {
    switch (this.dataType) {
      case 'json': return JSON.parse(json);
    }
  });
});

var request = Ajax.get.json.with({url: '/albums/12345.json'});

request().then(function(album) {
  alert(album.name);
});

request.with({url: '/'})().then(function())

Mutating Methods

  • Chain.def( fn )
  • Chain.getter( fn )
  • Chain.lazy( fn )
  • Chain.flag( name, properties )

Cloning Methods

  • Chain.with()
  • Chain.clone
  • Chain.tap()
  • Chain.promise()
  • Chain.then()
  • Chain.catch()

#def( namedFunction )

def is used to assign non-enumerable functions. Values can be assigned with #def( name, value ).

#getter( namedFunction )

Similar to def except getters are called without parenthesis:

var Invoice = Chain.clone;

Invoice.getter(function isDue() {
  return this.dueDate < new Date();
});

var invoice = Invoice.set({dueDate: new Date(2020)})

invoice.isDue === false;

#lazy( namedFunction )

lazy is just like getter, but it re-assigns itself as the return value of the passed function.

#with( attributes )

Create a clone with the passed attributes appended:

var person = Chain.with({
  first_name: "John",
  last_name: "Doe"
});

#flag( name, attributes )

flag is a shortcut for returning a clone with attributes appended:

var Order = Chain.clone;

Order.flag('asShipped', {status: 'shipped'});
Order.flag('asDelivered', {status: 'delivered'});

var order1 = Order.asShipped;
var order2 = order1.asDelivered;

order1.status === 'shipped';
order2.status === 'delivered';

#clone

#tap( fn )

tap creates a clone, applies fn to it, and then returns it. The clone is also passed as the first argument.

Chain.tap(function(clone) {
  this === clone;
});

#promise( fn )

chain.js promises require an ES6-compatible window.Promise object. Alternatively, you can set your own: Chain.def('Promise', RSVP.Promise). chain.js promises are lazy: fn isn't invoked until #then() has been called on the chain.

#then( onResolved, onRejected )

#catch( onRejected )

FAQs

Package last updated on 05 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc