# fndi

> Simple, functional, ligthweight, dependencyless DI library for JS

Latest version **0.3.0** (published 2018-05-03) · Unlicense license · 0 weekly downloads

## Install

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

## 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.3.0 |
| Published | 2018-05-03 |
| First published | 2018-04-28 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 285.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | jpichardo |
| Keywords | ioc, functional, dependency-injection |

## Links

- npm: https://www.npmjs.com/package/fndi
- Repository: https://github.com/pichardoJ/fndi.js
- Homepage: https://github.com/pichardoJ/fndi.js#readme
- Issues: https://github.com/pichardoJ/fndi.js/issues
- npm.io page: https://npm.io/package/fndi

## Recent versions

- 0.3.0 (latest) — 2018-05-03
- 0.2.0 — 2018-05-01
- 0.1.2 — 2018-04-28
- 0.1.1 — 2018-04-28
- 0.1.0 — 2018-04-28

## README

# fnDI.js

Simple, functional, ligthweight, dependencyless DI/IOC library for JS

## Usage

Install using yarn or npm

```bash
yarn add fndi

npm install fndi
```

Getting started

```javascript
// Function sintax
const scope = require('fndi');

function A() {}

// Class Sintax
class B {
  constructor(a) {
    this.a = a;
  }
}

// Registration Function
const registration = registry => {
  registry({ type: A });
  registry({ type: B });
};

function main(resolve, args) {
  const bInstance = resolve(B);
}

const scopedMain = scope(registration, main);

scopedMain(args);
```

## Register

The registration is done within a function that takes a `registry` function as
argument.

```javascript
const registration = registry => {

  //Only type registry
  registry({ type: MyTypedClass });

  // Named Registry
  registry({ name: 'MyNamedInstance', type: MyNamedClass });

  // Provide a single instance every time
  registry({ type: MyClassWithValue, value: { a: 1 } });

  // Registering a Factory function
  registry({
    type: MyClassWithFactory,
    factory: resolve => resolve(MyClassWithFactory);
  });

  // Delegate the class
  registry({
    type: MyDelegatedClass,
    by: AnotherCompletelyDifferentClass
  })

};
```

### Registry Entry

Each registry entry (the argument for the `registry` function) must follow the
following definition:

```typescript
interface Entry<T> {
  name?: string;
  type: Type<T>;
  value?: T;
  factory?: resolve: Function => T;
  by?: Type;
  persist?: boolean;
}
```

## Scopes

`scope` is a function wrapper, it provides a `resolve` function which can be
used in the underlying function to get instances of the defined types (by either
type or name).

```js
function main(resolve, arg1, arg2) {
  const fooInstance = resolve(Foo);
  const barInstance = resolve('BarRegistryName');
}

const scopedMain = scope(registration, main);

scopedMain(arg1, arg2);
```

# TODO

- [ ] async dependencies
- [ ] Lazy dependencies

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