Code Monkey home page Code Monkey logo

faltu's Introduction

travis-ci Coverage Status npm version License

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:

<script src="faltu.min.js"></script>

Usage

All the data passed is expected to be an array or objects.

e.g:

[array, array, ..., array]

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 = require('faltu');
var faltuData = new Faltu(data);

In other environments:

var faltuData = new Faltu(data);

Searching

You can use find method for searching. Search for all the guys who are 18 years of age:

var newData = new Faltu(data).find({
  age: 18
}).get();

newData would look something like:

[{
  name: "Doe",
  age: 18
}]

You should always call get at the end if you want an array back. Or It'll just return the faltu instance.

Search for all the guys who are 18 years of age or older:

var newData = new Faltu(data).find({
  age: {
    $gte: 18 // $gte is similar to >= (greater than or equal to)
  }
}).get();

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 = new Faltu(data).find({
  age: [16, 18]
}).get();

newData:

[{
  name: "John",
  age: 16
},{
  name: "Doe",
  age: 18
}]

Passing null, empty object {} or nothing to find means not performing any search. find accepts options as second argument.

e.g:

var newData = new Faltu(data).find({
  age: [16, 18]
}, {
  sort: {
    age: -1
  }
}).get();

Will return the data in descending order by age. Other than sort you can also pass:

  • skip
  • limit
  • unique

Sorting

Use sort to sort the result in descending order by age:

var newData = new Faltu(data).find({
  age: [16, 18]
}).sort({
  age: -1 // 1 = ASC, -1 = DESC
}).get();

newData:

[{
  name: "Doe",
  age: 18
},{
  name: "John",
  age: 16
}]

Limit

Let's get only 1 object back:

var newData = new Faltu(data).find().limit(1).get();

newData:

[{
  name: "John",
  age: 16
}]

Skip

Let's skip 1st object:

var newData = new Faltu(data).find().skip(1).get();

newData:

[{
  name: "Doe",
  age: 18
},{
  name: "Smith",
  age: 22
}]

Skip & Limit

Let's skip 1st object:

var newData = new Faltu(data).find().skip(1).limit(1).get();

newData:

[{
  name: "Doe",
  age: 18
}]

Unique

You can have result returned that is unique by a key.

var newData = new Faltu(data).find().unique('age').get();

Filtering

You can also perform jQuery-esque filtering yourself. Call filter method, pass a function.

var newData = new Faltu(data).find().filter(function (person) {
  return person.age == 16; // return true if you want to keep the object
}).get();

newData:

[{
  name: "John",
  age: 16
}]

Iterate

each iterates over all the records returned.

var newData = new Faltu(data).find(null).each(function(person, index){
  console.log('User name:', person.name);
});

faltu's People

Contributors

moinism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

faltu's Issues

Ability to update and delete found objects

Hi, i am interested in updating, deleting all or only search result(s), is this posible?
eq:

newData= new Faltu(data).find({
  age: 18
}).update(age:19).get();

newData would contain the rows found in data but with the updated value for age, 19 instead of 18...

and also the ability to delete:

newData = new Faltu(data).find({
  age: 19
}).delete().get();

or maybe even use delete instead of find:

newData = new Faltu(data).delete({
  age: 19
}).get();

here newdata will be an array with the deleted rows.
It would be nice if Sorting, Limit, Skip, Skip & Limit, Unique, Filtering and Iterate would work with update and delete too :)

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.