UNPKG

@chiper-inc/ecommerce-lib

Version:
95 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MaxQuantityHelper = exports.getDiscount = exports.roundToMultipleQuantity = exports.getMaximumValue = void 0; /** * Finds the highest quantity that can be sold of an item adjusted to its `multipleQuantity` * @param stock item stock * @param maxQuantity maximum quantity of this item that can be added * @param multipleQuantity `multipleQuantity` for the item * @returns the highest quantity of the item that can be sold */ const getMaximumValue = (stock, maxQuantity, multipleQuantity) => { var _a; const maxValue = Math.min(stock, maxQuantity); const maxQuantityTimes = (_a = Math.floor(maxValue / multipleQuantity)) !== null && _a !== void 0 ? _a : 1; const maxStockMultipleQuantity = maxQuantityTimes * multipleQuantity; return maxStockMultipleQuantity; }; exports.getMaximumValue = getMaximumValue; /** * Find the `multipleQuantity` closest to `quantity` * @param quantity amount you want to add * @param multipleQuantity multipleQuantity of this item * @param stock item stock * @returns quantity that complies with the `multiple Quantity` */ const roundToMultipleQuantity = (quantity, multipleQuantity, stock) => { const roundNumber = Math.round(quantity / multipleQuantity) * multipleQuantity; const roundStock = Math.floor(stock / multipleQuantity) * multipleQuantity; return stock < roundNumber ? roundStock : roundNumber; }; exports.roundToMultipleQuantity = roundToMultipleQuantity; const getDiscount = (regularPrice, discountedPrice) => ((regularPrice - discountedPrice) / regularPrice) * 100; exports.getDiscount = getDiscount; const min = (a, b) => { if (a === undefined) { return b; } if (b === undefined) { return a; } return a < b ? a : b; }; class MaxCounter { constructor(max, counter) { this._count = 0; this._max = max !== null && max !== void 0 ? max : undefined; this._counter = counter; } get max() { var _a; return min(this._max ? this._max - this._count : undefined, (_a = this._counter) === null || _a === void 0 ? void 0 : _a.max); } get maxAndAdd() { const maxQuantity = this.max; this.add(maxQuantity || 0); return maxQuantity; } set counter(counter) { this._counter = counter; } add(count) { var _a; this._count += count; (_a = this._counter) === null || _a === void 0 ? void 0 : _a.add(count); } } class MaxQuantityHelper { constructor(productMaxQuantity, discountedMaximumQuantity) { this._scales = new Map(); this._quantity = new MaxCounter(productMaxQuantity); this._discounted = new MaxCounter(discountedMaximumQuantity, this._quantity); } scaleMaxQuantity(counter, startQuantity = 1, endQuantity) { if (endQuantity) { const scale = this._scales.get(startQuantity); if (scale) { scale.counter = counter; counter = scale; } else { counter = new MaxCounter(endQuantity - startQuantity + 1, counter); this._scales.set(startQuantity, counter); } } return counter.maxAndAdd; } maxQuantity(startQuantity = 1, endQuantity) { return this.scaleMaxQuantity(this._quantity, startQuantity, endQuantity); } discountedMaxQuantity(startQuantity = 1, endQuantity) { return this.scaleMaxQuantity(this._discounted, startQuantity, endQuantity); } } exports.MaxQuantityHelper = MaxQuantityHelper; //# sourceMappingURL=helpers.js.map