UNPKG

dice-wizard

Version:

A utility package for rolling dice with various configurations

1 lines 1.87 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.DiceWizard=t():e.DiceWizard=t()}(this,(()=>(()=>{"use strict";var e={};return{156:function(e,t){var r=this&&this.__spreadArray||function(e,t,r){if(r||2===arguments.length)for(var i,o=0,n=t.length;o<n;o++)!i&&o in t||(i||(i=Array.prototype.slice.call(t,0,o)),i[o]=t[o]);return e.concat(i||Array.prototype.slice.call(t))};Object.defineProperty(t,"__esModule",{value:!0}),t.DiceWizard=void 0;var i=function(){function e(){this.results=[]}return e.prototype.roll=function(e,t){var r,i;if("string"==typeof e){var o=e.toLowerCase().match(/^(\d+)?d(\d+)$/);if(!o)throw new Error('Invalid dice notation. Use format: "d20" or "3d6"');if(r=o[1]?parseInt(o[1],10):1,i=parseInt(o[2],10),!this.isValidDieType(i))throw new Error("Invalid die type: d".concat(i))}else if(r=e,i=t,!this.isValidDieType(i))throw new Error("Invalid die type: d".concat(i));for(var n=[],u=0;u<r;u++){var s=Math.floor(Math.random()*i)+1;n.push({value:s,dieType:i})}return this.results=n,n},e.prototype.isValidDieType=function(e){return[4,6,8,10,12,20,100].includes(e)},e.prototype.getSum=function(){return this.results.reduce((function(e,t){return e+t.value}),0)},e.prototype.filterDice=function(e){return this.results.filter(e)},e.prototype.keepValues=function(e){return this.filterDice((function(t){return e.includes(t.value)}))},e.prototype.removeValues=function(e){return this.filterDice((function(t){return!e.includes(t.value)}))},e.prototype.keepHighest=function(e){return r([],this.results,!0).sort((function(e,t){return t.value-e.value})).slice(0,e)},e.prototype.keepLowest=function(e){return r([],this.results,!0).sort((function(e,t){return e.value-t.value})).slice(0,e)},e}();t.DiceWizard=i,t.default=i}}[156](0,e),e.default})()));