npm package: @nanomatic/design_patterns
Description: Implementation of the all design patterns
Author: NANOmatic
Latest version: 1.0.0

<p align="center">
  <a href="" rel="noopener">
  <img src="https://d1jnx9ba8s6j9r.cloudfront.net/blog/wp-content/uploads/2019/10/Design-patterns-in-javascript.png" height="250" alt="Command pattern"></a>
</p>
<h3 align="center">@nanomatic/design_patterns</h3>

<div align="center">

[![Status](https://img.shields.io/badge/status-active-success.svg)]()
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)

</div>

---

<p align="center">
    Implementation of the all design patterns.
    <br />
</p>

## 📝 Table of Contents

- [📝 Table of Contents](#-table-of-contents)
- [🏁 Getting Started <a name = "getting_started"></a>](#-getting-started-)
  - [Installing](#installing)
  - [Using](#using)
- [⛏️ Built With <a name = "tech_stack"></a>](#️-built-with-)
- [✍️ Authors <a name = "authors"></a>](#️-authors-)

## 🏁 Getting Started <a name = "getting_started"></a>

### Installing

```
npm i @nanomatic/design_patterns
```

### Using

Example code below:

```ts
import { Command, Commands } from '@nanomatic/design_patterns'

const add = (x: number = 0, y: number = 0) => x + y;
const sub = (x: number = 0, y: number = 0) => x - y;
const mul = (x: number = 0, y: number = 0) => x * y;
const div = (x: number = 0, y: number = 0) => x / y;

const addCommand = (value: number = 0) => new Command(add, sub, value);
const subCommand = (value: number = 0) => new Command(sub, add, value);
const mulCommand = (value: number = 1) => new Command(mul, div, value);
const divCommand = (value: number = 1) => new Command(div, mul, value);

// Helper function
const showResult = ({ getValue }: Commands) => console.log(`Result: ${getValue()}`);

const calculator = new Commands(0);         // Initial value.

calculator.add(addCommand(20));             // Add to queue without execute.
calculator.add(subCommand(16)).execute();   // Be aware - the first command in the queue will be executed only!
calculator.add(mulCommand(2)).execute();    // Be aware - the second command in the queue will be executed only!
calculator.add(divCommand(4)).execute();    // Be aware - the third command in the queue will be executed only!

calculator.execute();                       // You have to execute last command by calling "execute" method once again.

calculator.undo(3);                         // You can also undo some commands...
calculator.redo(3);                         // ...or redo how many times you want.

showResult(calculator);

calculator.clear();                         // Clear all pervious commands.

// ---------------------------------------------------------
// At every time, you can easly pass all commands as a array
// ---------------------------------------------------------
calculator.add([
    addCommand(20),
    subCommand(16),
    mulCommand(2),
    divCommand(4)
]).redoAll();                               // Execute all commands by calling "redoAll" method instead of "execute" 3 times.

calculator.undoAll();                       // You can also undo all commands...
calculator.redo(3);                         // ...or redo how many times you want.

calculator.execute(addCommand(2));          // It's possible to execute new command directly

showResult(calculator);
```

## ⛏️ Built With <a name = "tech_stack"></a>
- [ts-node-dev](https://www.npmjs.com/package/ts-node-dev) - TypeScript Node Dev
- [tsc-watch](https://www.npmjs.com/package/tsc-watch) - The nodemon for TypeScript
- [TSLint](https://www.npmjs.com/package/tslint) - TypeScript linter
- [NodeJs](https://nodejs.org/en/) - Test Environment

## ✍️ Authors <a name = "authors"></a>

- [@NANOmatic](https://github.com/NANOmatic) - Idea & Initial work