UNPKG

alpinejs-number

Version:

Enhanced Numeric Input for Alpine.js: Simplifying Number Formatting and Currency Handling.

2 lines (1 loc) 3.09 kB
function y(h){h.directive("number",(t,{value:n,modifiers:s,expression:f},{cleanup:i})=>{let l=(f??"").split("|"),r=l?.[0]??"",o=l?.[3]??n??"",d=l?.[1]??",",u=l?.[2]??".",g=parseInt(s.find(e=>!isNaN(parseInt(e)))??-1),p=s.find(e=>e==="unsigned")!==void 0,R={prefix:r,suffix:o,separator:d,decimalChar:u,precision:g,unsigned:p};m({target:t});let b=()=>{let e=t.value,a=t.selectionStart;a<r.length&&t.setSelectionRange(r.length,r.length),a>e.length-o.length&&t.setSelectionRange(e.length-o.length,e.length-o.length)};t.addEventListener("click",b);let k=e=>{if(e.key!=="-")return;let a=t.value,c=t.selectionStart,L=t.selectionEnd;if(a=a.substring(0,c)+a.substring(L),a!==""){if(t.value.startsWith(r+"-")){t.value=t.value.replace("-",""),t.setSelectionRange(c-1,c-1),t.dispatchEvent(new Event("input",{bubbles:!0})),e.preventDefault();return}t.value=t.value.replace(r,r+"-"),t.setSelectionRange(c+1,c+1),t.dispatchEvent(new Event("input",{bubbles:!0})),e.preventDefault()}};p||t.addEventListener("keydown",k);let E=e=>{if(e.key!=="ArrowLeft"&&e.key!=="ArrowRight"&&e.key!=="ArrowUp"&&e.key!=="ArrowDown")return;let a=t.value,c=t.selectionStart;e.key==="ArrowLeft"&&c--,e.key==="ArrowRight"&&c++,e.key==="ArrowUp"&&(c=0),e.key==="ArrowDown"&&(c=a.length),c<r.length&&(t.setSelectionRange(r.length,r.length),e.preventDefault()),c>a.length-o.length&&(t.setSelectionRange(a.length-o.length,a.length-o.length),e.preventDefault())};t.addEventListener("keydown",E),t.addEventListener("input",m);function m(e){let{value:a,cursorPosition:c}=w(e.target.value,e.target.selectionStart,e?.data===u,R);e.target.value!==a&&(e.target.value=a,t.dispatchEvent(new Event("input",{bubbles:!0}))),e.target.setSelectionRange(c,c)}i(()=>{t.removeEventListener("input",m),t.removeEventListener("click",b),t.removeEventListener("keydown",E),p||t.removeEventListener("keydown",k)})}),h.magic("toNumber",()=>(t,n=".",s=-1)=>Number(v(String(t),0,!1,{decimalChar:n,precision:s,unsigned:!1}).value.replace(n,"."))),h.magic("toFormatted",()=>(t,n="",s=",",f=".",i="",l=-1,r=!1)=>w(t,0,!1,{prefix:n,suffix:i,separator:s,decimalChar:f,precision:l,unsigned:r}).value);function v(t,n,s,{decimalChar:f,precision:i,unsigned:l}){let r=S(t,!l),o=s?n-1:t.indexOf(f);if((i>0||i===-1)&&o!==-1&&(r=r.filter(u=>o!==u)),i>0&&o!==-1){let u=0;for(let g=o+1;g<t.length;g++)r.includes(g)||(u++,u>i&&r.push(g))}t=N(t,r);let d=r.reduce((u,g)=>u+(g<n?1:0),0);return n=n-d,{value:t,cursorPosition:n}}function w(t,n,s,f){({value:t,cursorPosition:n}=v(t,n,s,f));let{prefix:i,suffix:l,separator:r,decimalChar:o}=f,d=t.startsWith("-");n=n-(d?1:0),t=t.replace("-","");let u=t.split(o),g=Math.max(0,Math.floor((Math.min(n,u[0].length)-1)/3));return u[0]=u[0].replace(/\B(?=(\d{3})+(?!\d))/g,r),t=i+(d?"-":"")+u.join(o)+l,n=n+i.length+(d?1:0)+g,t===i+l&&(t="",n=0),{value:t,cursorPosition:n}}function S(t,n){let s=[],f=!1;for(let i=0;i<t.length;i++)if(isNaN(parseInt(t[i]))){if(!f&&t[i]==="-"&&n){f=!0;continue}s.push(i)}else f=!0;return s}function N(t,n){let s=new Set(n);return t.split("").filter((i,l)=>!s.has(l)).join("")}}var D=y;export{D as default};