UNPKG

traverse.js

Version:
154 lines (150 loc) 2.83 kB
var t = { "d": {}, "data": function(data) { t.d = data; return t; }, "check": function() { if (t.d) return true; return false; }, "gt": function(val) { if (t.check) { t.d = t.d.filter(function(d) { return d > val }) } return t; }, "gte": function(val) { if (t.check) { t.d = t.d.filter(function(d) { return d >= val }) } return t; }, "lt": function(val) { if (t.check) { t.d = t.d.filter(function(d) { return d < val }) } return t; }, "lte": function(val) { if (t.check) { t.d = t.d.filter(function(d) { return d <= val }) } return t; }, "findByValue": function(val) { var res = []; if (t.check) { t.d.map(function(td, index) { if (td == val) { res.push(td) } }) t.d = res; } return t; }, "findByValues": function(val) { var res = []; if (t.check) { t.d.map(function(td, index) { val.map(function(v) { if (td == v) { res.push(td) } }) }) t.d = res; } return t; }, "findByKey": function(key) { var res = []; if (t.check) { t.d.map(function(td, index) { Object.keys(td).map(function(k) { if (k == key) { res.push(td) } }) }) t.d = res; } return t; }, "findByKeys": function(ks) { var res = []; if (t.check) { t.d.map(function(td, index) { Object.keys(td).map(function(k) { ks.map(function(key) { if (k == key) { res.push(td) } }) }) }) t.d = res; } return t; }, "findByKeyValue": function(key, value) { var res = []; if (t.check) { t.d = t.d.map(function(td,index) { if (td[key] == value) { res.push(td) } }) } }, "sortObjKeys": function(expr) { var res = {}; var ks = Object.keys(t.d).sort(function(a,b) { if (expr.toUpperCase() == "ASC") { a < b; } else { a > b; } }); ks.map(function(k) { res[k] = t.d[k]; }) t.d = res; return t; }, "sortValues": function(expr) { t.d = t.d.sort(function(a,b) { if (expr.toUpperCase() == "ASC") { a < b; } else { a > b; } }); return t; }, "sortByKey": function(expr, key) { t.d.sort(function(a,b) { if (expr.toUpperCase() == "ASC") { a[key] < b[key]; } else { a[key] > b[key]; } }) }, "result": function() { return t.d; } }; module.exports = t; var dataset = [4,3,2] var testing = t.data(dataset).sortValues().result(); console.log(testing);