# Glomb
#### Extended wrapper of the built-in Array for a MongoDB-like handling
---

**Are there any improvements, hints or bug fixes? Let me know!**

1. [Introduction](#introduction)
2. [Installation](#installation)
3. [Usage](#usage)
4. [Testing](#testing)
5. [Contribution](#contribution)
6. [License](#license)

## Introduction
This module is implemented in ECMAScript 6 (v2015). Therefore the development dependencies are based on `babel`.
Additionally `eslint` and `mocha` are used to grant a high quality implementation. Furthermore there are used some experimental features:

- Function bind `::`
- Decorators
- Class fields and static properties
- Object spread properties

**Glomb** ['gl&#596;mb] is Swabian term for something that doesn't work well or is difficult to handle. Should be an oxymoron/paradox :)

## Installation
For installation use the [Node Package Manager](https://github.com/npm/npm):
```
// production version with ES5 syntax
$ npm install --save glomb
```

or clone the repository:
```
// development version with ES6 syntax
$ git clone https://github.com/felixheck/Glomb
```

## Usage
Every method tagged with `@immutable` returns a new instance of an array or object.

#### Contruct new instance
First you have to import the module:
``` js
// ES6 module syntax
import Glomb from 'glomb';

// Node traditional syntax
var Glomb = require('glomb').default;
```

Afterwards it is possible to create an empty instance:
``` js
const todos = new Glomb();
```

Alternatively pass single parameters of `Object` type:
``` js
const todos = new Glomb(todoNo1, todoNo2);
```

Or make use of destructuring and pass an array of objects:
``` js
const todos = new Glomb(...listOfTodos);
```

#### Glomb.all()
Get list of stored objects:
``` js
/**
 * @immutable
 *
 * @returns {Array.<?Object>} List of stored data
 */

todos.all();
```

#### Glomb.get()
Get stored object by MongoDB `_id`:
``` js
/**
 * @immutable
 *
 * @param {string} idx The ID to be searched for
 * @returns {Object} Single item or empty object
 */

const dataId = '507f191e810c19729de860ea';
todos.get(dataId);
```

#### Glomb.find()
Get stored objects matching a MongoDB `_id` or custom filter:
``` js
/**
 * @immutable
 *
 * @param {String | Function} cond The conditions to be searched for
 * @returns {Array.<?Object>} List of matched items
 */

todos.find(item => item.name == 'foo');
```

#### Glomb.has()
Check if stored data containing at least one matching object:
``` js
/**
 * @param {String | Function} cond The conditions to be searched for
 * @returns {boolean} Data contains at least one matching item
 */

todos.has(item => item.name == 'foo');
```

#### Glomb.insert()
Add new items to internal data storage:
``` js
/**
 * @immutable
 *
 * @param {Array.<?Object>} items The items to be pushed
 * @returns {number} List of data including the inserted items
 */

todos.insert(todoNo1, todoNo2);
```

#### Glomb.update()
Update matching items with passed fields:
``` js
/**
 * @immutable
 *
 * @param {String | Function} cond The conditions to be searched for
 * @param {Object} fields The fields to be updated
 * @returns {Array.<?Object>} List of data including the updated items
 */

import Glomb from 'glomb';

const { increment, decrement, unset } = Glomb.operations;

todos.update(dataId, {
    'foo.bar': 'bar',
    'foo.senseOfLife': increment(2),  // increment the current value by 2
    'foo.biest': decrement(),         // decrement the current value by 1
    'foo.todos[0].bar': unset()       // unset the property
});
```

#### Glomb.remove()
Remove matching items from internal data storage:
``` js
/**
 * @immutable

 * @param {String | Function} cond The conditions to be searched for
 * @returns {Array.<?Object>} The updated stored data
 */

todos.remove(item => item.name == 'foo');
```

#### Glomb.reset()
Purge the internal data storage
``` js
/**
 * @immutable
 *
 * @returns {Array} The empty list of stored data
 */

todos.reset();
```

## Testing
First you have to install all dependencies:
```
$ npm install
```

To execute all unit tests once, use:
```
$ npm test
```

or to run the tests after each update, use:
```
$ npm start
```

To get information about the test coverage, use:
```
$ npm run coverage
```

## Contribution
Fork this repository and push in your ideas.

Do not forget to add corresponding tests to keep up 100% test coverage.

## License
The MIT License

Copyright (c) 2016 Felix Heck

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.