A lightweight, extensible object query tool for JavaScript / JSON objects.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md Updating small oversight in example. Aug 5, 2012
example.js
order.json Just throwing some code out there for this. Aug 2, 2012
package.json Renaming package to refine. Aug 5, 2012
refine.js Renaming package to refine. Aug 5, 2012

README.md

refine

Refine is a lightweight, extensible object query tool for JavaScript.

Refine offers:

  • A fluent, easy-to-read API
  • Custom-defined filters
  • Simplicity

Example

var refine = require('refine');
var select = refine.select;
var order = require('./order');

var entities = order.entities.map(function(entity) {
  entity.rel = entity.rel.split(/\s/);
  entity.class = entity.class.split(/\s/);
  return entity;
});

var customerInfo = 
select(entities)
  .where('rel')
    .contains('http://x.io/rels/customer')
  .and('class')
    .contains('info')
.map(function(customer) {
  return customer.properties;
})
.shift();

console.log(customerInfo);
// { customerId: 'pj123', name: 'Peter Joseph' }

Installation

$ npm install refine

Usage

refine.select(collection)

select takes an array as the collection parameter and returns a Query object for chaining.

Query#where(property)

Query#where creates a Filter object, with property as the property name to use when running a filter on the query. This method returns the newly created Filter object.

Filter#equals(val)

Executes an equality comparison filter using the provided val string. Returns an Array.

Filter#contains(val)

Executes a contains filter using the provided val string. Returns an Array.

Custom Filters

Custom filters can be assigned using refine.Filter.prototype.

Define this.action inside the filter to generate the filtered array.

Example of a custom filter:

var refine = require('refine');

refine.Filter.prototype.isInformational = function() {
  this.action = function(collection) {
    return collection.filter(function(item) {
      if (item.class && item.class.indexOf('info') > -1) {
        return item;
      }
    });
  };

  return refine.Query.querify(this.query.run());
};

Custom filter in action:

var select = require('refine').select;

var informational = 
  select(order.entities)
  .where()
  .isInformational()
  .shift();

console.log(informational);

/*
{ class: 'info customer',
  rel: 'http://x.io/rels/customer',
  properties: 
   { customerId: 'pj123',
     name: 'Peter Joseph' },
  links: 
   [ { rel: 'self',
       href: 'http://api.x.io/customers/pj123' } ] }
*/

License

MIT/X11