glomb v0.3.4
Glomb
Extended wrapper of the built-in Array for a MongoDB-like handling
Are there any improvements, hints or bug fixes? Let me know!
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ɔ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:
// 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:
// ES6 module syntax
import Glomb from 'glomb';
// Node traditional syntax
var Glomb = require('glomb').default;
Afterwards it is possible to create an empty instance:
const todos = new Glomb();
Alternatively pass single parameters of Object
type:
const todos = new Glomb(todoNo1, todoNo2);
Or make use of destructuring and pass an array of objects:
const todos = new Glomb(...listOfTodos);
Glomb.all()
Get list of stored objects:
/**
* @immutable
*
* @returns {Array.<?Object>} List of stored data
*/
todos.all();
Glomb.get()
Get stored object by MongoDB _id
:
/**
* @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:
/**
* @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:
/**
* @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:
/**
* @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:
/**
* @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:
/**
* @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
/**
* @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.
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago