# properties-and-events

> Convenience methods for adding properties and events with fluent api to objects

Latest version **1.2.0** (published 2020-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install properties-and-events
pnpm add properties-and-events
yarn add properties-and-events
bun add properties-and-events
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2020-10-08 |
| First published | 2016-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | lukebarlow |

## Links

- npm: https://www.npmjs.com/package/properties-and-events
- Repository: https://github.com/lukebarlow/properties-and-events
- Homepage: https://github.com/lukebarlow/properties-and-events#readme
- Issues: https://github.com/lukebarlow/properties-and-events/issues
- npm.io page: https://npm.io/package/properties-and-events

## Dependencies (1)

- [d3-dispatch](https://npm.io/package/d3-dispatch.md) ^2.0.0

## Recent versions

- 1.2.0 (latest) — 2020-10-08
- 1.1.0 — 2020-10-08
- 1.0.1 — 2018-09-04
- 1.0.0 — 2018-08-20
- 0.0.5 — 2017-10-09
- 0.0.4 — 2017-08-03
- 0.0.2 — 2016-10-28
- 0.0.1 — 2016-10-14

## README

properties-and-events
====================

Add properties and events with fluent api calls to your objects.


```

import { addProperties, addGetters, addEvents } from 'properties-and-events'

class Car {
  constructor () {
    addProperties(this, 'colour', 'upholstery', 'sunroof')
    addGetters(this, 'doors', 'engineSize')
    addEvents(this, 'start', 'stop')
    this._doors = 4
    this._engineSize = 1.98
  }

  drive (time) {
    this.fire('start')
    setTimeout(() => {
      this.fire('stop')
    }, time)
  }
}

const myCar = new Car()
  .colour('red')
  .upholstery('leather')
  .sunroof(false)
  .on('start', () => {console.log('car started')})
  .on('stop', () => {console.log('car stopped')})

console.log(myCar.doors()) // logs 4
console.log(myCar.colour()) // logs 'red'
console.log(myCar._colour) // also logs 'red'
```

**`addProperties`** adds fluent getter/setter methods to your object. If the
method is called with an argument, then it is treated as a setter, and returns
a reference to the original object to allow method chaining. If called without
any argument then it is treated as a getter and will return the value of the
property. The actual value of the property is stored in a field with the name
of the method prefixed with an underscore. i.e. `o.colour()` gets the value 
stored in `o._colour`.

**`addGetters`** works similarly to `addProperties` but without the setter
functionality.

**`addEvents`** adds events to your object. Two methods will be added to your
object. `myObject.on(type, handler)` is used to subscribe to events. Internally,
use `this.fire(type, arg1, arg2, ...)` to fire events. `arg1, arg2, ...` will
be passed to the handler.

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