# enti

> A super light-weight key-value 'observable' wrapper that works with references.

Latest version **6.4.4** (published 2020-10-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install enti
pnpm add enti
yarn add enti
bun add enti
```

## 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 | 6.4.4 |
| Published | 2020-10-12 |
| First published | 2015-01-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 44.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Maintainers | korynunn |

## Links

- npm: https://www.npmjs.com/package/enti
- Repository: https://github.com/KoryNunn/enti
- Homepage: https://github.com/KoryNunn/enti#readme
- Issues: https://github.com/KoryNunn/enti/issues
- npm.io page: https://npm.io/package/enti

## Dependencies (3)

- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.0
- [flat-merge](https://npm.io/package/flat-merge.md) ^1.0.0
- [is-instance](https://npm.io/package/is-instance.md) ^1.0.1

## Recent versions

- 6.4.4 (latest) — 2020-10-12
- 6.4.3 — 2020-10-11
- 6.4.2 — 2020-04-08
- 6.4.1 — 2020-01-09
- 6.4.0 — 2019-10-15
- 6.3.0 — 2019-10-11
- 6.2.0 — 2019-10-11
- 6.1.3 — 2019-05-19
- 6.1.2 — 2019-03-21
- 6.1.1 — 2019-01-24
- 6.1.0 — 2018-12-19
- 6.0.7 — 2018-11-29
- 6.0.6 — 2018-09-26
- 6.0.5 — 2018-09-24
- 6.0.4 — 2018-07-23
- … 55 more at https://npm.io/package/enti/versions

## README

# enti

A super light-weight key-value 'observable' wrapper that works with references.

# usage

```
var Enti = require('enti');

var object = {
    foo: 'bar'
};

var model1 = new Enti(object);

model1.on('foo', function(foo){
    // object.foo changed. do something.
});

model1.set('foo', 'baz');
```

Enti knows about references too:


```
var model2 = new Enti(object);

model2.on('foo', function(foo){
    // object.foo changed. do something.
});

model1.set('foo', 'baz'); // sent into a different Enti, triggers events for all enti's
```

And you can use wildcards to watch for events:

Single level:
```
model1.on('*', function(value){
    // object.<anything> changed. do something.
    // value will be undefined, because the target path contains a wildcard.
});

model1.set('foo', 'baz');
```

Any level:
```
model1.on('**', function(value){
    // object.<anything>.<anything>.<anything>.<etc...> changed. do something.
    // value will be undefined, because the target path contains a wildcard.
});

model1.set('foo', 'baz');
```

Which can be combined with other keys:


```
model1.on('foo.*.bar', function(value){
    // object.foo.<anything>.bar changed. do something.
    // value will be undefined, because the target path contains a wildcard.
});

model1.set('foo', 'baz', {
    bar:1
});
```

And used with filters, to specify what data you are actually after:

```
model1.on('foo|*.bar', function(foo){
    // object.foo.<anything>.bar changed. do something.
    // model.get(left hand side of the pipe (|)) will be passed as the first parameter.
});

model1.set('foo', 'baz', {
    bar:1
});
```

All handlers will be passed an event object with the object the event was raised on, and the key and value that caused the event:

```
model1.on('something', function(value, event){
    event.key === 'something';
    event.value === value;
    event.target === model1.get('.');
});
```

## API

### .get(path)

returns the value on the attached object at `path`

You can get the currently attached object using `'.'`

```javascript

model.get('.') // -> object

```

### .set(path, value)

sets the value on the attached object at `path` to `value`

### .remove(path)

`delete`s or `splice`s the `path` on the attached object

### .push([path,] value)

`push`s the `value` into the attached object, or the array at `path` on the attached object.

`push` will throw if the target of the push is not an array.

### .update([path,] value[, options])

`updates`s the target at `path` to match `value.

`options` can contain:

`strategy`: 'merge' (default) or 'morph'

`merge`: Merge `value` into target, retaining untouched keys in `target`
`morph`: Merge `value` into target, removing any keys that are not in `value`

### .move([path,] index)

`move`s the target at `path` to `index`

`move` will throw if the target of the move is not an array.

## Paths

The path syntax is fairly minimal, with only 4 special tokens

# . (dot/period)

Used to drill down into the object. eg:

```
var bar = get('foo.bar');
```

is equivilent to

```
var bar = object.foo.bar;
```

# * (Wildcard)

Will match events from any key on the object

# ** (Feralcard)

Recursive wildecard. Will match events form any key, and any sub-key  on the object.

# | (Filter)

Functionally identical to a dot/period, but separates the target of an event from the rest of the path.

Given the below event listener:

```
model.on('foo|bar.baz', function(target){...})
```

If the model raises an event on `foo.bar.baz`, Enti will `get('foo')`, and pass the result to the handler.

## Lazy initialisation

If you want to create an Enti to be attached to data later, you can pass `false` to the constructor:

```
var unattachedModel = new Enti(false);
```

This creates a model that will not listen to or be able to cause events to fire, meaning lower cycles for other Enti's that are attached.

You can check if a model is attached with the method `.isAttached()`

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