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

chain-methods

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

Create a class to chain methods from an object.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Chain Methods

This module exposes a single function to transform a plain object into a class, where methods can be chained.

Installation

npm install chain-methods

Usage

const chainMethods = require('chain-methods');
const Path = chainMethods(require('path'));

const rootPath = new Path(__dirname);
const imagesPath = rootPath.join('dist/static/images');

// With `chainMethods`:
const stream = fs.createReadStream(imagesPath.join('foo.jpg').value);

Why

This module was created for experimentation purposes. It works well for APIs like node's path and url modules, where the first argument of each function is consistent.

It makes code a little easier to read by avoiding this type of nesting:

const foo = obj.method1(obj.method2('bar'), 'baz');

Limitations

The first argument of each method must be consistent.

const Calculator = chainMethods({
    // Good:
    add: (n1, n2) => n1 + n2,
    subtract: (n1, n2) => n1 - n2,
    
    // Bad:
    operation(operationName, ...args) {
        return this[operationName](...args);
    },
});
const value = new Calculator(0)
    .add(10)
    .subtract(2)
    
    // This line won't work because of the argument order of `operation`:
    .operation('add', 20)
    
    .value;

In the example above, the first argument of operation is not the number being operated on. It is inconsistent with add and subtract, so will not work in chaining.

FAQs

Package last updated on 08 Apr 2017

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