You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jQuery(function($){/** * Utility: Get decimal places of a number string */if(!String.prototype.getDecimals){String.prototype.getDecimals=function(){constmatch=String(this).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);if(!match)return0;constdecimals=match[1] ? match[1].length : 0;constexponent=match[2] ? parseInt(match[2],10) : 0;returnMath.max(0,decimals-exponent);};}/** * Reusable function to update quantity value * @param {jQuery} $input - jQuery object of the input element * @param {boolean} increment - true to increment, false to decrement */functionupdateQuantity($input,increment){letcurrentVal=parseFloat($input.val())||0;letmax=parseFloat($input.attr('max'));letmin=parseFloat($input.attr('min'))||0;letstepAttr=$input.attr('step');letstep=(stepAttr==='any'||!stepAttr||isNaN(parseFloat(stepAttr))) ? 1 : parseFloat(stepAttr);if(increment){if(!isNaN(max)&¤tVal>=max){$input.val(max);}else{$input.val((currentVal+step).toFixed(String(step).getDecimals()));$input.val(currentVal+step).attr("value",(currentVal+step));}}else{if(!isNaN(min)&¤tVal<=min){$input.val(min);}elseif(currentVal>0){$input.val((currentVal-step).toFixed(String(step).getDecimals()));$input.val(currentVal-step).attr("value",(currentVal-step));}}$input.trigger('change');}/** * Handle quantity increment/decrement */$(document.body).on('click','.cart-plus, .cart-minus',function(){const$qty=$(this).closest('.product-quantity').find('.cart-input');updateQuantity($qty,$(this).hasClass('cart-plus'));});$('.cart-input').on('keydown',function(e){const$qty=$(this);if(e.key==="ArrowUp"){updateQuantity($qty,true);}if(e.key==="ArrowDown"){updateQuantity($qty,false);}});});
Emphasized readability and maintainability.
jQuery(function($){// Utility: Get decimal places of a number stringif(!String.prototype.getDecimals){String.prototype.getDecimals=function(){constmatch=String(this).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);if(!match)return0;constdecimals=match[1] ? match[1].length : 0;constexponent=match[2] ? parseInt(match[2],10) : 0;returnMath.max(0,decimals-exponent);};}// Handle quantity increment/decrement$(document.body).on('click','.cart-plus, .cart-minus',function(){const$qty=$(this).closest('.product-quantity').find('.cart-input');letcurrentVal=parseFloat($qty.val())||0;letmax=parseFloat($qty.attr('max'));letmin=parseFloat($qty.attr('min'))||0;letstepAttr=$qty.attr('step');letstep=(stepAttr==='any'||!stepAttr||isNaN(parseFloat(stepAttr))) ? 1 : parseFloat(stepAttr);// Increment or decrementif($(this).hasClass('cart-plus')){if(!isNaN(max)&¤tVal>=max){$qty.val(max);}else{$qty.val((currentVal+step).toFixed(String(step).getDecimals()));$qty.val(currentVal+step).attr("value",(currentVal+step));}}else{if(!isNaN(min)&¤tVal<=min){$qty.val(min);}elseif(currentVal>0){$qty.val((currentVal-step).toFixed(String(step).getDecimals()));$qty.val(currentVal-step).attr("value",(currentVal-step));}}// Trigger change event$qty.trigger('change');});});
Simplified complex logic into clean code.
$('.cart-minus').on('click',function(){var$input=$(this).parent().find('input');varcount=parseInt($input.val())-1;count=count<1 ? 1 : count;$input.val(count);$input.change();returnfalse;});$('.cart-plus').on('click',function(){var$input=$(this).parent().find('input');varcurrentVal=parseInt($input.val())||0;varmaxVal=$input.attr('max');// Read the max attribute.if(maxVal){// If the max attribute exists, it must be enforced.maxVal=parseInt(maxVal);if(currentVal<maxVal){$input.val(currentVal+1);$input.change();}}else{// If the max attribute is absent, allow unlimited increments.$input.val(currentVal+1);$input.change();}returnfalse;});