Class: obop

obop

new obop()

This class provides a set of methods where() / order() / update() / view() which manipulate array such like MongoDB-style object operators do.
Properties:
Name Type Description
system.name String Read only. System name: "obop"
system.version String Read only. System version: e.g. "0.0.1"
Source:
See:
Examples
// node.js
var obop = require('obop')();
var src = [ { a: 1 }, { a: 2 }, { a: 3 } ];
var out = obop.where( src, { a: 2 } ); // => [ { a: 2 } ]
<script src="obop.min.js"></script>
<script>
  var src = [ { a: 1 }, { a: 2 }, { a: 3 } ];
  var out = obop.where( src, { a: 2 } ); // => [ { a: 2 } ]
</script>

Methods

order(array, sort) → {Array}

This sorts an array of items, or generates a sort function compiled from order-by-clause sort parameters object. Since version 0.0.7, dot notation (e.g. foo.bar.baz) is supported to handle child nodes.
Parameters:
Name Type Argument Description
array Array <optional>
source array to sort
sort Object | Array | function sort parameters or function
Source:
Returns:
  • Sorted array of items (if source array given)
    Type
    Array
  • Sort function compiled for Array.prototype.sort() method (if source array not given)
    Type
    function
  • Error instance (if source array not given but invalid sort parameters given)
    Type
    Error
Example
var list = [
   { name: "apple", price: 50 },
   { name: "orange", price: 10 },
   { name: "pineapple", price: 70 },
   { name: "grape", price: 30 }
];

// without obop
var out1 = list.sort(function(a, b) {
    return a.price - b.price;
});
console.log(out1);

// with obop
var order = { price: 1 };
var out2 = obop.order(list, order);
console.log(out2);

// compile a sort function
var order = { price: 1 };
var sorter = obop.order(order);
if (sorter instanceof Error) throw sorter;
var out3 = list.sort(sorter);
console.log(out3);

update(array, update) → {Array}

This updates an array of items, or generates an update compiled function from update-clause operators object. Since version 0.0.4, dot notation (e.g. foo.bar.baz) is supported to handle child nodes.
Parameters:
Name Type Argument Description
array Array <optional>
source array to update
update Object | function update operators or function
Source:
See:
Returns:
  • Array of updated items (if source array given)
    Type
    Array
  • Update function compiled for Array.prototype.map() method (if source array not given)
    Type
    function
  • Error instance (if source array not given but invalid update operaters given)
    Type
    Error
Example
var list = [
   { name: "apple", price: 50 },
   { name: "orange", price: 10 },
   { name: "pineapple", price: 70 },
   { name: "grape", price: 30 }
];

// update without obop
var out1 = list.filter(function(item) {
    item.sale = true;
    item.price -= 5;
    return item;
});
console.log(out1);

// update with obop
var update = { $set: { sale: true }, $inc: { price: -5 } };
var out2 = obop.update(list, update);
console.log(out2);

// compile a update function
var update = { $set: { sale: true }, $inc: { price: -5 } };
var updater = obop.update(update);
if (updater instanceof Error) throw updater;
var out3 = list.map(updater);
console.log(out3);

view(array, param) → {Array}

This filters fields of items, or generates a projection function compiled from view mapping parameters object. Since version 0.0.6, dot notation (e.g. foo.bar.baz) is supported to handle child nodes.
Parameters:
Name Type Argument Description
array Array <optional>
source array to map
param Object | function output fields or function
Source:
Returns:
  • Array of filtered items (if source array given)
    Type
    Array
  • Projection function compiled for Array.prototype.map() method (if source array not given)
    Type
    function
  • Error instance (if source array not given but invalid output fields specified)
    Type
    Error
Example
var list = [
   { name: "apple", price: 50 },
   { name: "orange", price: 10 },
   { name: "pineapple", price: 70 },
   { name: "grape", price: 30 }
];

// map without obop
var out1 = list.map(function(item) {
    return { name: item.name };
});
console.log(out1);

// map with obop
var view = { name: 1 };
var out2 = obop.view(list, view);
console.log(out2);

// compile a map function
var view = { name: 1 };
var filter = obop.view(view);
if (filter instanceof Error) throw filter;
var out3 = list.map(filter);
console.log(out3);

where(array, param) → {Array}

This search items from an array, or generates a conditional function compiled from where-clause operators object. Since version 0.0.4, dot notation (e.g. foo.bar.baz) is supported to handle child nodes.
Parameters:
Name Type Argument Description
array Array <optional>
source array to search
param Object | function query selector or function
Source:
See:
Returns:
  • Array of matched items (if source array given)
    Type
    Array
  • Conditional function compiled for Array.prototype.filter() method (if source array not given)
    Type
    function
  • Error instance (if source array not given but invalid query selector given)
    Type
    Error
Example
var list = [
   { name: "apple", price: 50 },
   { name: "orange", price: 10 },
   { name: "pineapple", price: 70 },
   { name: "grape", price: 30 }
];

// search without obop
var out1 = list.filter(function(item) {
    return item.name == "orange";
});
console.log(out1);

// search with obop
var where = { name: "orange" };
var out2 = obop.where(list, where);
console.log(out2);

// compile a search function
var where = { name: "orange" };
var filter = obop.where(where);
if (filter instanceof Error) throw filter;
var out3 = list.filter(filter);
console.log(out3);