# dfn

> Dynamic Function Definition, using map pattern guard something.

Latest version **0.1.0** (published 2016-05-23) · MIT license · 0 weekly downloads

## Install

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

## 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.0 |
| Published | 2016-05-23 |
| First published | 2016-05-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ddchen |
| Maintainers | ddchen |
| Keywords | function, guard, pattern, dynamic |

## Links

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

## Dependencies (1)

- [keysmap](https://npm.io/package/keysmap.md) 0.0.1

## Recent versions

- 0.1.0 (latest) — 2016-05-23
- 0.0.1 — 2016-05-08

## README

# dfn

Dynamic Function Definition, using map pattern guard something.

[![Build Status](https://travis-ci.org/LoveKino/dfn.svg)](https://travis-ci.org/LoveKino/dfn.svg)
[![Coverage Status](https://coveralls.io/repos/github/LoveKino/dfn/badge.svg?branch=master)](https://coveralls.io/github/LoveKino/dfn?branch=master)

## install

`npm i dfn --save`

## example

```js
var dfn = require('dfn');
var defFun = dfn.defFun;
var special = dfn.special;
var guard = dfn.guard;

var fun = defFun();
special(fun, [1, 2], 5);
guard(fun, (x) => x < 0, (x) => -1 * x);

console.log(fun(1, 2)); // 5
console.log(fun(-3)); // 3
```

## create a function

```js
var dfn = require('dfn');
var defFun = dfn.defFun;
var fun = defFun();
```

## special case

You can set a special case for a function.

- `special(fun, args, ret)`

```js
var dfn = require('dfn');
var defFun = dfn.defFun;
var special = dfn.special;
var fun = defFun();

special(fun, [1, 2], 5);

console.log(fun(1, 2)); // 5
```

## guard

You can set a guard for function

- `guard(fun, condition, expression)`

When arguments satisfy condition (a function), function fun will get result from expression (a function).

```js
var dfn = require('dfn');
var defFun = dfn.defFun;
var guard = dfn.guard;

var fun = defFun();
guard(fun, (x) => x < 0, (x) => -1 * x);

console.log(fun(-3)); // 3
```

## default

You can set default expression for function.

When there is no special case or condition, default function will execute.

`def(fun, defFun)`

```js
var dfn = require('dfn');

var fun = defFun();
guard(fun, (x) => x < 0, (x) => -1 * x);
def(fun, (x) => x * x);

console.log(fun(3)); // 9
```

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