JavaScript helper for data processing (sorting, grouping, filtering, aggregates, searching)
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.
src
README.md

README.md

smarti.data.js

JavaScript helper for data processing (sorting, grouping, filtering, aggregates, searching).

Download

Reference

member description
getter(property) returns value getter function for any nested object property
if property name contains dot, use double backslash to escape it (some\\.property)
getter function can be used as setter if second value parameter is specified.
getter method is safe for case if parent property is not set
setter method will create parent property if not set
var dataItem = { Name: 'Some name' };
var getter = smarti.data.getter('Name');
var name = getter(dataItem); // returns 'Some name'
var name = getter(dataItem, 'New name'); // returns 'New name'
getter = smarti.data.getter('Address.City');
getter = smarti.data.getter('some\\.property');
get(property, dataItem) returns value of any nested object property
it is wrapper for smarti.data.getter(property)(dataItem)
var name = smarti.data.get('Name', dataItem);
smarti.data.get('a.b', { a: { b: 'ab' } }); //returns ab
smarti.data.get('aa.b', { a: { b: 'ab' } }); //returns undefined
set(property, dataItem, value) set value of any nested object property
it is wrapper for smarti.data.getter(property)(dataItem, value)
var obj = { c: 123 };
smarti.data.set('a.b', obj, 'test'); //obj = {c: 123, a: {b: 'test'}}
sort(data, options) sort an array of primitive values or complex objects
options can be:
string - represent direction 'asc | desc'
object - { field:'...', method:function(e){ return ... }, dir:'asc | desc' }
array - array of above objects
NB! method changes the original array
var data = [3,7,1,5];
smarti.data.sort(data); //sort ascending
smarti.data.sort(data, 'desc'); //descending
data = [
        { id:1, name:'a' },
        { id:2, name:'a' },
        { id:3, name:'b' }
      ];
//sort by field 'name'
smarti.data.sort(data, { field:'name' });
//multi field sort
smarti.data.sort(data, [{ field:'name' }, { field:'id', dir:'desc' }]);
//sort by string representation of 'id' field
smarti.data.sort(data, { method:function(e){ return e.id.toString() } });
group(data, by, aggregates) returns grouped data with calculated aggregates
by - represent field name
      or function that return primitive or complex object value (group by multiple fields)
      or an array of above parameters (multiple nested groups)
      or can be null (result will be single group with calculated aggregates)
aggregates - represent a javascript object of calculated field aggregates
      where the key is aggregate name (sum, avg, min, max, custom)
      and value is array of field names or/and objects that represents custom fields
      (custom aggregate represented by function or array of functions)
{
  sum: ['Amount', {custom_field: function(e){ return e.SomeField; } }],
  custom: [function(i, item, group){
    //custom implementation of sum aggregate
    group.custom.total = (group.custom.total || 0) + item.Amount;
  }]
}
NB! grouping does not change order of items, so supposed data is sorted already
var data = [
            { year:2015, name:'a', amount:1 },
            { year:2016, name:'a', amount:2 },
            { year:2015, name:'b', amount:3 }
          ];
var groupedData = smarti.data.group(data, 'year', { sum:'amount' });
//returns 
//[
//  {
//    "items":[{"year":2015,"name":"a","amount":1},{"year":2015,"name":"b","amount":3}],
//    "level":0,
//    "value":2015,
//    "count":2,
//    "first":{"year":2015,"name":"a","amount":1},
//    "last":{"year":2015,"name":"b","amount":3},
//    "sum":{"amount":4}
//  },
//  {
//    "items":[{"year":2016,"name":"a","amount":2}],
//    "level":0,
//    "value":2016,
//    "count":1,
//    "first":{"year":2016,"name":"a","amount":2},
//    "last":{"year":2016,"name":"a","amount":2},
//    "sum":{"amount":2}
//  }
//]
groupedData = smarti.data.group(data, function (e) { return { year: e.year, name: e.name } });
//returns 3 groups:
//...
//"value":{"year":2015,"name":"a"}
//"value":{"year":2016,"name":"a"}
//"value":{"year":2015,"name":"b"}
//...
groupedData = smarti.data.group(data, 'year', {
    sum: { customField: function (e) { return e.amount * 10 } }
});
//...
//"sum":{"customField":40}
//"sum":{"customField":20}
//...
groups(groups, callback) recursively iterates data grouped with smarti.data.group
callback - callback function for each group (function(index, group){ ... })
var data = [
            { year:2015, name:'a', amount:1 },
            { year:2016, name:'a', amount:2 },
            { year:2015, name:'b', amount:3 }
          ];
var groupedData = smarti.data.group(data, 'year', { sum:'amount' });
smarti.data.groups(groupedData, function(i, g){ g.customField = g.sum.amount * 10; });
filter(data, filters, operator) returns filtered data
filters - condition or array of conditions represented by functions
operator - operator to concatenate multiple conditions (and, or). Default and
var data = [
            { id:1, name:'a' },
            { id:2, name:'a' },
            { id:3, name:'b' }
          ];
var filteredData = smarti.data.filter(data, function(e){ return e.id > 1; });
filteredData = smarti.data.filter(data, [
  function(e){ return e.id == 1; },
  function(e){ return e.name == 'a'; }
], 'or');
sum(data, field) returns sum of any nested object property
or sum of primitive array (field must be null)
var arr = [1,2,3];
var sum = smarti.data.sum(arr); //returns 6
arr = [{ id:1, amount:1.2 },{ id:2, amount:3.4 }];
sum = smarti.data.sum(arr, 'amount'); //returns 4.6
min(data, field) returns min value of any nested object property
or min value of primitive array (field must be null)
accepts any data type
var arr = [1,2,3];
var min = smarti.data.min(arr); //returns 1
arr = [{ id:1, amount:1.2 },{ id:2, amount:3.4 }];
min = smarti.data.min(arr, 'amount'); //returns 1.2
max(data, field) returns max value of any nested object property
or max value of primitive array (field must be null)
accepts any data type
var arr = [1,2,3];
var max = smarti.data.max(arr); //returns 3
arr = [{ id:1, amount:1.2 },{ id:2, amount:3.4 }];
max = smarti.data.max(arr, 'amount'); //returns 3.4
avg(data, field) returns average of any nested object property
or average of primitive array (field must be null)
var arr = [1,2,3];
var avg = smarti.data.avg(arr); //returns 2
arr = [{ id:1, amount:1.2 },{ id:2, amount:3.4 }];
avg = smarti.data.avg(arr, 'amount'); //returns 2.3
contains(str, substr, cs) returns whether string contains substring
cs - case sensitive (true | false)
accepts any data type (arguments are converted toString())
var contains1 = smarti.data.contains('TEST', 't'); //returns true;
var contains2 = smarti.data.contains('TEST', 't', true); //returns false;
var contains3 = smarti.data.contains(new Date('2016-1-1'), 2016) //returns true;
starts(str, substr, cs) returns whether string starts with substring
cs - case sensitive (true | false)
accepts any data type (arguments are converted toString())
var starts1 = smarti.data.starts('TEST', 't'); //returns true;
var starts2 = smarti.data.starts('TEST', 't', true); //returns false;
var starts3 = smarti.data.starts(new Date('2016-1-1'), 2016) //returns false;
ends(str, substr, cs) returns whether string ends with substring
cs - case sensitive (true | false)
accepts any data type (arguments are converted toString())
var ends1 = smarti.data.ends('TEST', 't'); //returns true;
var ends2 = smarti.data.ends('TEST', 't', true); //returns false;
var ends3 = smarti.data.ends(new Date('2016-1-1'), 2016) //returns false;