Faltu
Search sort, filter, limit and iterate over an array of objects in Mongo-style.
Installation
In NodeJS:
npm install faltu --save
For other projects simply download and include the file in your pages:
Usage
All the data passed is expected to be an array or objects.
e.g:
[record, record, ..., record]
All the data returned is also of the same type.
For example:
var data = name: "John" age: 16 name: "Doe" age: 18 name: "Smith" age: 22;
Pass the array to constructor:
In NodeJS:
var Faltu = ;var faltuData = data;
Searching
You can use find
method for searching.
Search for all the guys who are 18 years of age:
var newData = data;
newData
would look something like:
name: "Doe" age: 18
Search for all the guys who are 18 years of age or older:
var newData = data;
newData
:
name: "Doe" age: 18 name: "Smith" age: 22
Other supported comparison operators in find
are:
$lt
: <$lte
: <=$gt
: >$ne
: !=
Search for all the guys who are 18 or 16 years of age:
var newData = data;
newData
:
name: "John" age: 16 name: "Doe" age: 18
Passing null
, empty object {}
or nothing to find
means not performing any search.
Sorting
Use sort
to sort the result in descending order by age
:
var newData = data;
newData
:
name: "Doe" age: 18 name: "John" age: 16
Limit
Let's get only 1 object back:
var newData = data;
newData
:
name: "John" age: 16
Skip
Let's skip 1st object:
var newData = data;
newData
:
name: "Doe" age: 18 name: "Smith" age: 22
Skip & Limit
Let's skip 1st object:
var newData = data;
newData
:
name: "Doe" age: 18
Filtering
You can also perform jQuery
-esque filtering yourself. Call filter
method, pass a function
.
var newData = data;
newData
:
name: "John" age: 16
Iterate
each
iterates over all the records returned.
var newData = data;