# clarifyjs

> A library to write more clear and declarative JavaScript code

Latest version **0.1.2** (published 2017-12-03) · MIT license · 0 weekly downloads

## Install

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

## 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.2 |
| Published | 2017-12-03 |
| First published | 2017-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 219 |
| Author | Omid Habibi |
| Maintainers | omidh |

## Links

- npm: https://www.npmjs.com/package/clarifyjs
- Repository: https://github.com/omidh28/clarifyJs
- Issues: https://github.com/omidh28/clarifyJs/issues
- npm.io page: https://npm.io/package/clarifyjs

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.0

## Recent versions

- 0.1.2 (latest) — 2017-12-03
- 0.0.2 — 2017-11-28
- 0.0.1 — 2017-11-27

## README

# clarifyJs

ClarifyJs allows you to easily create chained methods that can be executed in any order you want.

This feature is particularly useful when you want to make your code more friendly and declarative, For Example we have these chained methods for sending a message to a specific group of users:
```javascript
send('hello everyone!').to('friends');
```
This works fine with normal method chaining but what about when we want to create more complex chained methods?
For example we want to filter some of our friends out:
```javascript
send('hello everyone!').to('friends').except('john');
```
Normal javascript methods are executed from left-to-right, So when we reach "except" filter the message has already been sent to all friends. However ClarifyJs allows to overcome this limitation by allowing to priorities methods to allow execution in any order.

ClarifyJs also allows controlling async methods. Suppose we want to log a message to console when the message has been sent:
```javascript
send('hello everyone!').to('friends').then.log('message has been sent.');
```
The problem is that if "send" method is a async method then the log message will be printed before the message is actually sent. ClarifyJs allows to decide whether the process should wait for a specific async method to complete before moving on or just execute and pass by it.

These features come in price of making the whole process async.

---

## Installation

In a browser:
```html
<script src="clarify.js"></script>
```

Using npm:
```shell
$ npm i --save clarifyjs
```

---

## Usage
First the methods routes must be defined.

**Syntax:**
```javascript
// Array of routes objects
const routesData = [ ...];
const root = clarify({ routes: routesData });
```

### Routes

Routes are objects that have at least the two property **handler** and **path**.

| Property        | Type           | Default Value | Description  |
| :-------------: | :-------------: | :-----: | :-----: |
| path| String | None | The address of the method, Root address is empty string. Wildcard can be used.
| handler | Function | None | The method function.
| storeResultAs | String | Empty String | Store method's result with this name.
| inject | String/Array | Empty Array | Name of the stored results to pass to the method handler
| controller | String | () | "()" if the method should be called with paranthesis otherwise "[]"
| awaitForHandler | Boolean | False | Whether the process should wait for the method handler to complete or not.
| Priority | Number | -Infinity | Priority of the method execution, Higher priority methods will be executed before others.

### Example
Demonstration of the example explained above:
```javascript
const routesData = [
	{
		path: '',
		handler: sendMessageFunc,
		inject: ['contacts'],
  		awaitForHandler: true,
		priority: 0
	},
	{
		path: 'to',
		handler: contactSelectorFunc,
		storeResultAs: 'contacts',
		priority: 2
	},
        {
		path: 'to.except',
		handler: contactFilterFunc,
		inject: ['contacts'],
		storeResultAs: 'contacts',
		priority: 1
	},
	{
		path: 'to.except.then.log',
		handler: logFunc,
		priority: -Infinity
	}
];

const send = clarify({ routes: routesData });
send('hello everyone!').to('friends').except('john').then.log('message has been sent!');
```

### Storage

ClarifyJs has an integrated storage system that can store and pass values to methods.
These values may also have default values:

```javascript
const storage = {
	contacts: 'friends'
};

const send = clarify({ routes, storage });
```

[![Travis build status](http://img.shields.io/travis/omidh28/clarifyJs.svg?style=flat)](https://travis-ci.org/omidh28/clarifyJs)
[![Maintainability](https://api.codeclimate.com/v1/badges/f76a9e87744ce4283c42/maintainability)](https://codeclimate.com/github/omidh28/clarifyjs/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/f76a9e87744ce4283c42/test_coverage)](https://codeclimate.com/github/omidh28/clarifyjs/test_coverage)
[![Dependency Status](https://david-dm.org/omidh28/clarifyJs.svg)](https://david-dm.org/omidh28/clarifyJs)
[![devDependency Status](https://david-dm.org/omidh28/clarifyJs/dev-status.svg)](https://david-dm.org/omidh28/clarifyJs#info=devDependencies)

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