UNPKG

report-builder

Version:
2 lines (1 loc) 2.01 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(this,function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var e=require("object-path"),r=require("./utils"),o=function(){function o(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();t(this,o),this.report={summary:{total:{count:0,label:r},timestamp:n,notes:e},results:{}}}return o.prototype.section=function(t){return new n(r(this.report,t,{}),this.report)},o.prototype.setTotal=function(t){return this.report.summary.total.count=t,this},o.prototype.calcPercentOf=function(t,r){if(r="string"!=typeof r?"summary.total.count":["results",r,"count"].join("."),"string"!=typeof t||!e.get(this.report.results,t)||isNaN(e.get(this.report,r)))return this;var o=e.get(this.report,["results",t,"count"].join(".")),n=e.get(this.report,r),s=100*o/n;return e.set(this.report.results,t+".percent",s),this},o.prototype.toJSON=function(){return this.report},o}(),n=function(){function o(e,r){t(this,o),this.tree=r,this.store=e}return o.prototype.toJSON=function(){return this.tree},o.prototype.subSection=function(){return this.section.apply(this,arguments)},o.prototype.section=function(t){return new o(r(this.store,t,{}),this.tree)},o.prototype.summary=function(){return new o(this.tree.summary,this.tree)},o.prototype.calcPercent=function(t){if("number"!=typeof this.store.count)return this;var r=this.store.count,o="string"!=typeof t?this.tree.summary.total.count:e.get(this.tree,t);return this.store.percent=100*r/o,this},o.prototype.set=function(t,r){return t&&r?(e.set(this.store,t,r),this):this},o.prototype.increment=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;"number"==typeof t?(r=t,t="count"):(t="string"==typeof t?t.split("."):[],t.push("count"));var o=e.get(this.store,t,0);return e.set(this.store,t,o+r),this},o}();module.exports=o,module.exports.Section=n});