Comparing version 1.3.0 to 1.4.0
@@ -57,10 +57,61 @@ (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 _util=__webpack_require__(1);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 Arrays=new WeakMap();var Dodo=function(){function Dodo(array,filters,maps){_classCallCheck(this,Dodo);Arrays.set(this,array);this.filters=filters||[];this.maps=maps||[];}_createClass(Dodo,[{key:'toArray',value:function toArray(){return [].concat(_toConsumableArray(this));}},{key:Symbol.iterator,value:regeneratorRuntime.mark(function value(){var filters,maps,desc,_iteratorNormalCompletion,_didIteratorError,_iteratorError,_loop,_iterator,_step,proxy,row,_iteratorNormalCompletion2,_didIteratorError2,_iteratorError2,_iterator2,_step2,_iteratorNormalCompletion3,_didIteratorError3,_iteratorError3,_iterator3,_step3;return regeneratorRuntime.wrap(function value$(_context){while(1){switch(_context.prev=_context.next){case 0:filters=(0,_util.and)(this.filters);maps=this.maps.length?(0,_util.flow)(this.maps):false;desc={};_iteratorNormalCompletion=true;_didIteratorError=false;_iteratorError=undefined;_context.prev=6;_loop=function _loop(){var _step$value=_slicedToArray(_step.value,2);var colname=_step$value[0];var index=_step$value[1];desc[colname]={get:function get(){return row[index];}};};for(_iterator=Object.entries(Arrays.get(this).index)[Symbol.iterator]();!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){_loop();}_context.next=15;break;case 11:_context.prev=11;_context.t0=_context['catch'](6);_didIteratorError=true;_iteratorError=_context.t0;case 15:_context.prev=15;_context.prev=16;if(!_iteratorNormalCompletion&&_iterator.return){_iterator.return();}case 18:_context.prev=18;if(!_didIteratorError){_context.next=21;break;}throw _iteratorError;case 21:return _context.finish(18);case 22:return _context.finish(15);case 23:proxy=Object.defineProperties({},desc);row=undefined;if(!maps){_context.next=55;break;}_iteratorNormalCompletion2=true;_didIteratorError2=false;_iteratorError2=undefined;_context.prev=29;_iterator2=Arrays.get(this)[Symbol.iterator]();case 31:if(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done){_context.next=39;break;}row=_step2.value;if(!filters(proxy)){_context.next=36;break;}_context.next=36;return maps(proxy);case 36:_iteratorNormalCompletion2=true;_context.next=31;break;case 39:_context.next=45;break;case 41:_context.prev=41;_context.t1=_context['catch'](29);_didIteratorError2=true;_iteratorError2=_context.t1;case 45:_context.prev=45;_context.prev=46;if(!_iteratorNormalCompletion2&&_iterator2.return){_iterator2.return();}case 48:_context.prev=48;if(!_didIteratorError2){_context.next=51;break;}throw _iteratorError2;case 51:return _context.finish(48);case 52:return _context.finish(45);case 53:_context.next=82;break;case 55:_iteratorNormalCompletion3=true;_didIteratorError3=false;_iteratorError3=undefined;_context.prev=58;_iterator3=Arrays.get(this)[Symbol.iterator]();case 60:if(_iteratorNormalCompletion3=(_step3=_iterator3.next()).done){_context.next=68;break;}row=_step3.value;if(!filters(proxy)){_context.next=65;break;}_context.next=65;return row;case 65:_iteratorNormalCompletion3=true;_context.next=60;break;case 68:_context.next=74;break;case 70:_context.prev=70;_context.t2=_context['catch'](58);_didIteratorError3=true;_iteratorError3=_context.t2;case 74:_context.prev=74;_context.prev=75;if(!_iteratorNormalCompletion3&&_iterator3.return){_iterator3.return();}case 77:_context.prev=77;if(!_didIteratorError3){_context.next=80;break;}throw _iteratorError3;case 80:return _context.finish(77);case 81:return _context.finish(74);case 82:case 'end':return _context.stop();}}},value,this,[[6,11,15,23],[16,,18,22],[29,41,45,53],[46,,48,52],[58,70,74,82],[75,,77,81]]);})},{key:'filter',value:function filter(fn){return new Dodo(Arrays.get(this),[].concat(_toConsumableArray(this.filters),[fn]),this.maps);}},{key:'map',value:function map(fn){return new Dodo(Arrays.get(this),this.filters,[].concat(_toConsumableArray(this.maps),[fn]));}},{key:'uniq',value:function uniq(fn){return new Set(this.map(fn));}},{key:'count',value:function count(){var i=0;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 _row=_step4.value;++i;}}catch(err){_didIteratorError4=true;_iteratorError4=err;}finally {try{if(!_iteratorNormalCompletion4&&_iterator4.return){_iterator4.return();}}finally {if(_didIteratorError4){throw _iteratorError4;}}}return i;}},{key:'group',value:function group(colname){var _this=this;var map=[];var _iteratorNormalCompletion5=true;var _didIteratorError5=false;var _iteratorError5=undefined;try{var _loop2=function _loop2(){var val=_step5.value;map.push([val,_this.filter(function(d){return d[colname]==val;})]);};for(var _iterator5=this.uniq(function(d){return d[colname];})[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 Flock(map);}}]);return Dodo;}();exports.default=Dodo;function Flock(map,prop,args){map=new Map(map);if(prop){var _iteratorNormalCompletion6=true;var _didIteratorError6=false;var _iteratorError6=undefined;try{for(var _iterator6=map.entries()[Symbol.iterator](),_step6;!(_iteratorNormalCompletion6=(_step6=_iterator6.next()).done);_iteratorNormalCompletion6=true){var _step6$value=_slicedToArray(_step6.value,2);var key=_step6$value[0];var perspective=_step6$value[1];map.set(key,perspective[prop].apply(perspective,_toConsumableArray(args)));}}catch(err){_didIteratorError6=true;_iteratorError6=err;}finally {try{if(!_iteratorNormalCompletion6&&_iterator6.return){_iterator6.return();}}finally {if(_didIteratorError6){throw _iteratorError6;}}}}if(map.values().next().value instanceof Dodo){var _iteratorNormalCompletion7=true;var _didIteratorError7=false;var _iteratorError7=undefined;try{var _loop3=function _loop3(){var method=_step7.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 _iterator7=Object.getOwnPropertyNames(Dodo.prototype)[Symbol.iterator](),_step7;!(_iteratorNormalCompletion7=(_step7=_iterator7.next()).done);_iteratorNormalCompletion7=true){_loop3();}}catch(err){_didIteratorError7=true;_iteratorError7=err;}finally {try{if(!_iteratorNormalCompletion7&&_iterator7.return){_iterator7.return();}}finally {if(_didIteratorError7){throw _iteratorError7;}}}}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 _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;} | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports) { | ||
/***/ function(module, exports, __webpack_require__) { | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});var and=exports.and=function and(filters){return function(row){var bool=true;var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=filters[Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var mask=_step.value;bool=bool&&mask(row);if(!bool)break;}}catch(err){_didIteratorError=true;_iteratorError=err;}finally {try{if(!_iteratorNormalCompletion&&_iterator.return){_iterator.return();}}finally {if(_didIteratorError){throw _iteratorError;}}}return bool;};};var flow=exports.flow=function flow(fns){return function(arg){var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{for(var _iterator2=fns[Symbol.iterator](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){var fn=_step2.value;arg=fn(arg);}}catch(err){_didIteratorError2=true;_iteratorError2=err;}finally {try{if(!_iteratorNormalCompletion2&&_iterator2.return){_iterator2.return();}}finally {if(_didIteratorError2){throw _iteratorError2;}}}return arg;};}; | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
/** | ||
* Use invariant() to assert state which your program assumes to be true. | ||
* | ||
* Provide sprintf-style format (only %s is supported) and arguments | ||
* to provide information about what broke and what you were | ||
* expecting. | ||
* | ||
* The invariant message will be stripped in production, but the invariant | ||
* will remain to ensure logic does not differ in production. | ||
*/ | ||
var invariant = function(condition, format, a, b, c, d, e, f) { | ||
if (false) { | ||
if (format === undefined) { | ||
throw new Error('invariant requires an error message argument'); | ||
} | ||
} | ||
if (!condition) { | ||
var error; | ||
if (format === undefined) { | ||
error = new Error( | ||
'Minified exception occurred; use the non-minified dev environment ' + | ||
'for the full error message and additional helpful warnings.' | ||
); | ||
} else { | ||
var args = [a, b, c, d, e, f]; | ||
var argIndex = 0; | ||
error = new Error( | ||
format.replace(/%s/g, function() { return args[argIndex++]; }) | ||
); | ||
error.name = 'Invariant Violation'; | ||
} | ||
error.framesToPop = 1; // we don't care about invariant's own frame | ||
throw error; | ||
} | ||
}; | ||
module.exports = invariant; | ||
/***/ } | ||
@@ -67,0 +118,0 @@ /******/ ]) |
{ | ||
"name": "dodos", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Pandas-inspired iterator-based data wrangling library for js", | ||
@@ -10,3 +10,3 @@ "main": "dist/dodos.js", | ||
"test": "tap test/*.js", | ||
"tdd": "nodemon -q -x 'npm run test'" | ||
"tdd": "nodemon -q -x 'tap test/*.js -b'" | ||
}, | ||
@@ -24,11 +24,11 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "^6.5.1", | ||
"babel-core": "^6.6.4", | ||
"babel-eslint": "^5.0.0", | ||
"babel-loader": "^6.2.4", | ||
"babel-polyfill": "^6.6.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-polyfill": "^6.6.1", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-require": "^1.0.1", | ||
"eslint": "^2.2.0", | ||
"nodemon": "^1.9.1", | ||
"regenerator": "^0.8.42", | ||
"npm-check": "^4.1.4", | ||
"tap": "^5.7.0", | ||
@@ -43,3 +43,6 @@ "webpack": "^1.12.14" | ||
"comments": false | ||
}, | ||
"dependencies": { | ||
"invariant": "^2.2.0" | ||
} | ||
} |
@@ -10,1 +10,3 @@ # Dodos | ||
``` | ||
NOTE: This is very much a work in progress for now |
197
src/dodo.js
@@ -1,67 +0,192 @@ | ||
import {flow, and} from './util' | ||
import invariant from 'invariant' | ||
const MAP_ACTION = 'MAP_ACTION' | ||
const FILTER_ACTION = 'FILTER_ACTION' | ||
const SLICE_ACTION = 'SLICE_ACTION' | ||
const action = Symbol('action') | ||
const meta = Symbol('meta') | ||
const reduceD = Symbol('reduceDispatch') | ||
const noActions = [] | ||
const Arrays = new WeakMap() | ||
const ArraysMetadata = new WeakMap() | ||
const required = () => { | ||
throw new Error('Dodo - missing required argument') | ||
} | ||
export default class Dodo { | ||
constructor(array, filters, maps) { | ||
constructor(array=required(), actions=noActions) { | ||
Arrays.set(this, array) | ||
this.filters = filters || [] | ||
this.maps = maps || [] | ||
if (!ArraysMetadata.has(array)) | ||
ArraysMetadata.set(array, { | ||
length: array.length, | ||
columns: new Set(Object.keys(array.index)), | ||
}) | ||
this.actions = actions | ||
} | ||
get [meta]() { return ArraysMetadata.get(Arrays.get(this)) } | ||
toArray() { return [...this] } | ||
uniq() { | ||
return [...new Set(this)] | ||
} | ||
*[Symbol.iterator]() { | ||
const filters = and(this.filters) | ||
const maps = this.maps.length ? flow(this.maps) : false | ||
const array = Arrays.get(this) | ||
const Index = array.index | ||
const nrOfActions = this.actions.length | ||
let desc = {} | ||
for (const [colname, index] of Object.entries(Arrays.get(this).index)) | ||
desc[colname] = {get: () => row[index]} | ||
const proxy = Object.defineProperties({}, desc) | ||
if (!nrOfActions) { | ||
let row | ||
if (maps) { | ||
for (row of Arrays.get(this)) | ||
if (filters(proxy)) | ||
yield maps(proxy) | ||
for (const row of array) | ||
yield row | ||
return | ||
} else { | ||
for (row of Arrays.get(this)) | ||
if (filters(proxy)) | ||
yield row | ||
const lastActionIndex = this.actions.length - 1 | ||
let lastRowIndex = ArraysMetadata.get(array).length - 1 | ||
// TODO: if there is a slice before any filter | ||
// set rowIndex to slice.start - 1 | ||
let rowIndex = -1 | ||
while (rowIndex++ < lastRowIndex) { | ||
let row = array[rowIndex] | ||
let actionIndex = -1 | ||
let I = Index | ||
while (actionIndex++ < lastActionIndex) { | ||
const action = this.actions[actionIndex] | ||
if (action.type == SLICE_ACTION) { | ||
if (++action.counter < action.first) | ||
break | ||
if (action.counter == action.last) | ||
lastRowIndex = rowIndex | ||
} if (action.type == FILTER_ACTION) { | ||
if (!action(row, I)) | ||
break | ||
} if (action.type == MAP_ACTION) { | ||
row = action(row, I) | ||
I = action.I || I | ||
} | ||
if (actionIndex == lastActionIndex) | ||
yield row | ||
} | ||
} | ||
} | ||
} | ||
filter(fn) { | ||
return new Dodo(Arrays.get(this), [...this.filters, fn], this.maps) | ||
[action](action) { | ||
return new Dodo(Arrays.get(this), [...this.actions, action]) | ||
} | ||
map(fn) { | ||
return new Dodo(Arrays.get(this), this.filters, [...this.maps, fn]) | ||
filter(fn=required()) { | ||
invariant(typeof fn == 'function', `Dodo#filter(fn) — fn not a function`) | ||
fn.type = FILTER_ACTION | ||
return this[action](fn) | ||
} | ||
uniq(fn) { return new Set(this.map(fn)) } | ||
map(fn=required()) { | ||
invariant(typeof fn == 'function', `Dodo#map(fn) — fn not a function`) | ||
fn.type = MAP_ACTION | ||
return this[action](fn) | ||
} | ||
count() { | ||
let i = 0 | ||
for (const row of this) // eslint-disable-line no-unused-vars | ||
++i | ||
return i | ||
col(name=required()) { | ||
invariant(this[meta].columns.has(name), | ||
`Dodo#col(name) — name ${name} not in index`) | ||
return this.map((row, I) => row[I[name]]) | ||
} | ||
group(colname) { | ||
cols(names=required()) { | ||
names.forEach(name => invariant( | ||
this[meta].columns.has(name), | ||
`Dodo#cols(names) — name ${name} not in index` | ||
)) | ||
const fn = (row, I) => names.map(name => row[I[name]]) | ||
fn.I = {} | ||
names.forEach((name, i) => fn.I[name] = i) | ||
return this.map(fn) | ||
} | ||
slice(start=0, end) { | ||
const len = this[meta].length | ||
if (typeof end != 'number') | ||
end = len | ||
if (end == len && start == 0) | ||
return this | ||
invariant(start >= 0, `Dodo#slice(start, end) — start smaller than 0`) | ||
invariant(end >= 0, `Dodo#slice(start, end) — end smaller than 0`) | ||
invariant(start < end, `Dodo#slice(start, end) — end larger than start`) | ||
return this[action]({ | ||
type: SLICE_ACTION, | ||
counter: -1, | ||
first: start, | ||
last: end - 1, | ||
}) | ||
} | ||
skip(amount=required()) { | ||
invariant(amount > 0, `Dodo#skip(amount) — amount smaller than 0`) | ||
return this.slice(amount) | ||
} | ||
take(amount=required()) { | ||
invariant(amount > 0, `Dodo#take(amount) — amount smaller than 0`) | ||
return this.slice(0, amount) | ||
} | ||
groupBy(name=required()) { | ||
invariant(this[meta].columns.has(name), | ||
`Dodo#group(name) — name ${name} not in index`) | ||
let map = [] | ||
for (const val of this.uniq(d => d[colname])) | ||
map.push([val, this.filter(d => d[colname] == val)]) | ||
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) | ||
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 | ||
} | ||
[reduceD](fn, init) { | ||
const oneColumn = !!this.actions | ||
.filter(act => act.type == MAP_ACTION && !act.I).length | ||
if (oneColumn) return this.reduce(fn, init) | ||
else return this.reduceEach(fn, init) | ||
} | ||
count() { return this[reduceD](count => ++count, 0) } | ||
sum() { return this[reduceD]((sum, el) => sum + el, 0) } | ||
min() { return this[reduceD]((min, el) => min < el ? min : el, Infinity) } | ||
max() { return this[reduceD]((max, el) => max > el ? max : el, -Infinity) } | ||
mean() { return this.sum() / this.count() } | ||
} | ||
function Flock(map, prop, args) { | ||
function Flock(map, method, args) { | ||
map = new Map(map) | ||
if (prop) { | ||
for (const [key, perspective] of map.entries()) | ||
map.set(key, perspective[prop](...args)) | ||
} | ||
// if called with method arg call that method on all Dodos | ||
if (method) | ||
for (const [key, dodo] of map.entries()) | ||
map.set(key, dodo[method](...args)) | ||
@@ -68,0 +193,0 @@ // if the values are Dodos add the Dodo methods to the returned Map |
@@ -7,2 +7,12 @@ const table = [ | ||
[6, 7, 8, 9, 20], | ||
[1, 2, 3, 4, 65], | ||
[45, 3, 4, 1, 2], | ||
[2, 3, 4, 2, 3], | ||
[1, 2, 3, 9, 4], | ||
[6, 7, 8, 9, 20], | ||
[100, 2, 3, 4, 5], | ||
[2, 3, 7, 1, 2], | ||
[2, 3, 4, 2, 3], | ||
[1, 2, 8, 3, 57], | ||
[5, 7, 8, 9, 20], | ||
] | ||
@@ -9,0 +19,0 @@ |
348
test/test.js
@@ -8,9 +8,353 @@ 'use strict' | ||
const table = require('./fixture/table').default | ||
const array = [...table] | ||
const Index = table.index | ||
const tap = require('tap') | ||
tap.test('With no filter returns original array', t => { | ||
tap.test('no actions', t => { | ||
const dodo = new Dodo(table) | ||
t.same([...dodo], [...table]) | ||
t.equal(dodo.actions.length, 0) | ||
t.same([...dodo], array) | ||
t.end() | ||
}) | ||
tap.test('throws without an array', t => { | ||
t.throws(() => new Dodo()) | ||
t.end() | ||
}) | ||
tap.test('slice', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
[...dodo.slice(0, 2)], | ||
array.slice(0, 2) | ||
) | ||
t.same( | ||
[...dodo.slice(1, 2)], | ||
array.slice(1, 2) | ||
) | ||
t.same( | ||
[...dodo.slice(0, array.length)], | ||
array | ||
) | ||
t.same( | ||
[...dodo.slice(0)], | ||
array | ||
) | ||
t.same( | ||
[...dodo.slice()], | ||
array | ||
) | ||
t.throws(dodo.slice.bind(dodo, 2, 2)) | ||
t.end() | ||
}) | ||
tap.test('slice shorthand: take', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.take(10).toArray(), | ||
array.slice(0, 10) | ||
) | ||
t.end() | ||
}) | ||
tap.test('slice shorthand: skip', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.skip(3).toArray(), | ||
array.slice(3) | ||
) | ||
t.end() | ||
}) | ||
tap.test('slice + slice', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
[...dodo.slice(0, 2).skip(2)], | ||
array.slice(0, 2).slice(2) | ||
) | ||
t.same( | ||
[...dodo.slice(1, 2).slice(0)], | ||
array.slice(1, 2) | ||
) | ||
t.same( | ||
[...dodo.slice(0, 2).slice(1, 2)], | ||
array.slice(0, 2).slice(1, 2) | ||
) | ||
t.same( | ||
[...dodo.slice(0, 2).take(3)], | ||
array.slice(0, 2).slice(0, 3) | ||
) | ||
t.same( | ||
[...dodo.slice(0, 5).slice(0, 2)], | ||
array.slice(0, 2) | ||
) | ||
t.same( | ||
[...dodo.slice(1).slice(0, 2)], | ||
array.slice(1).slice(0, 2) | ||
) | ||
t.throws(dodo.slice(2).slice.bind(dodo, 2, 2)) | ||
t.end() | ||
}) | ||
tap.test('filter', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.filter((row, I) => row[I.Date] == 4).toArray(), | ||
array.filter(row => row[Index.Date] == 4) | ||
) | ||
t.same( | ||
dodo.filter((row, I) => row[I.Date] <= 4).toArray(), | ||
array.filter(row => row[Index.Date] <= 4) | ||
) | ||
t.same( | ||
dodo.filter((row, I) => row[I.Age] <= 4 && row[I.Weight] == 2).toArray(), | ||
array.filter(row => row[Index.Age] <= 4 && row[Index.Weight] == 2) | ||
) | ||
t.throws(() => dodo.filter()) | ||
t.throws(() => dodo.filter('not a function')) | ||
t.end() | ||
}) | ||
tap.test('filter + filter', t => { | ||
const dodo = new Dodo(table) | ||
const expected = array | ||
.filter(row => row[Index.Date] == 4) | ||
.filter(r => r[Index.Weight] == 2) | ||
t.same( | ||
dodo | ||
.filter((r,I) => r[I.Date] == 4) | ||
.filter((r,I) => r[I.Weight] == 2) | ||
.toArray(), | ||
expected | ||
) | ||
t.same( | ||
dodo.filter((r, I) => r[I.Date] == 4 && r[I.Weight] == 2).toArray(), | ||
expected | ||
) | ||
t.end() | ||
}) | ||
tap.test('map', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.map((row, I) => row[I.Date]).toArray(), | ||
array.map(row => row[Index.Date]) | ||
) | ||
t.same( | ||
dodo.map((row, I) => row[I.Date] * 2).toArray(), | ||
array.map(row => row[Index.Date] * 2) | ||
) | ||
t.same( | ||
dodo.map((row, I) => row[I.Date] + row[I.Age]).toArray(), | ||
array.map(row => row[Index.Date] + row[Index.Age]) | ||
) | ||
t.throws(() => dodo.map()) | ||
t.throws(() => dodo.map('not a function')) | ||
t.end() | ||
}) | ||
tap.test('map shorthand: col', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.col('Date').toArray(), | ||
array.map(row => row[Index.Date]) | ||
) | ||
t.throws(() => dodo.col('some column not in the index')) | ||
t.throws(() => dodo.col()) | ||
t.end() | ||
}) | ||
tap.test('map shorthand: cols', t => { | ||
const dodo = new Dodo(table) | ||
const cols = ['Date', 'Age', 'Height'] | ||
t.same( | ||
dodo.cols(cols).toArray(), | ||
array.map(row => cols.map(col => row[Index[col]])) | ||
) | ||
t.throws(() => dodo.cols([...cols, 'Column that does not exist'])) | ||
t.end() | ||
}) | ||
tap.test('multiple maps', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.col('Date').map(d => d * 2).toArray(), | ||
array.map(row => row[Index.Date]).map(d => d * 2) | ||
) | ||
const cols = ['Age', 'Date', 'Height', 'Weight'] | ||
t.same( | ||
[...dodo.cols(cols).col('Date')], | ||
array.map(row => row[Index.Date]) | ||
) | ||
const colsAfter = ['Age', 'Date'] | ||
t.same( | ||
[...dodo.cols(cols).cols(colsAfter)], | ||
array.map(row => colsAfter.map(col => row[Index[col]])) | ||
) | ||
t.end() | ||
}) | ||
tap.test('map + filter', t => { | ||
const dodo = new Dodo(table) | ||
const expected = array | ||
.filter(row => row[Index.Date] > 3) | ||
.map(row => row[Index.Date]) | ||
t.same( | ||
dodo.filter((row, I) => row[I.Date] > 3).col('Date').toArray(), | ||
expected | ||
) | ||
t.same( | ||
dodo.col('Date').filter(a => a > 3).toArray(), | ||
expected | ||
) | ||
t.same( | ||
[...dodo | ||
.col('Date') | ||
.filter(a => a > 3) | ||
.map(a => a * 2) | ||
.filter(a => a > 8)], | ||
array | ||
.map(row => row[Index.Date]) | ||
.filter(a => a > 3) | ||
.map(a => a * 2) | ||
.filter(a => a > 8) | ||
) | ||
const cols = ['Age', 'Date'] | ||
t.same( | ||
[...dodo | ||
.cols(cols) | ||
.filter((row, I) => row[I.Date] == 7)], | ||
array | ||
.map(row => cols.map(col => row[Index[col]])) | ||
.filter(row => row[1] == 7) | ||
) | ||
t.same( | ||
[...dodo | ||
.filter((row, I) => row[I.Height] == 7) | ||
.cols(cols) | ||
.filter((row, I) => row[I.Age] == 5) | ||
.col('Date')], | ||
array | ||
.filter(row => row[Index.Height] == 7) | ||
.map(row => cols.map(col => row[Index[col]])) | ||
.filter(row => row[cols.indexOf('Age')] == 5) | ||
.map(row => row[cols.indexOf('Date')]) | ||
) | ||
t.end() | ||
}) | ||
tap.test('map + slice', t => { | ||
const dodo = new Dodo(table) | ||
const expected = array.map(row => row[Index.Date] * 2).slice(2, 4) | ||
t.same( | ||
dodo.map((row, I) => row[I.Date] * 2).slice(2, 4).toArray(), | ||
expected | ||
) | ||
t.same( | ||
dodo.slice(2, 4).map((row, I) => row[I.Date] * 2).toArray(), | ||
expected | ||
) | ||
// TODO: map + slice + map + slice | ||
// TODO: slice + map + slice + map | ||
t.end() | ||
}) | ||
tap.test('filter + slice', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
[...dodo.filter((row, I) => row[I.Date] == 4).slice(1)], | ||
array.filter(row => row[Index.Date] == 4).slice(1) | ||
) | ||
t.same( | ||
[...dodo.slice(1).filter((row, I) => row[I.Date] == 4)], | ||
array.slice(1).filter(row => row[Index.Date] == 4) | ||
) | ||
t.same( | ||
dodo | ||
.filter((row, I) => row[I.Date] >= 4) | ||
.slice(1) | ||
.filter((row, I) => row[I.Height] == 7) | ||
.slice(0) | ||
.toArray(), | ||
array | ||
.filter(row => row[Index.Date] >= 4) | ||
.slice(1) | ||
.filter(row => row[Index.Height] == 7) | ||
.slice(0) | ||
) | ||
t.same( | ||
dodo | ||
.slice(1) | ||
.filter((row, I) => row[I.Date] >= 4) | ||
.slice(1) | ||
.filter((row, I) => row[I.Height] == 7) | ||
.toArray(), | ||
array | ||
.slice(1) | ||
.filter(row => row[Index.Date] >= 4) | ||
.slice(1) | ||
.filter(row => row[Index.Height] == 7) | ||
) | ||
t.end() | ||
}) | ||
tap.test('uniq', t => { | ||
const dodo = new Dodo(table) | ||
t.true(Array.isArray(dodo.col('Date').uniq())) | ||
t.same( | ||
dodo.col('Date').uniq(), | ||
[...new Set(array.map(row => row[Index.Date]))] | ||
) | ||
t.end() | ||
}) | ||
tap.test('map + filter + slice') | ||
tap.test('reduce of single column', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.col('Age').reduce((acc, a) => acc * a, 1), | ||
array.map(row => row[Index.Age]).reduce((acc, a) => acc * a, 1) | ||
) | ||
t.end() | ||
}) | ||
tap.test('reduce over multiple columns', t => { | ||
const dodo = new Dodo(table) | ||
t.same( | ||
dodo.reduceEach((acc, a) => acc + a, 0), | ||
Object.values(Index) | ||
.map(i => array.reduce((arr, row) => [...arr, row[i]], [])) | ||
.map(arr => arr.reduce((acc, a) => acc + a, 0)) | ||
) | ||
t.end() | ||
}) | ||
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('groupBy', t => { | ||
const dodo = new Dodo(table) | ||
const grouped = dodo.groupBy('Age') | ||
dodo.col('Age').uniq().forEach(uniq => { | ||
t.true(grouped.has(uniq)) | ||
t.true(grouped.get(uniq) instanceof Dodo) | ||
t.true(grouped.get(uniq).actions.length == dodo.actions.length + 1) | ||
}) | ||
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.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
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
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
80695
822
12
1
1
+ Addedinvariant@^2.2.0
+ Addedinvariant@2.2.4(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedloose-envify@1.4.0(transitive)