UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 3.49 kB
(()=>{"use strict";var e={d:(t,r)=>{for(var i in r)e.o(r,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:r[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyNeedBar:()=>i});class r{#e=new Map;#t;#r;#i;get factors(){const e={};for(let[t,r]of this.#e.entries())e[t]={...r};return e}get currentPercent(){return this.#r/this.#t*100}set maxValue(e){if("number"!=typeof e||Number.isNaN(e)||e<=0)throw new TypeError("maxValue must be a positive number.");this.#t=e,this.#r=Math.min(this.#r,e)}get maxValue(){return this.#t}get currentValue(){return this.#r}set infiniteValue(e){if("number"!=typeof e||Number.isNaN(e))throw new TypeError("infiniteValue must be a number.");this.#i=e,this.#r=Math.max(0,e),this.#r=Math.min(this.#r,this.#t)}get infiniteValue(){return this.#i}constructor(e=100,t=1,r=1){if("number"!=typeof e||Number.isNaN(e)||e<=0)throw new TypeError("maxValue must be a positive number.");this.#t=e,this.setFactor("main",t,r),this.#r=e,this.#i=e}getFactor(e){if("string"!=typeof e||!e)throw new TypeError("Key must be a non-empty string.");const t=this.#e.get(e);if(!t)throw new Error(`Factor with key "${e}" not found.`);return{...t}}hasFactor(e){if("string"!=typeof e||!e)throw new TypeError("Key must be a non-empty string.");return this.#e.has(e)}setFactor(e,t,r=1){if("string"!=typeof e||!e)throw new TypeError("Key must be a non-empty string.");if("number"!=typeof t||Number.isNaN(t))throw new TypeError("Amount must be a valid number.");if("number"!=typeof r||Number.isNaN(r))throw new TypeError("Multiplier must be a valid number.");this.#e.set(e,{amount:t,multiplier:r})}removeFactor(e){if("string"!=typeof e||!e)throw new TypeError("Key must be a non-empty string.");return this.#e.delete(e)}#n(e){const t=this.#i;return this.#i-=e,this.#r=Math.max(0,this.#r-e),{prevValue:t,removedTotal:e,removedPercent:e/this.#t*100,currentPercent:this.currentPercent,remainingValue:this.#r,infiniteRemaining:this.#i}}tick(){let e=0;for(let[t,r]of this.#e.entries())e+=r.amount*r.multiplier;return this.#n(e)}tickWithTempFactor(e){if("object"!=typeof e||null===e)throw new TypeError("You must provide a valid factor object.");if("number"!=typeof e.amount||Number.isNaN(e.amount))throw new TypeError('Temp factor "amount" must be a valid number.');if("multiplier"in e&&("number"!=typeof e.multiplier||Number.isNaN(e.multiplier)))throw new TypeError('Temp factor "multiplier" must be a valid number if provided.');let t=0;for(let[e,r]of this.#e.entries())t+=r.amount*r.multiplier;return e&&(t+=e.amount*(e.multiplier??1)),this.#n(t)}tickSingleFactor(e){if("object"!=typeof e||null===e)throw new TypeError("You must provide a valid factor object.");if("number"!=typeof e.amount||Number.isNaN(e.amount))throw new TypeError('Temp factor "amount" must be a valid number.');if("multiplier"in e&&("number"!=typeof e.multiplier||Number.isNaN(e.multiplier)))throw new TypeError('Temp factor "multiplier" must be a valid number if provided.');if(!e)throw new Error("You must provide a factor to apply.");const t=e.amount*(e.multiplier??1);return this.#n(t)}toJSON(){return{maxValue:this.#t,currentValue:this.#r,infiniteValue:this.#i,factors:this.factors}}static fromJSON(e){const t=new r(e.maxValue,0,0);t.infiniteValue=e.infiniteValue,t.#e.clear();for(const[r,i]of Object.entries(e.factors))t.setFactor(r,i.amount,i.multiplier);return t}clone(){return r.fromJSON(this.toJSON())}clearFactors(){this.#e.clear()}}const i=r;window.TinyNeedBar=t.TinyNeedBar})();