GitXplorerGitXplorer
f

object-query

public
3 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
5ef1aa6f7d6032e2fc54107350795610b92f3350

Moving object reference out of the closure

ffantactuka committed 11 years ago
Unverified
e1216f0b43312ec0cd319879886aed023d5ee249

3 years old typo fix

ffantactuka committed 12 years ago
Unverified
3afed8475c50011ef0d383ad07bd94aa90364dbc

Rename

ffantactuka committed 14 years ago
Unverified
bf8bb686db735643dff37f49633f1b772dbf1aad

Rename

ffantactuka committed 14 years ago
Unverified
d0ee5ec4d8b1edd1c13a01ebf36a95639f3287c2

Readme

ffantactuka committed 14 years ago
Unverified
4df9f5ceae660c794256046000f45eae72d6b3df

Readme

ffantactuka committed 14 years ago

README

The README file for this repository.

JavaScript Object Query

DSL for JavaScript arrays of objects querying

Usage examples

Initial data that will be queried

  var entireData = [
        { name: 'Bob', id: 1, age: 20 },
        { name: 'Rob', id: 2, age: 42 },
        { name: 'Tim', id: 3, age: 21 },
        { name: 'Hap', id: 4, age: 54 },
        { name: 'Ted', id: 5, age: 43 },
        { name: 'Sam', id: 6, age: 20 },
        { name: 'Liz', id: 7, age: 21 }
  ];

from()

new Jql().from(entireData).where('age >= ?', 25).query() or new Jql(entireData).where('age >= ?', 25).query()

[
    { name: 'Rob', id: 2, age: 42 },
    { name: 'Hap', id: 4, age: 54 },
    { name: 'Ted', id: 5, age: 43 }
]

where()

new Jql(entireData).where('age >= ?', 25).order('age').query() or as a function:

new Jql(entireData).where(function(dataItem) {
    return dataItem.age >= 25;
}).order('age').query()


[
    { name: 'Rob', id: 2, age: 42 },
    { name: 'Ted', id: 5, age: 43 },
    { name: 'Hap', id: 4, age: 54 }
]

limit()

new Jql(entireData).where('age >= ?', 25).limit(2).query()

[
    { name: 'Rob', id: 2, age: 42 },
    { name: 'Hap', id: 4, age: 54 }
]

offset()

new Jql(entireData).where('age >= ?', 25).offset(1).query()

[
    { name: 'Hap', id: 4, age: 54 },
    { name: 'Ted', id: 5, age: 43 }
]

select()

new Jql(entireData).where('age >= ?', 25).select('name').query()

['Rob', 'Hap', 'Ted']

scope()

You're able to create a scopes. It stores all query options within new query model:

var q1 = new Jql(entireData).where('age >= ?', 25);
var q2 = q1.scope();

q1.where('name != 'Rob').query()
[
    { name: 'Hap', id: 4, age: 54 },
    { name: 'Ted', id: 5, age: 43 }
]

q2.where('name != ?', 'Hap').order('age')
[
    { name: 'Rob', id: 2, age: 42 },
    { name: 'Ted', id: 5, age: 43 }
]

This means that when you create a scope you get a brand new query selector based on another one.

Scopes could be usefull if you want to apply save queries options for different data sets

var searchScope = new Jql().select('name').where('age > ', 20).where(function(dataItem) {
    return dataItem.name.match(/[a-z]$/);
}.order('age', true).limit(10).scope();

searchScope.from(dataSet1).query();
searchScope.from(dataSet2).query();
searchScope.from(dataSet3).offset(1).query();