Comparing version 1.4.0 to 1.5.0
@@ -57,4 +57,551 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
'use strict';Object.defineProperty(exports,"__esModule",{value:true});var _slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally {try{if(!_n&&_i["return"])_i["return"]();}finally {if(_d)throw _e;}}return _arr;}return function(arr,i){if(Array.isArray(arr)){return arr;}else if(Symbol.iterator in Object(arr)){return sliceIterator(arr,i);}else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value" in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _invariant=__webpack_require__(1);var _invariant2=_interopRequireDefault(_invariant);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i];}return arr2;}else {return Array.from(arr);}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var MAP_ACTION='MAP_ACTION';var FILTER_ACTION='FILTER_ACTION';var SLICE_ACTION='SLICE_ACTION';var action=Symbol('action');var meta=Symbol('meta');var reduceD=Symbol('reduceDispatch');var noActions=[];var Arrays=new WeakMap();var ArraysMetadata=new WeakMap();var required=function required(){throw new Error('Dodo - missing required argument');};var Dodo=function(){function Dodo(){var array=arguments.length<=0||arguments[0]===undefined?required():arguments[0];var actions=arguments.length<=1||arguments[1]===undefined?noActions:arguments[1];_classCallCheck(this,Dodo);Arrays.set(this,array);if(!ArraysMetadata.has(array))ArraysMetadata.set(array,{length:array.length,columns:new Set(Object.keys(array.index))});this.actions=actions;}_createClass(Dodo,[{key:'toArray',value:function toArray(){return [].concat(_toConsumableArray(this));}},{key:'uniq',value:function uniq(){return [].concat(_toConsumableArray(new Set(this)));}},{key:Symbol.iterator,value:regeneratorRuntime.mark(function value(){var array,Index,nrOfActions,_iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,row,lastActionIndex,lastRowIndex,rowIndex,actionIndex,I,_action;return regeneratorRuntime.wrap(function value$(_context){while(1){switch(_context.prev=_context.next){case 0:array=Arrays.get(this);Index=array.index;nrOfActions=this.actions.length;if(nrOfActions){_context.next=33;break;}_iteratorNormalCompletion=true;_didIteratorError=false;_iteratorError=undefined;_context.prev=7;_iterator=array[Symbol.iterator]();case 9:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=16;break;}row=_step.value;_context.next=13;return row;case 13:_iteratorNormalCompletion=true;_context.next=9;break;case 16:_context.next=22;break;case 18:_context.prev=18;_context.t0=_context['catch'](7);_didIteratorError=true;_iteratorError=_context.t0;case 22:_context.prev=22;_context.prev=23;if(!_iteratorNormalCompletion&&_iterator.return){_iterator.return();}case 25:_context.prev=25;if(!_didIteratorError){_context.next=28;break;}throw _iteratorError;case 28:return _context.finish(25);case 29:return _context.finish(22);case 30:return _context.abrupt('return');case 33:lastActionIndex=this.actions.length-1;lastRowIndex=ArraysMetadata.get(array).length-1;rowIndex=-1;case 36:if(!(rowIndex++<lastRowIndex)){_context.next=57;break;}row=array[rowIndex];actionIndex=-1;I=Index;case 40:if(!(actionIndex++<lastActionIndex)){_context.next=55;break;}_action=this.actions[actionIndex];if(!(_action.type==SLICE_ACTION)){_context.next=46;break;}if(!(++_action.counter<_action.first)){_context.next=45;break;}return _context.abrupt('break',55);case 45:if(_action.counter==_action.last)lastRowIndex=rowIndex;case 46:if(!(_action.type==FILTER_ACTION)){_context.next=49;break;}if(_action(row,I)){_context.next=49;break;}return _context.abrupt('break',55);case 49:if(_action.type==MAP_ACTION){row=_action(row,I);I=_action.I||I;}if(!(actionIndex==lastActionIndex)){_context.next=53;break;}_context.next=53;return row;case 53:_context.next=40;break;case 55:_context.next=36;break;case 57:case 'end':return _context.stop();}}},value,this,[[7,18,22,30],[23,,25,29]]);})},{key:action,value:function value(action){return new Dodo(Arrays.get(this),[].concat(_toConsumableArray(this.actions),[action]));}},{key:'filter',value:function filter(){var fn=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(typeof fn=='function','Dodo#filter(fn) — fn not a function');fn.type=FILTER_ACTION;return this[action](fn);}},{key:'map',value:function map(){var fn=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(typeof fn=='function','Dodo#map(fn) — fn not a function');fn.type=MAP_ACTION;return this[action](fn);}},{key:'col',value:function col(){var name=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(this[meta].columns.has(name),'Dodo#col(name) — name '+name+' not in index');return this.map(function(row,I){return row[I[name]];});}},{key:'cols',value:function cols(){var _this=this;var names=arguments.length<=0||arguments[0]===undefined?required():arguments[0];names.forEach(function(name){return (0,_invariant2.default)(_this[meta].columns.has(name),'Dodo#cols(names) — name '+name+' not in index');});var fn=function fn(row,I){return names.map(function(name){return row[I[name]];});};fn.I={};names.forEach(function(name,i){return fn.I[name]=i;});return this.map(fn);}},{key:'slice',value:function slice(){var start=arguments.length<=0||arguments[0]===undefined?0:arguments[0];var end=arguments[1];var len=this[meta].length;if(typeof end!='number')end=len;if(end==len&&start==0)return this;(0,_invariant2.default)(start>=0,'Dodo#slice(start, end) — start smaller than 0');(0,_invariant2.default)(end>=0,'Dodo#slice(start, end) — end smaller than 0');(0,_invariant2.default)(start<end,'Dodo#slice(start, end) — end larger than start');return this[action]({type:SLICE_ACTION,counter:-1,first:start,last:end-1});}},{key:'skip',value:function skip(){var amount=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(amount>0,'Dodo#skip(amount) — amount smaller than 0');return this.slice(amount);}},{key:'take',value:function take(){var amount=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(amount>0,'Dodo#take(amount) — amount smaller than 0');return this.slice(0,amount);}},{key:'groupBy',value:function groupBy(){var _this2=this;var name=arguments.length<=0||arguments[0]===undefined?required():arguments[0];(0,_invariant2.default)(this[meta].columns.has(name),'Dodo#group(name) — name '+name+' not in index');var map=[];var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{var _loop=function _loop(){var val=_step2.value;map.push([val,_this2.filter(function(d,I){return d[I[name]]==val;})]);};for(var _iterator2=this.col(name).uniq()[Symbol.iterator](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){_loop();}}catch(err){_didIteratorError2=true;_iteratorError2=err;}finally {try{if(!_iteratorNormalCompletion2&&_iterator2.return){_iterator2.return();}}finally {if(_didIteratorError2){throw _iteratorError2;}}}return Flock(map);}},{key:'reduce',value:function reduce(){var fn=arguments.length<=0||arguments[0]===undefined?required():arguments[0];var init=arguments.length<=1||arguments[1]===undefined?required():arguments[1];(0,_invariant2.default)(typeof fn=='function','Dodo#reduce(fn) — fn not a function');var _iteratorNormalCompletion3=true;var _didIteratorError3=false;var _iteratorError3=undefined;try{for(var _iterator3=this[Symbol.iterator](),_step3;!(_iteratorNormalCompletion3=(_step3=_iterator3.next()).done);_iteratorNormalCompletion3=true){var _row=_step3.value;init=fn(init,_row);}}catch(err){_didIteratorError3=true;_iteratorError3=err;}finally {try{if(!_iteratorNormalCompletion3&&_iterator3.return){_iterator3.return();}}finally {if(_didIteratorError3){throw _iteratorError3;}}}return init;}},{key:'reduceEach',value:function reduceEach(){var fn=arguments.length<=0||arguments[0]===undefined?required():arguments[0];var init=arguments.length<=1||arguments[1]===undefined?required():arguments[1];(0,_invariant2.default)(typeof fn=='function','Dodo#reduceEach(fn) — fn not a function');var _iteratorNormalCompletion4=true;var _didIteratorError4=false;var _iteratorError4=undefined;try{for(var _iterator4=this[Symbol.iterator](),_step4;!(_iteratorNormalCompletion4=(_step4=_iterator4.next()).done);_iteratorNormalCompletion4=true){var _row2=_step4.value;init=_row2.map(function(col,i){return fn(init[i]||init,col);});}}catch(err){_didIteratorError4=true;_iteratorError4=err;}finally {try{if(!_iteratorNormalCompletion4&&_iterator4.return){_iterator4.return();}}finally {if(_didIteratorError4){throw _iteratorError4;}}}return init;}},{key:reduceD,value:function value(fn,init){var oneColumn=!!this.actions.filter(function(act){return act.type==MAP_ACTION&&!act.I;}).length;if(oneColumn)return this.reduce(fn,init);else return this.reduceEach(fn,init);}},{key:'count',value:function count(){return this[reduceD](function(count){return ++count;},0);}},{key:'sum',value:function sum(){return this[reduceD](function(sum,el){return sum+el;},0);}},{key:'min',value:function min(){return this[reduceD](function(min,el){return min<el?min:el;},Infinity);}},{key:'max',value:function max(){return this[reduceD](function(max,el){return max>el?max:el;},-Infinity);}},{key:'mean',value:function mean(){return this.sum()/this.count();}},{key:meta,get:function get(){return ArraysMetadata.get(Arrays.get(this));}}]);return Dodo;}();exports.default=Dodo;function Flock(map,method,args){map=new Map(map);if(method){var _iteratorNormalCompletion5=true;var _didIteratorError5=false;var _iteratorError5=undefined;try{for(var _iterator5=map.entries()[Symbol.iterator](),_step5;!(_iteratorNormalCompletion5=(_step5=_iterator5.next()).done);_iteratorNormalCompletion5=true){var _step5$value=_slicedToArray(_step5.value,2);var key=_step5$value[0];var dodo=_step5$value[1];map.set(key,dodo[method].apply(dodo,_toConsumableArray(args)));}}catch(err){_didIteratorError5=true;_iteratorError5=err;}finally {try{if(!_iteratorNormalCompletion5&&_iterator5.return){_iterator5.return();}}finally {if(_didIteratorError5){throw _iteratorError5;}}}}if(map.values().next().value instanceof Dodo){var _iteratorNormalCompletion6=true;var _didIteratorError6=false;var _iteratorError6=undefined;try{var _loop2=function _loop2(){var method=_step6.value;map[method]=function(){for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key];}return Flock(map,method,args);};};for(var _iterator6=Object.getOwnPropertyNames(Dodo.prototype)[Symbol.iterator](),_step6;!(_iteratorNormalCompletion6=(_step6=_iterator6.next()).done);_iteratorNormalCompletion6=true){_loop2();}}catch(err){_didIteratorError6=true;_iteratorError6=err;}finally {try{if(!_iteratorNormalCompletion6&&_iterator6.return){_iterator6.return();}}finally {if(_didIteratorError6){throw _iteratorError6;}}}}return map;} | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _invariant = __webpack_require__(1); | ||
var _invariant2 = _interopRequireDefault(_invariant); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var MAP_ACTION = 'MAP_ACTION'; | ||
var FILTER_ACTION = 'FILTER_ACTION'; | ||
var SLICE_ACTION = 'SLICE_ACTION'; | ||
var action = Symbol('action'); | ||
var index = Symbol('index'); | ||
var meta = Symbol('meta'); | ||
var dispatch = Symbol('dispatch'); | ||
var noActions = []; | ||
var Arrays = new WeakMap(); | ||
var ArraysMetadata = new WeakMap(); | ||
var required = function required() { | ||
throw new Error('Dodo - missing required argument'); | ||
}; | ||
var Dodo = function () { | ||
function Dodo() { | ||
var array = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
var actions = arguments.length <= 1 || arguments[1] === undefined ? noActions : arguments[1]; | ||
_classCallCheck(this, Dodo); | ||
Arrays.set(this, array); | ||
if (!ArraysMetadata.has(array)) ArraysMetadata.set(array, { | ||
length: array.length, | ||
columns: new Set(Object.keys(array.index)) | ||
}); | ||
this.actions = actions; | ||
} | ||
_createClass(Dodo, [{ | ||
key: dispatch, | ||
value: function value(method, fn, init) { | ||
if (this[index] == Arrays.get(this).index) return this[method](fn, init);else return this[method + 'Each'](fn, init); | ||
} | ||
}, { | ||
key: action, | ||
value: function value(action) { | ||
return new Dodo(Arrays.get(this), [].concat(_toConsumableArray(this.actions), [action])); | ||
} | ||
}, { | ||
key: Symbol.iterator, | ||
value: regeneratorRuntime.mark(function value() { | ||
var array, Index, nrOfActions, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, row, lastActionIndex, lastRowIndex, rowIndex, _row, actionIndex, I, _action; | ||
return regeneratorRuntime.wrap(function value$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
array = Arrays.get(this); | ||
Index = array.index; | ||
nrOfActions = this.actions.length; | ||
if (nrOfActions) { | ||
_context.next = 33; | ||
break; | ||
} | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context.prev = 7; | ||
_iterator = array[Symbol.iterator](); | ||
case 9: | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context.next = 16; | ||
break; | ||
} | ||
row = _step.value; | ||
_context.next = 13; | ||
return row; | ||
case 13: | ||
_iteratorNormalCompletion = true; | ||
_context.next = 9; | ||
break; | ||
case 16: | ||
_context.next = 22; | ||
break; | ||
case 18: | ||
_context.prev = 18; | ||
_context.t0 = _context['catch'](7); | ||
_didIteratorError = true; | ||
_iteratorError = _context.t0; | ||
case 22: | ||
_context.prev = 22; | ||
_context.prev = 23; | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
case 25: | ||
_context.prev = 25; | ||
if (!_didIteratorError) { | ||
_context.next = 28; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 28: | ||
return _context.finish(25); | ||
case 29: | ||
return _context.finish(22); | ||
case 30: | ||
return _context.abrupt('return'); | ||
case 33: | ||
lastActionIndex = this.actions.length - 1; | ||
lastRowIndex = ArraysMetadata.get(array).length - 1; | ||
// TODO: if there is a slice before any filter | ||
// set rowIndex to slice.start - 1 | ||
rowIndex = -1; | ||
case 36: | ||
if (!(rowIndex++ < lastRowIndex)) { | ||
_context.next = 57; | ||
break; | ||
} | ||
_row = array[rowIndex]; | ||
actionIndex = -1; | ||
I = Index; | ||
case 40: | ||
if (!(actionIndex++ < lastActionIndex)) { | ||
_context.next = 55; | ||
break; | ||
} | ||
_action = this.actions[actionIndex]; | ||
if (!(_action.type == SLICE_ACTION)) { | ||
_context.next = 46; | ||
break; | ||
} | ||
if (!(++_action.counter < _action.first)) { | ||
_context.next = 45; | ||
break; | ||
} | ||
return _context.abrupt('break', 55); | ||
case 45: | ||
if (_action.counter == _action.last) lastRowIndex = rowIndex; | ||
case 46: | ||
if (!(_action.type == FILTER_ACTION)) { | ||
_context.next = 49; | ||
break; | ||
} | ||
if (_action(_row, I)) { | ||
_context.next = 49; | ||
break; | ||
} | ||
return _context.abrupt('break', 55); | ||
case 49: | ||
if (_action.type == MAP_ACTION) { | ||
_row = _action(_row, I); | ||
I = _action.I || I; | ||
} | ||
if (!(actionIndex == lastActionIndex)) { | ||
_context.next = 53; | ||
break; | ||
} | ||
_context.next = 53; | ||
return _row; | ||
case 53: | ||
_context.next = 40; | ||
break; | ||
case 55: | ||
_context.next = 36; | ||
break; | ||
case 57: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, value, this, [[7, 18, 22, 30], [23,, 25, 29]]); | ||
}) | ||
}, { | ||
key: 'toArray', | ||
value: function toArray() { | ||
return [].concat(_toConsumableArray(this)); | ||
} | ||
}, { | ||
key: 'uniq', | ||
value: function uniq() { | ||
return [].concat(_toConsumableArray(new Set(this))); | ||
} | ||
}, { | ||
key: 'filter', | ||
value: function filter() { | ||
var fn = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(typeof fn == 'function', 'Dodo#filter(fn) — fn not a function'); | ||
fn.type = FILTER_ACTION; | ||
return this[action](fn); | ||
} | ||
}, { | ||
key: 'map', | ||
value: function map() { | ||
var fn = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(typeof fn == 'function', 'Dodo#map(fn) — fn not a function'); | ||
fn.type = MAP_ACTION; | ||
return this[action](fn); | ||
} | ||
}, { | ||
key: 'col', | ||
value: function col() { | ||
var name = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(this[meta].columns.has(name), 'Dodo#col(name) — name ' + name + ' not in index'); | ||
return this.map(function (row, I) { | ||
return row[I[name]]; | ||
}); | ||
} | ||
}, { | ||
key: 'cols', | ||
value: function cols() { | ||
var _this = this; | ||
var names = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
names.forEach(function (name) { | ||
return (0, _invariant2.default)(_this[meta].columns.has(name), 'Dodo#cols(names) — name ' + name + ' not in index'); | ||
}); | ||
var fn = function fn(row, I) { | ||
return names.map(function (name) { | ||
return row[I[name]]; | ||
}); | ||
}; | ||
fn.I = {}; | ||
names.forEach(function (name, i) { | ||
return fn.I[name] = i; | ||
}); | ||
return this.map(fn); | ||
} | ||
}, { | ||
key: 'slice', | ||
value: function slice() { | ||
var start = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; | ||
var end = arguments[1]; | ||
var len = this[meta].length; | ||
if (typeof end != 'number') end = len; | ||
if (end == len && start == 0) return this; | ||
(0, _invariant2.default)(start >= 0, 'Dodo#slice(start, end) — start smaller than 0'); | ||
(0, _invariant2.default)(end >= 0, 'Dodo#slice(start, end) — end smaller than 0'); | ||
(0, _invariant2.default)(start < end, 'Dodo#slice(start, end) — end larger than start'); | ||
return this[action]({ | ||
type: SLICE_ACTION, | ||
counter: -1, | ||
first: start, | ||
last: end - 1 | ||
}); | ||
} | ||
}, { | ||
key: 'skip', | ||
value: function skip() { | ||
var amount = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(amount > 0, 'Dodo#skip(amount) — amount smaller than 0'); | ||
return this.slice(amount); | ||
} | ||
}, { | ||
key: 'take', | ||
value: function take() { | ||
var amount = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(amount > 0, 'Dodo#take(amount) — amount smaller than 0'); | ||
return this.slice(0, amount); | ||
} | ||
}, { | ||
key: 'reduce', | ||
value: function reduce() { | ||
var fn = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
var init = arguments.length <= 1 || arguments[1] === undefined ? required() : arguments[1]; | ||
(0, _invariant2.default)(typeof fn == 'function', 'Dodo#reduce(fn) — fn not a function'); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = this[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var row = _step2.value; | ||
init = fn(init, row); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
return init; | ||
} | ||
}, { | ||
key: 'reduceEach', | ||
value: function reduceEach() { | ||
var _this2 = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return Object.keys(this[index]).map(function (col) { | ||
var _col; | ||
return (_col = _this2.col(col)).reduce.apply(_col, args); | ||
}); | ||
} | ||
}, { | ||
key: 'count', | ||
value: function count() { | ||
return this[dispatch]('reduce', function (count) { | ||
return ++count; | ||
}, 0); | ||
} | ||
}, { | ||
key: 'sum', | ||
value: function sum() { | ||
return this[dispatch]('reduce', function (sum, el) { | ||
return sum + el; | ||
}, 0); | ||
} | ||
}, { | ||
key: 'min', | ||
value: function min() { | ||
return this[dispatch]('reduce', function (min, el) { | ||
return min < el ? min : el; | ||
}, Infinity); | ||
} | ||
}, { | ||
key: 'max', | ||
value: function max() { | ||
return this[dispatch]('reduce', function (max, el) { | ||
return max > el ? max : el; | ||
}, -Infinity); | ||
} | ||
}, { | ||
key: 'mean', | ||
value: function mean() { | ||
var _this3 = this; | ||
if (this[index] == Arrays.get(this).index) return this.sum() / this.count();else { | ||
var _ret = function () { | ||
var counts = _this3.count(); | ||
return { | ||
v: _this3.sum().map(function (sum, i) { | ||
return sum / counts[i]; | ||
}) | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} | ||
} | ||
}, { | ||
key: 'groupBy', | ||
value: function groupBy() { | ||
var _this4 = this; | ||
var name = arguments.length <= 0 || arguments[0] === undefined ? required() : arguments[0]; | ||
(0, _invariant2.default)(this[meta].columns.has(name), 'Dodo#group(name) — name ' + name + ' not in index'); | ||
var map = []; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
var _loop = function _loop() { | ||
var val = _step3.value; | ||
map.push([val, _this4.filter(function (d, I) { | ||
return d[I[name]] == val; | ||
})]); | ||
}; | ||
for (var _iterator3 = this.col(name).uniq()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
_loop(); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return Flock(map); | ||
} | ||
}, { | ||
key: meta, | ||
get: function get() { | ||
return ArraysMetadata.get(Arrays.get(this)); | ||
} | ||
}, { | ||
key: index, | ||
get: function get() { | ||
var lastMapWithIndex = this.actions.filter(function (act) { | ||
return act.type == MAP_ACTION && act.I; | ||
}).slice(-1); | ||
if (lastMapWithIndex.length) return lastMapWithIndex[0].I;else return Arrays.get(this).index; | ||
} | ||
}]); | ||
return Dodo; | ||
}(); | ||
exports.default = Dodo; | ||
function Flock(map, method, args) { | ||
map = new Map(map); | ||
// if called with method arg call that method on all Dodos | ||
if (method) { | ||
var _iteratorNormalCompletion4 = true; | ||
var _didIteratorError4 = false; | ||
var _iteratorError4 = undefined; | ||
try { | ||
for (var _iterator4 = map.entries()[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var _step4$value = _slicedToArray(_step4.value, 2); | ||
var key = _step4$value[0]; | ||
var dodo = _step4$value[1]; | ||
map.set(key, dodo[method].apply(dodo, _toConsumableArray(args))); | ||
} | ||
} catch (err) { | ||
_didIteratorError4 = true; | ||
_iteratorError4 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError4) { | ||
throw _iteratorError4; | ||
} | ||
} | ||
} | ||
} // if the values are Dodos add the Dodo methods to the returned Map | ||
// otherwise just return a regular Map | ||
if (map.values().next().value instanceof Dodo) { | ||
var _iteratorNormalCompletion5 = true; | ||
var _didIteratorError5 = false; | ||
var _iteratorError5 = undefined; | ||
try { | ||
var _loop2 = function _loop2() { | ||
var method = _step5.value; | ||
if (method != 'constructor') map[method] = function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return Flock(map, method, args); | ||
}; | ||
}; | ||
for (var _iterator5 = Object.getOwnPropertyNames(Dodo.prototype)[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
_loop2(); | ||
} | ||
} catch (err) { | ||
_didIteratorError5 = true; | ||
_iteratorError5 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion5 && _iterator5.return) { | ||
_iterator5.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError5) { | ||
throw _iteratorError5; | ||
} | ||
} | ||
} | ||
}return map; | ||
} | ||
/***/ }, | ||
@@ -61,0 +608,0 @@ /* 1 */ |
{ | ||
"name": "dodos", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Pandas-inspired iterator-based data wrangling library for js", | ||
@@ -8,5 +8,6 @@ "main": "dist/dodos.js", | ||
"build": "webpack", | ||
"prepublish": "npm run test && npm run build", | ||
"test": "tap test/*.js", | ||
"tdd": "nodemon -q -x 'tap test/*.js -b'" | ||
"prepublish": "npm test && npm run build", | ||
"test": "babel-node test/test.js | tap-mocha-reporter classic", | ||
"coverage": "babel-node node_modules/isparta/bin/isparta cover --report html test/test.js && open coverage/index.html", | ||
"tdd": "nodemon -q -x 'npm test'" | ||
}, | ||
@@ -24,3 +25,4 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "^6.6.4", | ||
"babel-cli": "^6.6.5", | ||
"babel-core": "^6.6.5", | ||
"babel-eslint": "^5.0.0", | ||
@@ -31,18 +33,18 @@ "babel-loader": "^6.2.4", | ||
"babel-require": "^1.0.1", | ||
"eslint": "^2.2.0", | ||
"eslint": "~2.3.0", | ||
"lodash": "^4.6.1", | ||
"nodemon": "^1.9.1", | ||
"npm-check": "^4.1.4", | ||
"tap": "^5.7.0", | ||
"tap-spec": "^4.1.1", | ||
"webpack": "^1.12.14" | ||
}, | ||
"dependencies": { | ||
"invariant": "^2.2.0" | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015" | ||
], | ||
"compact": true, | ||
"comments": false | ||
}, | ||
"dependencies": { | ||
"invariant": "^2.2.0" | ||
] | ||
} | ||
} |
@@ -8,4 +8,5 @@ import invariant from 'invariant' | ||
const action = Symbol('action') | ||
const index = Symbol('index') | ||
const meta = Symbol('meta') | ||
const reduceD = Symbol('reduceDispatch') | ||
const dispatch = Symbol('dispatch') | ||
const noActions = [] | ||
@@ -33,8 +34,24 @@ | ||
toArray() { return [...this] } | ||
get [index]() { | ||
const lastMapWithIndex = this.actions | ||
.filter(act => act.type == MAP_ACTION && act.I) | ||
.slice(-1) | ||
uniq() { | ||
return [...new Set(this)] | ||
if (lastMapWithIndex.length) | ||
return lastMapWithIndex[0].I | ||
else | ||
return Arrays.get(this).index | ||
} | ||
[dispatch](method, fn, init) { | ||
if (this[index] == Arrays.get(this).index) | ||
return this[method](fn, init) | ||
else | ||
return this[method + 'Each'](fn, init) | ||
} | ||
[action](action) { | ||
return new Dodo(Arrays.get(this), [...this.actions, action]) | ||
} | ||
*[Symbol.iterator]() { | ||
@@ -85,4 +102,6 @@ const array = Arrays.get(this) | ||
[action](action) { | ||
return new Dodo(Arrays.get(this), [...this.actions, action]) | ||
toArray() { return [...this] } | ||
uniq() { | ||
return [...new Set(this)] | ||
} | ||
@@ -146,42 +165,48 @@ | ||
groupBy(name=required()) { | ||
invariant(this[meta].columns.has(name), | ||
`Dodo#group(name) — name ${name} not in index`) | ||
let map = [] | ||
for (const val of this.col(name).uniq()) | ||
map.push([val, this.filter((d, I) => d[I[name]] == val)]) | ||
return Flock(map) | ||
} | ||
reduce(fn=required(), init=required()) { | ||
invariant(typeof fn == 'function', `Dodo#reduce(fn) — fn not a function`) | ||
for (const row of this) | ||
for (const row of this) { | ||
init = fn(init, row) | ||
} | ||
return init | ||
} | ||
reduceEach(fn=required(), init=required()) { | ||
invariant(typeof fn =='function', `Dodo#reduceEach(fn) — fn not a function`) | ||
for (const row of this) | ||
init = row.map((col, i) => fn(init[i] || init, col)) | ||
return init | ||
reduceEach(...args) { | ||
return Object.keys(this[index]) | ||
.map(col => this.col(col).reduce(...args)) | ||
} | ||
[reduceD](fn, init) { | ||
const oneColumn = !!this.actions | ||
.filter(act => act.type == MAP_ACTION && !act.I).length | ||
count() { | ||
return this[dispatch]('reduce', count => ++count, 0) | ||
} | ||
if (oneColumn) return this.reduce(fn, init) | ||
else return this.reduceEach(fn, init) | ||
sum() { | ||
return this[dispatch]('reduce', (sum, el) => sum + el, 0) | ||
} | ||
count() { return this[reduceD](count => ++count, 0) } | ||
min() { | ||
return this[dispatch]('reduce', (min, el) => min < el ? min : el, Infinity) | ||
} | ||
sum() { return this[reduceD]((sum, el) => sum + el, 0) } | ||
max() { | ||
return this[dispatch]('reduce', (max, el) => max > el ? max : el, -Infinity) | ||
} | ||
min() { return this[reduceD]((min, el) => min < el ? min : el, Infinity) } | ||
mean() { | ||
if (this[index] == Arrays.get(this).index) | ||
return this.sum() / this.count() | ||
else { | ||
const counts = this.count() | ||
return this.sum().map((sum, i) => sum / counts[i]) | ||
} | ||
} | ||
max() { return this[reduceD]((max, el) => max > el ? max : el, -Infinity) } | ||
mean() { return this.sum() / this.count() } | ||
groupBy(name=required()) { | ||
invariant(this[meta].columns.has(name), | ||
`Dodo#group(name) — name ${name} not in index`) | ||
let map = [] | ||
for (const val of this.col(name).uniq()) | ||
map.push([val, this.filter((d, I) => d[I[name]] == val)]) | ||
return Flock(map) | ||
} | ||
} | ||
@@ -201,5 +226,6 @@ | ||
for (const method of Object.getOwnPropertyNames(Dodo.prototype)) | ||
map[method] = (...args) => Flock(map, method, args) | ||
if (method != 'constructor') | ||
map[method] = (...args) => Flock(map, method, args) | ||
return map | ||
} |
@@ -1,13 +0,9 @@ | ||
'use strict' | ||
import tap from 'tap' | ||
import _ from 'lodash' | ||
require('babel-register') | ||
require('babel-polyfill') | ||
const Dodo = require('../src/dodo').default | ||
const table = require('./fixture/table').default | ||
import Dodo from '../src/dodo' | ||
import table from './fixture/table' | ||
const array = [...table] | ||
const Index = table.index | ||
const tap = require('tap') | ||
tap.test('no actions', t => { | ||
@@ -335,7 +331,31 @@ const dodo = new Dodo(table) | ||
tap.test('reduce shorthand: count') | ||
tap.test('reduce shorthand: sum') | ||
tap.test('reduce shorthand: min') | ||
tap.test('reduce shorthand: max') | ||
tap.test('reduce shorthand: mean') | ||
tap.test('reduce shorthands', t => { | ||
const dodo = new Dodo(table) | ||
const shorthands = [ | ||
['count', arr => arr.length], | ||
['sum', arr => arr.reduce((a, b) => a + b, 0)], | ||
['min', arr => Math.min(...arr)], | ||
['max', arr => Math.max(...arr)], | ||
['mean', arr => _.mean(arr)], | ||
] | ||
const col = 'Date' | ||
const cols = ['Date', 'Age'] | ||
for (const s of shorthands) | ||
t.test(s[0], t => { | ||
t.same( | ||
dodo.col(col)[s[0]](), | ||
s[1](array.map(row => row[Index.Date])), | ||
`${s[0]} over 1 column` | ||
) | ||
t.same( | ||
dodo.cols(cols)[s[0]](), | ||
cols | ||
.map(col => array.map(row => row[Index[col]])) | ||
.map(col => s[1](col)), | ||
`${s[0]} over several columns` | ||
) | ||
t.end() | ||
}) | ||
t.end() | ||
}) | ||
@@ -345,3 +365,5 @@ tap.test('groupBy', t => { | ||
const grouped = dodo.groupBy('Age') | ||
dodo.col('Age').uniq().forEach(uniq => { | ||
const ageUniques = dodo.col('Age').uniq() | ||
ageUniques.forEach(uniq => { | ||
t.true(grouped.has(uniq)) | ||
@@ -352,11 +374,46 @@ t.true(grouped.get(uniq) instanceof Dodo) | ||
t.test('Dodo prototype methods', t => { | ||
for (const method of Object.getOwnPropertyNames(Dodo.prototype)) | ||
t.true(method in grouped && typeof grouped[method] == 'function') | ||
for (const method of Object.getOwnPropertyNames(grouped)) {} | ||
t.test('Dodo prototype methods present', t => { | ||
const methods = new Set(Object.getOwnPropertyNames(grouped)) | ||
for (const method of Object.getOwnPropertyNames(Dodo.prototype)) { | ||
if (method == 'constructor') continue | ||
t.true(methods.has(method), method + ' not present') | ||
t.true(typeof grouped[method] == 'function', method + ' not function') | ||
} | ||
t.end() | ||
}) | ||
t.test('groupBy().map()', t => { | ||
const mappedGrouped = grouped.col('Date') | ||
ageUniques.forEach(uniq => { | ||
t.true(mappedGrouped.has(uniq), `uniq ${uniq} is missing`) | ||
t.true(mappedGrouped.get(uniq) instanceof Dodo, | ||
`value of uniq ${uniq} is not a dodo`) | ||
t.true(mappedGrouped.get(uniq).actions.length | ||
== grouped.get(uniq).actions.length + 1, | ||
`dodo for uniq ${uniq} doesn't have the right number of actions`) | ||
const expected = dodo.filter((row, I) => row[I.Age] == uniq).col('Date') | ||
t.same( | ||
mappedGrouped.get(uniq).toArray(), | ||
expected.toArray() | ||
) | ||
t.same( | ||
mappedGrouped.toArray().get(uniq), | ||
expected.toArray() | ||
) | ||
}) | ||
t.end() | ||
}) | ||
t.test('Dodo prototype methods absent after toArray', t => { | ||
const groupedToArray = grouped.toArray() | ||
const methods = new Set(Object.getOwnPropertyNames(groupedToArray)) | ||
for (const method of Object.getOwnPropertyNames(Dodo.prototype)) { | ||
if (method == 'constructor') continue | ||
t.false(methods.has(method), method + ' present') | ||
} | ||
t.end() | ||
}) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
164983
21
1734
0
14