🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

to-emitter

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

to-emitter

Convert any object into an event-emitter, and emits events with the name of any any method called on the object.

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

to-emitter NPM version

Convert any object into an event-emitter, and emits events with the name of any any method called on the object.

Wraps each method on an object so that an event is emitted with the name of the method along with the arguments passed to the method.

Example

// an old object with a property that has a function value
var obj = {cache: {}};
obj.set = function(key, val) {
  obj[key] = val;
  return obj;
};

// pass an instance of your favorite event emitter, 
// see `example.js` for a working example
toEmitter(emitter, obj);

// now `obj` is an emitter, and `obj.set` will emit a `set` event.
obj.on('set', function(key, value) {
  console.log('set:', key, value);
});

obj.set('foo', 'bar');

Install

Install with npm

$ npm i to-emitter --save

Usage

var Emitter = require('component-emitter');
var toEmitter = require('to-emitter');

var obj = {
  cache: {},
  set: function (key, val) {
    obj.cache[key] = val
    return obj;
  },
  get: function (key) {
    return obj.cache[key];
  }
};

obj.set('foo', 'bar');

var emitter = Emitter({});
toEmitter(emitter, obj);

obj.on('set', function (key, val) {
  console.log('set:', key, val);
});

obj.on('get', function (key) {
  console.log('get:', key);
});

obj.set('a', 'b');
obj.set('c', 'd');
obj.get('a');
obj.get('c');
  • collection-visit: Visit a method over the items in an object, or map visit over the objects… more
  • minimist-events: Add events to minimist, ~30 sloc.
  • map-visit: Map visit over an array of objects.
  • object-visit: Call the given method on each value in the given object.

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.

This file was generated by verb-cli on August 06, 2015.

Keywords

emit

FAQs

Package last updated on 07 Aug 2015

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