brita
v1.2.0
Published
Object Filter Module for Nodejs. Built in filters for filtering object values by type. Filter by key using regular expression
Downloads
6
Maintainers
Readme
#brita
##Description Object Filter Module for nodejs
###Syntax
brita(object, filter)
####Parameters object
Object to be filtered
filter
Function or Object.
A function to be used to filter the object, should return true or false.
An object containing filter options.
##Usage Install the brita module using npm
$ npm install brita
Basic Example
// require brita module
var brita = require('brita');
// object to be filtered
var myObject = {
key1: 1,
key2: 2,
key3: 3,
key4: 4
};
// filter function
var myFilter = function(value){
// return true for values less than 3
if (value < 3) {
return true;
} else {
return false;
}
};
// apply filter to create new object filtered by brita
var filteredObject = brita(myObject, myFilter);
// output from brita has been assigned to filteredObject
console.log(filteredObject) // returns { key1: 1, key2: 2 }
###Using built in filter options
Built-in filter options can be used by passing an object as the second parameter.
| Key | value | Description | |---------|------------|------------------| | valueType | 'string' |returns all key value pairs with values that are strings | | valueType | 'number' |returns all key value pairs with values that are numbers | | valueType | 'boolean' |returns all key value pairs with values that are booleans | | valueType | 'array' |returns all key value pairs with values that are arrays | | valueType | 'object' |returns all key value pairs with values that are objects | | valueType | 'regex' |returns all key value pairs with values that are regular expressions | | keyFilter | RegExp |returns all key value pairs with keys that match regular expression |
Example using built-in type filter
// require brita module
var brita = require('brita');
// object to be filtered
var myObject = {
key1: 1,
key2: '2',
key3: 3,
key4: '4'
};
// apply filter to create new object filtered by brita
var filteredObject = brita(myObject, {valueType: 'string'});
// output from brita has been assigned to filteredObject
console.log(filteredObject); // returns { key2: '2', key4: '4' }
Example using regular expression to filter by key
// require brita module
var brita = require('brita');
// object to be filtered
var myObject = {
abc1: 'a',
abc2: 'b',
def1: 'c',
def2: 'd'
};
//apply filter to create new object filter by brita
var filteredObject = brita(myObject, {keyFilter: /abc/});
// output from brita has been assigned to filteredObject
console.log(filteredObject); // returns { abc1: 'a', abc2: 'b' }