Socket
Socket
Sign inDemoInstall

delegates

Package Overview
Dependencies
0
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    delegates

delegate methods and accessors to another property


Version published
Weekly downloads
14M
decreased by-8.21%
Maintainers
2
Install size
7.03 kB
Created
Weekly downloads
 

Package description

What is delegates?

The 'delegates' npm package provides a convenient way to create delegate methods and properties on objects, allowing you to forward method calls or property accesses to another object. This is particularly useful for implementing patterns like composition over inheritance, where you want to expose a certain interface on an object without inheriting from another class.

What are delegates's main functionalities?

Method Delegation

This feature allows you to delegate method calls from one object to another. In the provided code, method calls to 'instance.method()' are delegated to 'obj.method()', allowing 'instance' to expose the method of 'obj'.

{
  const delegates = require('delegates');
  const proto = {};
  const obj = { method: function() { return 'method called'; } };
  delegates(proto, 'obj').method('method');
  const instance = Object.create(proto);
  instance.obj = obj;
  console.log(instance.method()); // 'method called'
}

Property Delegation

This feature enables the delegation of property access. In the example, accessing 'instance.prop' will return the value of 'obj.prop'. This is useful for exposing properties of one object on another without direct inheritance.

{
  const delegates = require('delegates');
  const proto = {};
  const obj = { prop: 'value' };
  delegates(proto, 'obj').access('prop');
  const instance = Object.create(proto);
  instance.obj = obj;
  console.log(instance.prop); // 'value'
}

Other packages similar to delegates

Readme

Source

delegates

Node method and accessor delegation utilty.

Installation

$ npm install delegates

Example

var delegate = require('delegates');

...

delegate(proto, 'request')
  .method('acceptsLanguages')
  .method('acceptsEncodings')
  .method('acceptsCharsets')
  .method('accepts')
  .method('is')
  .access('querystring')
  .access('idempotent')
  .access('socket')
  .access('length')
  .access('query')
  .access('search')
  .access('status')
  .access('method')
  .access('path')
  .access('body')
  .access('host')
  .access('url')
  .getter('subdomains')
  .getter('protocol')
  .getter('header')
  .getter('stale')
  .getter('fresh')
  .getter('secure')
  .getter('ips')
  .getter('ip')

API

Delegate(proto, prop)

Creates a delegator instance used to configure using the prop on the given proto object. (which is usually a prototype)

Delegate#method(name)

Allows the given method name to be accessed on the host.

Delegate#getter(name)

Creates a "getter" for the property with the given name on the delegated object.

Delegate#setter(name)

Creates a "setter" for the property with the given name on the delegated object.

Delegate#access(name)

Creates an "accessor" (ie: both getter and setter) for the property with the given name on the delegated object.

Delegate#fluent(name)

A unique type of "accessor" that works for a "fluent" API. When called as a getter, the method returns the expected value. However, if the method is called with a value, it will return itself so it can be chained. For example:

delegate(proto, 'request')
  .fluent('query')

// getter
var q = request.query();

// setter (chainable)
request
  .query({ a: 1 })
  .query({ b: 2 });

License

MIT

Keywords

FAQs

Last updated on 17 Oct 2014

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc