Socket
Book a DemoInstallSign in
Socket

co-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-wrapper

Wrap each function in object with a thunk. Useful for generator-based flow control such as co.

0.0.3
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

co-wrapper

Wrap each function in object with a thunk. Useful for generator-based flow control such as co.

How to use it

var EventEmitter = require('events').EventEmitter,
  inherits = require('util').inherits;

var TestClass = module.exports = function() {
  EventEmitter.apply(this, arguments);
  // define some properties
  this._foo = 'foo';
  this._bar = 'bar';

  Object.defineProperty(this, 'foo', {
    get: function() {
      return this._foo;
    },
    set: function(val) {
      this._foo = val;
    }
  });

  Object.defineProperty(this, 'bar', {
    get: function() {
      return this._bar;
    },
    set: function(val) {
      this._bar = val;
    }
  });
};
inherits(TestClass, EventEmitter);

// define async function
TestClass.prototype.getFooBar = function(callback) {
  callback(null, this.foo + this.bar);
};

// define sync function
TestClass.prototype.getFooBarSync = function() {
  return this.foo + this.bar;
};

// wrap oroginal object with co-wrapper
var wrap = require('co-wrapper');
var wrapper = wrapper = wrap(testObj, {
  exclude: ['getFooBarSync'],
  isEventEmitter: true,
  properties: {
    foo: {
      get: '_foo',
      set: function(val) {
        testObj._foo = val;
      }
    },
    bar: {
      get: function() {
        return testObj._bar;
      },
      set: '_bar'
    }
  }
});

(function *() {
  console.log(yield wrapper.getFooBar());
})();

Options

{
  exclude: [], // object functions, that should be excluded
  methods: [], // list of methods to wrap
  isEventEmitter: true, // set to true, if object has event emitter functionality
  properties: {
    name: {
      get: function () {return this._foo}, // getters and setters can be String,
      // that defines source object property, or function
      set: '_foo' // function would be run in source object scope
      // other Object.defineProperty parameters
    }
  }
}

License: MIT

Keywords

co

FAQs

Package last updated on 12 Jun 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.