# radar.js

> A simple wrapper for objects that emits events *around* specified methods. It effectively sandwiches object methods between two event emitters.

Latest version **0.1.7** (published 2014-08-18) · 0 weekly downloads

## Install

```sh
npm install radar.js
pnpm add radar.js
yarn add radar.js
bun add radar.js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2014-08-18 |
| First published | 2014-08-18 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Michael Giuliana |
| Maintainers | rpnzl |

## Links

- npm: https://www.npmjs.com/package/radar.js
- Repository: https://github.com/rpnzl/radar.js
- Issues: http://github.com/rpnzl/radar.js/issues
- npm.io page: https://npm.io/package/radar.js

## Dependencies (2)

- [async](https://npm.io/package/async.md) ^0.9.0
- [lodash](https://npm.io/package/lodash.md) ^2.4.1

## Recent versions

- 0.1.7 (latest) — 2014-08-18
- 0.1.6 — 2014-08-18
- 0.1.5 — 2014-08-18
- 0.1.4 — 2014-08-18
- 0.1.2 — 2014-08-18

## README

# Radar.js

A simple wrapper for objects that emits events *around* specified methods. It effectively
sandwiches object methods between two event emitters.

[![Build Status](https://travis-ci.org/rpnzl/radar.js.svg?branch=master)](https://travis-ci.org/rpnzl/radar.js)
[![NPM version](https://badge.fury.io/js/radar.js.svg)](http://badge.fury.io/js/radar.js)


## Installation

    npm install radar.js


## Quick Start

```javascript
var Radar = require("./lib")
  , radar = new Radar({ methods: ["leave"] });

var bear = radar.wrap({
  name: "Bear",
  leave: function () {
    console.log(this.name + " is leaving...");
  },
  stay: function () {
    console.log(this.name + " is staying...");
  }
}, {
  prefix:  "bear",
  methods: ["stay"]
});

radar.before("bear:leave", function () {
  console.log(this.name + " is thinking about leaving...");
});

radar.after("bear:leave", function () {
  console.log(this.name + " left.");
});

radar.before("bear:stay", function () {
  console.log(this.name + " is thinking about staying...");
});

radar.after("bear:stay", function () {
  console.log(this.name + " stayed.");
});

bear.leave();
bear.stay();

// Bear is thinking about leaving...
// Bear is leaving...
// Bear is thinking about staying...
// Bear is staying...
// Bear left.
// Bear stayed.
```


## Sync vs. Async

This library works with both synchronous and asynchronous methods, but it does
require an adherence to a certain asynchronous convention -- **callback
parameters MUST be the last parameter supplied to a method**. For example...

```javascript
var okay = radar.wrap({
  asyncMethod: function (name, done) {
    done(null, name.toUpperCase());
  }
}, { prefix: "okay" });

var notOkay = radar.wrap({
  asyncMethod: function (done, name) {
    done(null, name.toUpperCase());
  }
}, { prefix: "notOkay" });
```


## API

### Radar(options) [constructor]

Create a new Radar object with some optional configuration.

#### Usage

```javascript
var radar = new Radar();
var radar = new Radar({ methods: ["leave"] });
```

#### Arguments

name|req|type
----|---|----
options|n|`obj`

#### Options

name|req|type|default|description
----|---|----|-------|-----------
separator|n|`str`|`":"`|separates the prefix and method name (prefix:method)
methods|n|`arr`|`[]`|list of methods to wrap, if they exist

### Radar.wrap(object, options)

Wraps designated methods within an object with before and after event emitters.

#### Usage

```javascript
var bear = radar.wrap({
  name:  "Bear",
  leave: function () {
    console.log(this.name + " is leaving...");
  }
}, { prefix: "bear" });

var fox = radar.wrap({
  name: "Fox",
  stay: function () {
    console.log(this.name + " is staying...");
  }
}, {
  prefix:    "fox",
  separator: "/"
  methods:   ["stay"]
})
```

#### Arguments

name|req|type
----|---|----
object|y|`obj`
options|y|`obj`

#### Options

name|req|type|default|description
----|---|----|-------|-----------
prefix|y|`str`|`""`|prefixes the event names attached to this object
separator|n|`str`|`":"`|overrides the default separator for the `object` argument's events only
methods|n|`arr`|`[]`|list of additional methods to wrap for the `object` argument only

### Radar.before(eventName, handler)

Trigger handler *before* the wrapped method is called on the `object`. The context (`this`) within this
method is the `object` which the event was registered against.

#### Usage

```javascript
radar.before("bear:leave", function () {
  console.log(this.name + " is about to leave..."); // Bear is about to leave...
});
```

#### Arguments

name|req|type
----|---|----
eventName|y|`str`
handler|y|`func`

### Radar.after(eventName, handler)

Trigger handler *after* the wrapped method is called on the `object`. The context (`this`) within this
method is the `object` which the event was registered against.

#### Usage

```javascript
radar.after("bear:leave", function () {
  console.log(this.name + " left."); // Bear left.
});
```

#### Arguments

name|req|type
----|---|----
eventName|y|`str`
handler|y|`func`

## License

[MIT](http://opensource.org/licenses/MIT)

---
_Source: https://npm.io/package/radar.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
