numora 3.3.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/NumoraInput.d.ts +3 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +329 -280
- package/dist/types.d.ts +1 -2
- package/dist/utils/event-handlers.d.ts +14 -0
- package/dist/utils/locale.d.ts +3 -6
- package/package.json +2 -2
package/dist/NumoraInput.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface NumoraInputOptions extends Partial<Omit<HTMLInputElement, 'valu
|
|
|
6
6
|
decimalSeparator?: string;
|
|
7
7
|
decimalMaxLength?: number;
|
|
8
8
|
decimalMinLength?: number;
|
|
9
|
+
locale?: string | true;
|
|
9
10
|
enableCompactNotation?: boolean;
|
|
10
11
|
enableNegative?: boolean;
|
|
11
12
|
enableLeadingZeros?: boolean;
|
|
@@ -19,13 +20,14 @@ export declare class NumoraInput {
|
|
|
19
20
|
private resolvedOptions;
|
|
20
21
|
private rawValue;
|
|
21
22
|
private caretPositionBeforeChange?;
|
|
22
|
-
constructor(container: HTMLElement, { decimalMaxLength, decimalMinLength, formatOn, thousandSeparator, thousandStyle, decimalSeparator, enableCompactNotation, enableNegative, enableLeadingZeros, rawValueMode, onChange, ...rest }: NumoraInputOptions);
|
|
23
|
+
constructor(container: HTMLElement, { decimalMaxLength, decimalMinLength, formatOn, thousandSeparator, thousandStyle, decimalSeparator, locale, enableCompactNotation, enableNegative, enableLeadingZeros, rawValueMode, onChange, ...rest }: NumoraInputOptions);
|
|
23
24
|
private createInputElement;
|
|
24
25
|
private setupEventListeners;
|
|
25
26
|
private getResolvedOptions;
|
|
26
27
|
private buildFormattingOptions;
|
|
27
28
|
private handleValueChange;
|
|
28
29
|
private formatValueForDisplay;
|
|
30
|
+
private handleBeforeInput;
|
|
29
31
|
private handleChange;
|
|
30
32
|
private handleKeyDown;
|
|
31
33
|
private handlePaste;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './NumoraInput';
|
|
2
2
|
export { ThousandStyle, FormatOn } from './types';
|
|
3
|
-
export { getSeparatorsFromLocale,
|
|
4
|
-
export { handleOnChangeNumoraInput, handleOnPasteNumoraInput, handleOnKeyDownNumoraInput, } from './utils/event-handlers';
|
|
3
|
+
export { getSeparatorsFromLocale, applyLocale } from './utils/locale';
|
|
4
|
+
export { handleOnBeforeInputNumoraInput, handleOnChangeNumoraInput, handleOnPasteNumoraInput, handleOnKeyDownNumoraInput, } from './utils/event-handlers';
|
|
5
5
|
export { formatValueForDisplay, formatInputValue, } from './utils/format-utils';
|
|
6
6
|
export type { FormattingOptions, CaretPositionInfo } from './types';
|
|
7
7
|
export { removeThousandSeparators } from './features/sanitization';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var le=Object.defineProperty;var he=(e,t,n)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var C=(e,t,n)=>he(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var L=(e=>(e.Blur="blur",e.Change="change",e))(L||{}),p=(e=>(e.None="none",e.Thousand="thousand",e.Lakh="lakh",e.Wan="wan",e.Locale="locale",e))(p||{});const G=2,B=0,Z=L.Blur,ue=",",H=p.None,y=".",k=!1,W=!1,U=!1,P=!1;function w(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}const z=new Map;function O(e,t="g"){const n=`${e}:${t}`;let r=z.get(n);return r||(r=new RegExp(e,t),z.set(n,r)),r}function de(e,t="g"){const n=w(e);return O(n,t)}const fe=/[.,]/g;function _(e){return{decimalSeparator:(e==null?void 0:e.decimalSeparator)??y,thousandSeparator:e==null?void 0:e.thousandSeparator}}function Y(e,t){const n=e.startsWith("-"),r=n?e.slice(1):e,[i="",s=""]=r.split(t);return{sign:n?"-":"",integer:i,decimal:s}}function ge(e,t){if(!e.value.includes(t))return!1;const{selectionStart:n,selectionEnd:r,value:i}=e;return!i.slice(n??0,r??0).includes(t)}function Se(e,t,n){const{key:r}=e;if(r!==","&&r!==".")return!1;if(ge(t,n))return!0;if(r!==n){const{selectionStart:i,selectionEnd:s,value:a}=t,c=i??0,l=s??c;t.value=a.slice(0,c)+n+a.slice(l);const o=c+1;return t.setSelectionRange(o,o),!0}return!1}const Ee=(e,t,n=y)=>{const{sign:r,integer:i,decimal:s}=Y(e,n);if(!s)return e;const a=s.slice(0,t);return`${r}${i}${n}${a}`},me=(e,t=y)=>{const n=e.indexOf(t);if(n===-1||n===e.length-1)return e;const r=e.slice(0,n+1),i=e.slice(n+1),a=t==="."||t===","?fe:O("[,\\."+w(t)+"]","g");return r+i.replace(a,"")},pe=(e,t=0,n=y)=>{if(t<=0)return e;const{sign:r,integer:i,decimal:s}=Y(e,n);if(s.length>=t)return e;const a=s.padEnd(t,"0");return`${r}${i}${n}${a}`},D={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}},be=/^(0+)/;function Q(e,t,n=p.Thousand,r=!1,i="."){if(!e||e==="0"||e===i||e==="-"||e===`-${i}`)return e;const s=e.includes(i),a=e.startsWith("-"),c=a?e.slice(1):e,[l,o]=c.split(i);if(!l){const u=o?`${i}${o}`:c;return a?`-${u}`:u}if(r&&l.startsWith("0")&&l.length>1){const u=l.match(be);if(u){const S=u[1],E=l.slice(S.length);if(E){const m=J(E,t,n),f=S+m,g=a?"-":"";return s?o?`${g}${f}${i}${o}`:`${g}${f}${i}`:`${g}${f}`}}}const h=J(l,t,n),d=a?"-":"";return s?o?`${d}${h}${i}${o}`:`${d}${h}${i}`:`${d}${h}`}function J(e,t,n){if(e==="0"||e==="")return e;switch(n){case p.None:return e;case p.Thousand:return X(e,t,D.thousand.size);case p.Lakh:return Le(e,t);case p.Wan:return X(e,t,D.wan.size);default:return e}}function Le(e,t){if(e.length<=D.lakh.firstGroup)return e;const n=[],r=e.length-D.lakh.firstGroup;n.unshift(e.slice(r));for(let i=r;i>0;i-=D.lakh.restGroup)n.unshift(e.slice(Math.max(0,i-D.lakh.restGroup),i));return n.join(t)}function X(e,t,n){const r=[];for(let i=e.length;i>0;i-=n)r.unshift(e.slice(Math.max(0,i-n),i));return r.join(t)}function Ne(e,t,n){return(t==null?void 0:t.formatOn)===L.Change&&t.thousandSeparator?Q(e,t.thousandSeparator,t.ThousandStyle??p.None,t.enableLeadingZeros,(n==null?void 0:n.decimalSeparator)??y):e}function $(e,t,n){let r=0;for(let i=0;i<t&&i<e.length;i++)e[i]!==n&&r++;return r}function ve(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r===t-1)return i+1;r++}return e.length}function T(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r++,r===t)return i+1;if(r>t)return i}return e.length}function V(e,t,n){return t<0||t>=e.length?!1:e[t]===n}const $e=/\d/;function De(e,t={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:i="",suffix:s=""}=t,a=new Array(e.length+1).fill(!0);if(i&&a.fill(!1,0,i.length),s){const c=e.length-s.length;a.fill(!1,c+1,e.length+1)}for(let c=0;c<e.length;c++){const l=e[c];(n&&l===n||l===r)&&(a[c]=!1,c+1<e.length&&!$e.test(e[c+1])&&(a[c+1]=!1))}return a.some(c=>c)||a.fill(!0),a}function x(e,t,n,r){const i=e.length;if(t=Math.max(0,Math.min(t,i)),r==="left"){for(;t>=0&&!n[t];)t--;t===-1&&(t=n.indexOf(!0))}else if(r==="right"){for(;t<=i&&!n[t];)t++;t>i&&(t=n.lastIndexOf(!0))}else if(!n[t]){let s=t;for(;s<=i&&!n[s];)s++;let a=t;for(;a>=0&&!n[a];)a--;s<=i&&a>=0?t=t-a<s-t?a:s:s<=i?t=s:a>=0&&(t=a)}return(t===-1||t>i)&&(t=i),t}const j=/(\d+\.?\d*)\s*[kmbt]$/i,ye=(e,t)=>e===t;function Ce(e,t,n,r,i,s,a=".",c={}){if(n<0)return 0;if(n>e.length||e===""||t===""||j.test(e)&&!j.test(t)&&t.length>e.length&&n>=e.length-1)return t.length;const l=t.length<e.length,o=V(e,n,r),h=e.indexOf(a),d=t.indexOf(a);if(c.isCharacterEquivalent&&e!==t){const f={thousandSeparator:r||void 0,...c},g=we(e,t,n,c.isCharacterEquivalent||ye,s,f);if(g!==void 0)return g}const S=new Map,E=(f,g,N)=>{const b=`${f===e?"o":"n"}:${g}`;return S.has(b)||S.set(b,$(f,g,r)),S.get(b)};if(l)return Ie(e,t,n,r,o,h,d,s,c,E);const m=Te(e,t,n,r,o,E);return c.boundary?x(t,m,c.boundary):m}function we(e,t,n,r,i,s){const a=e.length,c=t.length;let l=0,o=0,h=c;for(let g=0;g<a;g++){let N=-1;for(let b=l;b<c;b++)if(r(e[g],t[b],{oldValue:e,newValue:t,typedRange:i,oldIndex:g,newIndex:b})){N=b;break}N!==-1&&(l=N+1,g<n?o=N+1:h===c&&/\d/.test(e[g])&&(h=N))}if(o>h)return h;const d=t.length<e.length||i!==void 0&&i.isDelete===!1;let u,S=!1,E=!1;if(d){u=o;const g=s.thousandSeparator,N=s.decimalSeparator??".",b=u<t.length?t[u]:"";g&&b===g?(n>0?e[n-1]:"")===g?(u=h,S=!0):E=!0:b===N&&(E=!0)}else u=n-o<h-n?o:h;const m=d&&!S?"left":"right";if(s.boundary)return E?u:x(t,u,s.boundary,m);const f=s.thousandSeparator;if(!E&&f&&u>=0&&u<t.length&&t[u]===f)if(d&&!S)for(;u>0&&t[u]===f;)u--;else for(;u<t.length&&t[u]===f;)u++;return u}function Ie(e,t,n,r,i,s,a,c,l={},o=$){if(i)return Me(e,t,n,r,a,o);const h=o(e,n,r),d=o(e,e.length,r),u=o(t,t.length,r),S=d-u,E=Ae(e,n,r,h,S,s,c,o),m=s===-1||n<=s,f=Re(t,E,r,S,c,m,a,o);return l.boundary?x(t,f,l.boundary):f}function Me(e,t,n,r,i,s=$){const a=n+1;if(a<e.length){const c=s(e,a,r),l=ve(t,c,r);return l<t.length&&t[l]!==r?l+1:l}return n}function Ae(e,t,n,r,i,s,a,c=$){if(a){const{start:o,isDelete:h}=a;return h?c(e,o,n):Math.max(0,c(e,o,n))}return t>0&&e[t-1]===n&&i>0?r+1:r}function K(e,t,n,r,i,s,a=$){if(t>0&&t<e.length&&a(e,t,r)===n){if(e[t]===r&&s&&i>0&&t<e.length-1)return t+1;if(!s&&i>0&&t<e.length-1)return Math.min(t+1,e.length)}return t}function Re(e,t,n,r,i,s,a,c=$){if(s&&a!==-1){const o=e.substring(0,a),h=c(o,o.length,n);if(t<=h){const d=T(o,t,n);return K(o,d,t,n,r,i,c)}}const l=T(e,t,n);return K(e,l,t,n,r,i,c)}function Te(e,t,n,r,i,s=$){const a=n>=e.length,c=s(e,n,r),l=s(e,e.length,r),o=s(t,t.length,r);if(a||c===l)return t.length;const h=o-l;let d=c;h>0&&!a&&c<l&&(d=c+1);const u=T(t,d,r);return i&&!V(t,u,r)?Math.max(0,u-1):u}function Oe(e,t,n){const{selectionStart:r,selectionEnd:i,endOffset:s=0}=e;if(r!==i){const h=i-r;return{start:r,end:i,deletedLength:h,isDelete:!1}}if(s>0){const h=s;return{start:r,end:r+h,deletedLength:h,isDelete:!0}}const c=t.length,l=n.length,o=c-l;if(!(o<=0))return{start:r,end:r+o,deletedLength:o,isDelete:!1}}function _e(e,t){if(e===t)return;let n=0;for(;n<e.length&&n<t.length&&e[n]===t[n];)n++;let r=e.length-1,i=t.length-1;for(;r>=n&&i>=n&&e[r]===t[i];)r--,i--;const s=r-n+1,a=i-n+1;if(!(s===0&&a===0))return{start:n,end:r+1,deletedLength:s,isDelete:s>a}}function q(e,t){if(e.value=e.value,e===null)return!1;if(e.createTextRange){const n=e.createTextRange();return n.move("character",t),n.select(),!0}return e.selectionStart!==null||e.selectionStart===0?(e.focus(),e.setSelectionRange(t,t),!0):(e.focus(),!1)}function xe(e,t,n){return e.selectionStart===0&&e.selectionEnd===e.value.length?null:(q(e,t),setTimeout(()=>{e.value===n&&e.selectionStart!==t&&q(e,t)},0))}function Ge(e){return Math.max(e.selectionStart,e.selectionEnd)}function Be(e,t,n){if((n==null?void 0:n.formatOn)!==L.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:i,value:s}=t;if(r===null||i===null||r!==i)return;const{key:a}=e,c=n.thousandSeparator;a==="Backspace"&&r>0&&s[r-1]===c&&t.setSelectionRange(r-1,r-1),a==="Delete"&&s[r]===c&&t.setSelectionRange(r+1,r+1)}function Ze(e,t,n,r,i,s,a){if(!i)return;const{selectionStart:c=0,selectionEnd:l=0,endOffset:o=0}=i;let h=Oe({selectionStart:c,selectionEnd:l,endOffset:o},t,n);if(h||(h=_e(t,n)),!h)return;const d=De(n,{thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator}),u={thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator,isCharacterEquivalent:(f,g)=>{const N=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator;return N&&f===N?!1:f===g},boundary:d},S=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator??",",E=(a==null?void 0:a.ThousandStyle)??p.None,m=Ce(t,n,r,S,E,h,s.decimalSeparator,u);xe(e,m,n)}const ke=(e,t=!1,n=".")=>{const r=w(n),i=O(`[^0-9${r}]`,"g");if(!t)return e.replace(i,"");const s=e.startsWith("-"),a=e.replace(i,"");return s&&(a.length>0||e==="-")?"-"+a:a},We=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g,F=/^0+$/,A=/\.?0+$/;function Ue(e){return!e.includes("e")&&!e.includes("E")?e:e.replace(We,(t,n,r)=>{const i=parseInt(r,10);if(i===0)return n;const s=n.startsWith("-"),a=s?n.slice(1):n,[c,l=""]=a.split("."),o=i>0?Pe(c,l,i):ze(c,l,Math.abs(i));return s?"-"+o:o})}function Pe(e,t,n){const r=e+t;if(r==="0"||F.test(r))return"0";const i=t.length;if(n<=i){const a=r.slice(0,e.length+n),c=r.slice(e.length+n);return c?`${a}.${c}`:a}const s=n-i;return r+"0".repeat(s)}function ze(e,t,n){const r=e+t;if(r==="0"||F.test(r))return"0";const s=e.length-n;if(s<=0){const a=Math.abs(s),c=`0.${"0".repeat(a)}${r}`;return R(c)}if(s<e.length){const a=r.slice(0,s),c=r.slice(s),l=`${a}.${c}`;return R(l)}return R(r)}function R(e){if(!e.includes("."))return e;if(e==="0.")return"0";if(e.startsWith("0.")){const t=e.replace(A,"");return t==="0"?"0":t||"0"}if(e.startsWith("-0.")){const t=e.replace(A,"");return t==="-0"||t==="0"?"0":t||"0"}return e.replace(A,"")||e}const Je=/(\d+\.?\d*)\s*([kmbt])/gi,Xe=/^0+/,je=/^(-?)0+([1-9])/,Ke=/^-?0+$/,qe=/\.?0+$/,He={k:3,m:6,b:9,t:12};function Ye(e){return e.replace(Je,(t,n,r)=>{const i=r.toLowerCase(),s=He[i];if(!s)return t;const[a,c=""]=n.split("."),l=a.replace(Xe,"")||"0";let o;if(c.length===0)o=l+"0".repeat(s);else if(c.length<=s){const h=s-c.length;o=l+c+"0".repeat(h)}else{const h=c.slice(0,s),d=c.slice(s);o=l+h+"."+d}return o=o.replace(je,"$1$2"),Ke.test(o)&&(o="0"),o.includes(".")&&(o=o.replace(qe,"")),o})}function Qe(e){if(!e||e==="0"||e==="-0"||e==="-"||e===".")return e;const t=e.startsWith("-"),n=t?e.slice(1):e;if(n===".")return e;if(n.includes(".")){const[i,s]=n.split(".");if(i){const c=(i.replace(/^0+/,"")||"0")+"."+s;return t?"-"+c:c}return e}if(n.startsWith("0")){const i=n.replace(/^0+/,"")||"0";return t?"-"+i:i}return e}function Ve(e){return e.replace(/[\s\u200B]/g,"")}function v(e,t){const n=de(t);return e.replace(n,"")}const Fe=(e,t)=>{let n=Ve(e);return t!=null&&t.thousandSeparator&&(n=v(n,t.thousandSeparator)),t!=null&&t.enableCompactNotation&&(n=Ye(n)),n=Ue(n),n=ke(n,t==null?void 0:t.enableNegative,t==null?void 0:t.decimalSeparator),n=me(n,t==null?void 0:t.decimalSeparator),t!=null&&t.enableLeadingZeros||(n=Qe(n)),n};function et(e,t,n){return{enableCompactNotation:e==null?void 0:e.enableCompactNotation,enableNegative:e==null?void 0:e.enableNegative,enableLeadingZeros:e==null?void 0:e.enableLeadingZeros,decimalSeparator:t.decimalSeparator,thousandSeparator:n?t.thousandSeparator:void 0}}function M(e,t,n,r){const i=_(n),s=r??(n==null?void 0:n.formatOn)===L.Change,a=Fe(e,et(n,i,s)),c=Ee(a,t,i.decimalSeparator),l=(n==null?void 0:n.decimalMinLength)??0,o=pe(c,l,i.decimalSeparator);return{formatted:Ne(o,n,i),raw:o}}function tt(e,t,n){const r=!!(n!=null&&n.thousandSeparator);return M(e,t,n,r)}function nt(e,t,n){if(e==="Backspace"||e==="Delete")return e==="Delete"&&t===n?{endOffset:1}:{endOffset:0}}function ee(e,t){const{decimalSeparator:n}=_(t),r=e.target;if(Se(e,r,n)){e.preventDefault();return}return Be(e,r,t),nt(e.key,r.selectionStart,r.selectionEnd)}function te(e,t,n,r){const i=e.target,s=i.value,a=Ge(i),c=_(r),l=(r==null?void 0:r.formatOn)===L.Change,{formatted:o,raw:h}=M(s,t,r,l);return i.value=o,s!==o&&Ze(i,s,o,a,n,c,r),{formatted:o,raw:h}}function rt(e,t,n,r){const i=r-n;return e+t+i}function ne(e,t,n){var u;e.preventDefault();const r=e.target,{value:i,selectionStart:s,selectionEnd:a}=r,c=((u=e.clipboardData)==null?void 0:u.getData("text/plain"))||"",l=i.slice(0,s||0)+c+i.slice(a||0),{formatted:o,raw:h}=M(l,t,n,!0);r.value=o;const d=rt(s||0,c.length,l.length,o.length);return r.setSelectionRange(d,d),{formatted:o,raw:h}}function re(e,t){const n=w(e);return t?`^-?[0-9]*[${n}]?[0-9]*$`:`^[0-9]*[${n}]?[0-9]*$`}function ie(e){var t,n;try{const i=new Intl.NumberFormat(e).formatToParts(123456789e-2);return{decimalSeparator:((t=i.find(s=>s.type==="decimal"))==null?void 0:t.value)??".",thousandSeparator:((n=i.find(s=>s.type==="group"))==null?void 0:n.value)??","}}catch{return{thousandSeparator:",",decimalSeparator:"."}}}function se(e){let t=e.thousandSeparator??ue,n=e.decimalSeparator??y,r=e.thousandStyle??H;if(e.thousandStyle===p.Locale||e.decimalSeparator==="auto"){const s=ie();e.thousandStyle===p.Locale&&(t=e.thousandSeparator??s.thousandSeparator,n=e.decimalSeparator==="auto"||e.decimalSeparator===void 0?s.decimalSeparator:e.decimalSeparator,r=p.Thousand),e.decimalSeparator==="auto"&&(n=s.decimalSeparator)}return{thousandSeparator:t,thousandStyle:r,decimalSeparator:n}}function ae(e){it(e.decimalMaxLength),st(e.decimalMinLength),at(e.decimalMinLength,e.decimalMaxLength),ct(e.formatOn),ot(e.thousandSeparator),lt(e.thousandStyle),ht(e.decimalSeparator),ut(e.thousandSeparator,e.decimalSeparator),I("enableCompactNotation",e.enableCompactNotation),I("enableNegative",e.enableNegative),I("enableLeadingZeros",e.enableLeadingZeros),I("rawValueMode",e.rawValueMode),dt(e.onChange)}function it(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMaxLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMaxLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMaxLength must be non-negative. Received: ${e}`)}}function st(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMinLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMinLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMinLength must be non-negative. Received: ${e}`)}}function at(e,t){if(!(e===void 0||t===void 0)&&e>t)throw new Error(`decimalMinLength (${e}) cannot be greater than decimalMaxLength (${t}).`)}function ct(e){if(e!==void 0&&e!==L.Blur&&e!==L.Change)throw new Error(`formatOn must be either ${L.Blur} or ${L.Change}. Received: ${JSON.stringify(e)}`)}function ot(e){if(e!==void 0){if(typeof e!="string")throw new Error(`thousandSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`thousandSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`thousandSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function lt(e){if(e!==void 0&&!Object.values(p).includes(e))throw new Error(`ThousandStyle must be one of: ${Object.values(p).map(t=>`'${t}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function ht(e){if(!(e===void 0||e==="auto")){if(typeof e!="string")throw new Error(`decimalSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`decimalSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`decimalSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function ut(e,t){if(!(e===void 0||t===void 0)&&t!=="auto"&&e===t)throw new Error(`Decimal separator can't be same as thousand separator. thousandSeparator: ${e}, decimalSeparator: ${t}`)}function I(e,t){if(t!==void 0&&typeof t!="boolean")throw new Error(`${e} must be a boolean. Received: ${typeof t} (${JSON.stringify(t)})`)}function dt(e){if(e!==void 0&&typeof e!="function")throw new Error(`onChange must be a function or undefined. Received: ${typeof e} (${JSON.stringify(e)})`)}class ft{constructor(t,{decimalMaxLength:n=G,decimalMinLength:r=B,formatOn:i=Z,thousandSeparator:s,thousandStyle:a=H,decimalSeparator:c,enableCompactNotation:l=k,enableNegative:o=W,enableLeadingZeros:h=U,rawValueMode:d=P,onChange:u,...S}){C(this,"element");C(this,"resolvedOptions");C(this,"rawValue","");C(this,"caretPositionBeforeChange");ae({decimalMaxLength:n,decimalMinLength:r,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,enableCompactNotation:l,enableNegative:o,enableLeadingZeros:h,rawValueMode:d,onChange:u});const E={decimalMaxLength:n,decimalMinLength:r,onChange:u,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,enableCompactNotation:l,enableNegative:o,enableLeadingZeros:h,rawValueMode:d,...S};if(this.resolvedOptions=this.getResolvedOptions(E),this.createInputElement(t,E),this.setupEventListeners(),this.resolvedOptions.rawValueMode&&this.element.value){const m=this.element.value,f=this.resolvedOptions.thousandSeparator?v(m,this.resolvedOptions.thousandSeparator):m;this.rawValue=f,this.element.value=this.formatValueForDisplay(f)}else if(this.element.value){const m=this.element.value;this.element.value=this.formatValueForDisplay(m)}}createInputElement(t,n){this.element=document.createElement("input"),this.element.setAttribute("type","text"),this.element.setAttribute("inputmode","decimal"),this.element.setAttribute("spellcheck","false"),this.element.setAttribute("autocomplete","off");const r=re(this.resolvedOptions.decimalSeparator,this.resolvedOptions.enableNegative);this.element.setAttribute("pattern",r);const{decimalMaxLength:i,decimalMinLength:s,formatOn:a,thousandSeparator:c,thousandStyle:l,decimalSeparator:o,enableCompactNotation:h,enableNegative:d,enableLeadingZeros:u,rawValueMode:S,onChange:E,value:m,defaultValue:f,type:g,inputMode:N,spellcheck:b,autocomplete:ce,...oe}=n;Object.assign(this.element,oe),m!==void 0?this.element.value=m:f!==void 0&&(this.element.defaultValue=f,this.element.value=f),t.appendChild(this.element)}setupEventListeners(){this.element.addEventListener("input",this.handleChange.bind(this)),this.element.addEventListener("keydown",this.handleKeyDown.bind(this)),this.element.addEventListener("paste",this.handlePaste.bind(this)),this.resolvedOptions.formatOn===L.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)))}getResolvedOptions(t){const n=se({thousandSeparator:t.thousandSeparator,thousandStyle:t.thousandStyle,decimalSeparator:t.decimalSeparator});return{decimalMaxLength:t.decimalMaxLength??G,decimalMinLength:t.decimalMinLength??B,formatOn:t.formatOn??Z,thousandSeparator:n.thousandSeparator,thousandStyle:n.thousandStyle,decimalSeparator:n.decimalSeparator,enableCompactNotation:t.enableCompactNotation??k,enableNegative:t.enableNegative??W,enableLeadingZeros:t.enableLeadingZeros??U,rawValueMode:t.rawValueMode??P,onChange:t.onChange}}buildFormattingOptions(){return{formatOn:this.resolvedOptions.formatOn,thousandSeparator:this.resolvedOptions.thousandSeparator,ThousandStyle:this.resolvedOptions.thousandStyle,enableCompactNotation:this.resolvedOptions.enableCompactNotation,enableNegative:this.resolvedOptions.enableNegative,enableLeadingZeros:this.resolvedOptions.enableLeadingZeros,decimalSeparator:this.resolvedOptions.decimalSeparator,decimalMinLength:this.resolvedOptions.decimalMinLength,rawValueMode:this.resolvedOptions.rawValueMode}}handleValueChange(t,n){if(this.resolvedOptions.rawValueMode&&n!==void 0&&(this.rawValue=n),this.resolvedOptions.onChange){const r=this.resolvedOptions.rawValueMode?this.rawValue:t;this.resolvedOptions.onChange(r)}}formatValueForDisplay(t){if(!t)return t;const{thousandSeparator:n,thousandStyle:r,enableLeadingZeros:i,decimalSeparator:s}=this.resolvedOptions;return n&&r!==p.None?Q(t,n,r,i,s):t}handleChange(t){const{formatted:n,raw:r}=te(t,this.resolvedOptions.decimalMaxLength,this.caretPositionBeforeChange,this.buildFormattingOptions());this.caretPositionBeforeChange=void 0,this.handleValueChange(n,r)}handleKeyDown(t){const n=t.target,{selectionStart:r,selectionEnd:i}=n,s=this.buildFormattingOptions(),a=ee(t,{formatOn:s.formatOn,thousandSeparator:s.thousandSeparator,ThousandStyle:s.ThousandStyle,decimalSeparator:s.decimalSeparator});a?this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0,endOffset:a.endOffset}:this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0}}handlePaste(t){const{formatted:n,raw:r}=ne(t,this.resolvedOptions.decimalMaxLength,this.buildFormattingOptions());this.handleValueChange(n,r);const i=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(i)}handleFocus(t){if(this.resolvedOptions.formatOn===L.Blur&&this.resolvedOptions.thousandSeparator){const n=t.target;n.value=v(n.value,this.resolvedOptions.thousandSeparator)}}handleBlur(t){const n=t.target,{thousandSeparator:r,thousandStyle:i}=this.resolvedOptions;if(r&&i!==p.None&&n.value){const s=this.formatValueForDisplay(n.value);n.value=s;const a=this.resolvedOptions.rawValueMode?v(s,r):void 0;this.handleValueChange(s,a)}}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(t){if(this.resolvedOptions.rawValueMode){const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t;this.rawValue=n,this.element.value=this.formatValueForDisplay(n)}else this.element.value=t}disable(){this.element.disabled=!0}enable(){this.element.disabled=!1}addEventListener(t,n){this.element.addEventListener(t,n)}removeEventListener(t,n){this.element.removeEventListener(t,n)}getElement(){return this.element}get value(){return this.getValue()}set value(t){this.setValue(t)}get valueAsNumber(){const t=this.getValue();if(!t)return NaN;const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t,r=this.resolvedOptions.decimalSeparator&&this.resolvedOptions.decimalSeparator!=="."?n.replace(new RegExp(w(this.resolvedOptions.decimalSeparator),"g"),"."):n;return parseFloat(r)}set valueAsNumber(t){if(isNaN(t)){this.setValue("");return}const n=t.toString();this.setValue(n)}}exports.FormatOn=L;exports.NumoraInput=ft;exports.ThousandStyle=p;exports.formatInputValue=M;exports.formatValueForDisplay=tt;exports.getNumoraPattern=re;exports.getSeparatorsFromLocale=ie;exports.handleOnChangeNumoraInput=te;exports.handleOnKeyDownNumoraInput=ee;exports.handleOnPasteNumoraInput=ne;exports.removeThousandSeparators=v;exports.resolveLocaleOptions=se;exports.validateNumoraInputOptions=ae;
|
|
1
|
+
"use strict";var ue=Object.defineProperty;var de=(e,t,n)=>t in e?ue(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var w=(e,t,n)=>de(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var m=(e=>(e.Blur="blur",e.Change="change",e))(m||{}),b=(e=>(e.None="none",e.Thousand="thousand",e.Lakh="lakh",e.Wan="wan",e))(b||{});const B=2,G=0,k=m.Blur,fe=",",Z=b.None,C=".",W=!1,U=!1,P=!1,z=!1;function y(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}const J=new Map;function O(e,t="g"){const n=`${e}:${t}`;let r=J.get(n);return r||(r=new RegExp(e,t),J.set(n,r)),r}function ge(e,t="g"){const n=y(e);return O(n,t)}const Se=/[.,]/g;function _(e){return{decimalSeparator:(e==null?void 0:e.decimalSeparator)??C,thousandSeparator:e==null?void 0:e.thousandSeparator}}function Y(e,t){const n=e.startsWith("-"),r=n?e.slice(1):e,[i="",s=""]=r.split(t);return{sign:n?"-":"",integer:i,decimal:s}}const pe=(e,t,n=C)=>{const{sign:r,integer:i,decimal:s}=Y(e,n);if(!s)return e;const a=s.slice(0,t);return`${r}${i}${n}${a}`},Ee=(e,t=C)=>{const n=e.indexOf(t);if(n===-1||n===e.length-1)return e;const r=e.slice(0,n+1),i=e.slice(n+1),a=t==="."||t===","?Se:O("[,\\."+y(t)+"]","g");return r+i.replace(a,"")},me=(e,t=0,n=C)=>{if(t<=0)return e;const{sign:r,integer:i,decimal:s}=Y(e,n);if(s.length>=t)return e;const a=s.padEnd(t,"0");return`${r}${i}${n}${a}`},$={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}},be=/^(0+)/;function Q(e,t,n=b.Thousand,r=!1,i="."){if(!e||e==="0"||e===i||e==="-"||e===`-${i}`)return e;const s=e.includes(i),a=e.startsWith("-"),c=a?e.slice(1):e,[o,l]=c.split(i);if(!o){const u=l?`${i}${l}`:c;return a?`-${u}`:u}if(r&&o.startsWith("0")&&o.length>1){const u=o.match(be);if(u){const S=u[1],p=o.slice(S.length);if(p){const E=X(p,t,n),d=S+E,f=a?"-":"";return s?l?`${f}${d}${i}${l}`:`${f}${d}${i}`:`${f}${d}`}}}const h=X(o,t,n),g=a?"-":"";return s?l?`${g}${h}${i}${l}`:`${g}${h}${i}`:`${g}${h}`}function X(e,t,n){if(e==="0"||e==="")return e;switch(n){case b.None:return e;case b.Thousand:return j(e,t,$.thousand.size);case b.Lakh:return Ne(e,t);case b.Wan:return j(e,t,$.wan.size);default:return e}}function Ne(e,t){if(e.length<=$.lakh.firstGroup)return e;const n=[],r=e.length-$.lakh.firstGroup;n.unshift(e.slice(r));for(let i=r;i>0;i-=$.lakh.restGroup)n.unshift(e.slice(Math.max(0,i-$.lakh.restGroup),i));return n.join(t)}function j(e,t,n){const r=[];for(let i=e.length;i>0;i-=n)r.unshift(e.slice(Math.max(0,i-n),i));return r.join(t)}function Le(e,t,n){return(t==null?void 0:t.formatOn)===m.Change&&t.thousandSeparator?Q(e,t.thousandSeparator,t.ThousandStyle??b.None,t.enableLeadingZeros,(n==null?void 0:n.decimalSeparator)??C):e}function D(e,t,n){let r=0;for(let i=0;i<t&&i<e.length;i++)e[i]!==n&&r++;return r}function ve(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r===t-1)return i+1;r++}return e.length}function R(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r++,r===t)return i+1;if(r>t)return i}return e.length}function V(e,t,n){return t<0||t>=e.length?!1:e[t]===n}const De=/\d/;function $e(e,t={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:i="",suffix:s=""}=t,a=new Array(e.length+1).fill(!0);if(i&&a.fill(!1,0,i.length),s){const c=e.length-s.length;a.fill(!1,c+1,e.length+1)}for(let c=0;c<e.length;c++){const o=e[c];(n&&o===n||o===r)&&(a[c]=!1,c+1<e.length&&!De.test(e[c+1])&&(a[c+1]=!1))}return a.some(c=>c)||a.fill(!0),a}function x(e,t,n,r){const i=e.length;if(t=Math.max(0,Math.min(t,i)),r==="left"){for(;t>=0&&!n[t];)t--;t===-1&&(t=n.indexOf(!0))}else if(r==="right"){for(;t<=i&&!n[t];)t++;t>i&&(t=n.lastIndexOf(!0))}else if(!n[t]){let s=t;for(;s<=i&&!n[s];)s++;let a=t;for(;a>=0&&!n[a];)a--;s<=i&&a>=0?t=t-a<s-t?a:s:s<=i?t=s:a>=0&&(t=a)}return(t===-1||t>i)&&(t=i),t}const K=/(\d+\.?\d*)\s*[kmbt]$/i,Ce=(e,t)=>e===t;function we(e,t,n,r,i,s,a=".",c={}){if(n<0)return 0;if(n>e.length||e===""||t===""||K.test(e)&&!K.test(t)&&t.length>e.length&&n>=e.length-1)return t.length;const o=t.length<e.length,l=V(e,n,r),h=e.indexOf(a),g=t.indexOf(a);if(c.isCharacterEquivalent&&e!==t){const d={thousandSeparator:r||void 0,...c},f=ye(e,t,n,c.isCharacterEquivalent||Ce,s,d);if(f!==void 0)return f}const S=new Map,p=(d,f,L)=>{const N=`${d===e?"o":"n"}:${f}`;return S.has(N)||S.set(N,D(d,f,r)),S.get(N)};if(o)return Ie(e,t,n,r,l,h,g,s,c,p);const E=Re(e,t,n,r,l,p);return c.boundary?x(t,E,c.boundary):E}function ye(e,t,n,r,i,s){const a=e.length,c=t.length;let o=0,l=0,h=c;for(let f=0;f<a;f++){let L=-1;for(let N=o;N<c;N++)if(r(e[f],t[N],{oldValue:e,newValue:t,typedRange:i,oldIndex:f,newIndex:N})){L=N;break}L!==-1&&(o=L+1,f<n?l=L+1:h===c&&/\d/.test(e[f])&&(h=L))}if(l>h)return h;const g=t.length<e.length||i!==void 0&&i.isDelete===!1;let u,S=!1,p=!1;if(g){u=l;const f=s.thousandSeparator,L=s.decimalSeparator??".",N=u<t.length?t[u]:"";f&&N===f?(n>0?e[n-1]:"")===f?(u=h,S=!0):p=!0:N===L&&(p=!0)}else u=n-l<h-n?l:h;const E=g&&!S?"left":"right";if(s.boundary)return p?u:x(t,u,s.boundary,E);const d=s.thousandSeparator;if(!p&&d&&u>=0&&u<t.length&&t[u]===d)if(g&&!S)for(;u>0&&t[u]===d;)u--;else for(;u<t.length&&t[u]===d;)u++;return u}function Ie(e,t,n,r,i,s,a,c,o={},l=D){if(i)return Me(e,t,n,r,a,l);const h=l(e,n,r),g=l(e,e.length,r),u=l(t,t.length,r),S=g-u,p=Te(e,n,r,h,S,s,c,l),E=s===-1||n<=s,d=Ae(t,p,r,S,c,E,a,l);return o.boundary?x(t,d,o.boundary):d}function Me(e,t,n,r,i,s=D){const a=n+1;if(a<e.length){const c=s(e,a,r),o=ve(t,c,r);return o<t.length&&t[o]!==r?o+1:o}return n}function Te(e,t,n,r,i,s,a,c=D){if(a){const{start:l,isDelete:h}=a;return h?c(e,l,n):Math.max(0,c(e,l,n))}return t>0&&e[t-1]===n&&i>0?r+1:r}function q(e,t,n,r,i,s,a=D){if(t>0&&t<e.length&&a(e,t,r)===n){if(e[t]===r&&s&&i>0&&t<e.length-1)return t+1;if(!s&&i>0&&t<e.length-1)return Math.min(t+1,e.length)}return t}function Ae(e,t,n,r,i,s,a,c=D){if(s&&a!==-1){const l=e.substring(0,a),h=c(l,l.length,n);if(t<=h){const g=R(l,t,n);return q(l,g,t,n,r,i,c)}}const o=R(e,t,n);return q(e,o,t,n,r,i,c)}function Re(e,t,n,r,i,s=D){const a=n>=e.length,c=s(e,n,r),o=s(e,e.length,r),l=s(t,t.length,r);if(a||c===o)return t.length;const h=l-o;let g=c;h>0&&!a&&c<o&&(g=c+1);const u=R(t,g,r);return i&&!V(t,u,r)?Math.max(0,u-1):u}function Oe(e,t,n){const{selectionStart:r,selectionEnd:i,endOffset:s=0}=e;if(r!==i){const h=i-r;return{start:r,end:i,deletedLength:h,isDelete:!1}}if(s>0){const h=s;return{start:r,end:r+h,deletedLength:h,isDelete:!0}}const c=t.length,o=n.length,l=c-o;if(!(l<=0))return{start:r,end:r+l,deletedLength:l,isDelete:!1}}function _e(e,t){if(e===t)return;let n=0;for(;n<e.length&&n<t.length&&e[n]===t[n];)n++;let r=e.length-1,i=t.length-1;for(;r>=n&&i>=n&&e[r]===t[i];)r--,i--;const s=r-n+1,a=i-n+1;if(!(s===0&&a===0))return{start:n,end:r+1,deletedLength:s,isDelete:s>a}}function H(e,t){if(e.value=e.value,e===null)return!1;if(e.createTextRange){const n=e.createTextRange();return n.move("character",t),n.select(),!0}return e.selectionStart!==null||e.selectionStart===0?(e.focus(),e.setSelectionRange(t,t),!0):(e.focus(),!1)}function xe(e,t,n){return e.selectionStart===0&&e.selectionEnd===e.value.length?null:(H(e,t),setTimeout(()=>{e.value===n&&e.selectionStart!==t&&H(e,t)},0))}function Be(e){return Math.max(e.selectionStart,e.selectionEnd)}function Ge(e,t,n){if((n==null?void 0:n.formatOn)!==m.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:i,value:s}=t;if(r===null||i===null||r!==i)return;const{key:a}=e,c=n.thousandSeparator;a==="Backspace"&&r>0&&s[r-1]===c&&t.setSelectionRange(r-1,r-1),a==="Delete"&&s[r]===c&&t.setSelectionRange(r+1,r+1)}function F(e,t,n,r,i,s,a){if(!i)return;const{selectionStart:c=0,selectionEnd:o=0,endOffset:l=0}=i;let h=Oe({selectionStart:c,selectionEnd:o,endOffset:l},t,n);if(h||(h=_e(t,n)),!h)return;const g=$e(n,{thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator}),u={thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator,isCharacterEquivalent:(d,f)=>{const L=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator;return L&&d===L?!1:d===f},boundary:g},S=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator??",",p=(a==null?void 0:a.ThousandStyle)??b.None,E=we(t,n,r,S,p,h,s.decimalSeparator,u);xe(e,E,n)}const ke=(e,t=!1,n=".")=>{const r=y(n),i=O(`[^0-9${r}]`,"g");if(!t)return e.replace(i,"");const s=e.startsWith("-"),a=e.replace(i,"");return s&&(a.length>0||e==="-")?"-"+a:a},Ze=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g,ee=/^0+$/,T=/\.?0+$/;function We(e){return!e.includes("e")&&!e.includes("E")?e:e.replace(Ze,(t,n,r)=>{const i=parseInt(r,10);if(i===0)return n;const s=n.startsWith("-"),a=s?n.slice(1):n,[c,o=""]=a.split("."),l=i>0?Ue(c,o,i):Pe(c,o,Math.abs(i));return s?"-"+l:l})}function Ue(e,t,n){const r=e+t;if(r==="0"||ee.test(r))return"0";const i=t.length;if(n<=i){const a=r.slice(0,e.length+n),c=r.slice(e.length+n);return c?`${a}.${c}`:a}const s=n-i;return r+"0".repeat(s)}function Pe(e,t,n){const r=e+t;if(r==="0"||ee.test(r))return"0";const s=e.length-n;if(s<=0){const a=Math.abs(s),c=`0.${"0".repeat(a)}${r}`;return A(c)}if(s<e.length){const a=r.slice(0,s),c=r.slice(s),o=`${a}.${c}`;return A(o)}return A(r)}function A(e){if(!e.includes("."))return e;if(e==="0.")return"0";if(e.startsWith("0.")){const t=e.replace(T,"");return t==="0"?"0":t||"0"}if(e.startsWith("-0.")){const t=e.replace(T,"");return t==="-0"||t==="0"?"0":t||"0"}return e.replace(T,"")||e}const ze=/(\d+\.?\d*)\s*([kmbt])/gi,Je=/^0+/,Xe=/^(-?)0+([1-9])/,je=/^-?0+$/,Ke=/\.?0+$/,qe={k:3,m:6,b:9,t:12};function He(e){return e.replace(ze,(t,n,r)=>{const i=r.toLowerCase(),s=qe[i];if(!s)return t;const[a,c=""]=n.split("."),o=a.replace(Je,"")||"0";let l;if(c.length===0)l=o+"0".repeat(s);else if(c.length<=s){const h=s-c.length;l=o+c+"0".repeat(h)}else{const h=c.slice(0,s),g=c.slice(s);l=o+h+"."+g}return l=l.replace(Xe,"$1$2"),je.test(l)&&(l="0"),l.includes(".")&&(l=l.replace(Ke,"")),l})}function Ye(e){if(!e||e==="0"||e==="-0"||e==="-"||e===".")return e;const t=e.startsWith("-"),n=t?e.slice(1):e;if(n===".")return e;if(n.includes(".")){const[i,s]=n.split(".");if(i){const c=(i.replace(/^0+/,"")||"0")+"."+s;return t?"-"+c:c}return e}if(n.startsWith("0")){const i=n.replace(/^0+/,"")||"0";return t?"-"+i:i}return e}function Qe(e){return e.replace(/[\s\u200B]/g,"")}function v(e,t){const n=ge(t);return e.replace(n,"")}const Ve=(e,t)=>{let n=Qe(e);return t!=null&&t.thousandSeparator&&(n=v(n,t.thousandSeparator)),t!=null&&t.enableCompactNotation&&(n=He(n)),n=We(n),n=ke(n,t==null?void 0:t.enableNegative,t==null?void 0:t.decimalSeparator),n=Ee(n,t==null?void 0:t.decimalSeparator),t!=null&&t.enableLeadingZeros||(n=Ye(n)),n};function Fe(e,t,n){return{enableCompactNotation:e==null?void 0:e.enableCompactNotation,enableNegative:e==null?void 0:e.enableNegative,enableLeadingZeros:e==null?void 0:e.enableLeadingZeros,decimalSeparator:t.decimalSeparator,thousandSeparator:n?t.thousandSeparator:void 0}}function I(e,t,n,r){const i=_(n),s=r??(n==null?void 0:n.formatOn)===m.Change,a=Ve(e,Fe(n,i,s)),c=pe(a,t,i.decimalSeparator),o=(n==null?void 0:n.decimalMinLength)??0,l=me(c,o,i.decimalSeparator);return{formatted:Le(l,n,i),raw:l}}function et(e,t,n){const r=!!(n!=null&&n.thousandSeparator);return I(e,t,n,r)}function te(e,t,n){if(e.inputType==="insertFromPaste"||e.inputType==="insertFromDrop")return null;const r=e.target,i=r.value,s=r.selectionStart??0,a=r.selectionEnd??0,c=_(n);let o=e.data??"";if(e.inputType==="insertText"&&(e.data===","||e.data===".")){const d=c.decimalSeparator;if((i.slice(0,s)+i.slice(a)).includes(d))return e.preventDefault(),null;o=d}let l,h;switch(e.inputType){case"insertText":{l=i.slice(0,s)+o+i.slice(a),h=s+o.length;break}case"deleteContentBackward":{if(s!==a)l=i.slice(0,s)+i.slice(a),h=s;else{const d=Math.max(0,s-1);l=i.slice(0,d)+i.slice(s),h=d}break}case"deleteContentForward":{s!==a?(l=i.slice(0,s)+i.slice(a),h=s):(l=i.slice(0,s)+i.slice(s+1),h=s);break}case"deleteByCut":case"deleteByDrag":{l=i.slice(0,s)+i.slice(a),h=s;break}default:return null}e.preventDefault();const g=(n==null?void 0:n.formatOn)===m.Change,{formatted:u,raw:S}=I(l,t,n,g);r.setRangeText(u,0,i.length,"end");const p=e.inputType==="deleteContentForward"?1:0;return l!==u?F(r,l,u,h,{selectionStart:s,selectionEnd:a,endOffset:p},c,n):r.setSelectionRange(h,h),{formatted:u,raw:S}}function tt(e,t,n){if(e==="Backspace"||e==="Delete")return e==="Delete"&&t===n?{endOffset:1}:{endOffset:0}}function ne(e,t){const n=e.target;return Ge(e,n,t),tt(e.key,n.selectionStart,n.selectionEnd)}function re(e,t,n,r){const i=e.target,s=i.value,a=Be(i),c=_(r),o=(r==null?void 0:r.formatOn)===m.Change,{formatted:l,raw:h}=I(s,t,r,o);return i.value=l,s!==l&&F(i,s,l,a,n,c,r),{formatted:l,raw:h}}function nt(e,t,n,r){const i=r-n;return e+t+i}function ie(e,t,n){var u;e.preventDefault();const r=e.target,{value:i,selectionStart:s,selectionEnd:a}=r,c=((u=e.clipboardData)==null?void 0:u.getData("text/plain"))||"",o=i.slice(0,s||0)+c+i.slice(a||0),{formatted:l,raw:h}=I(o,t,n,!0);r.value=l;const g=nt(s||0,c.length,o.length,l.length);return r.setSelectionRange(g,g),{formatted:l,raw:h}}function se(e,t){const n=y(e);return t?`^-?[0-9]*[${n}]?[0-9]*$`:`^[0-9]*[${n}]?[0-9]*$`}function ae(e){var t,n;try{const i=new Intl.NumberFormat(e).formatToParts(123456789e-2);return{decimalSeparator:((t=i.find(s=>s.type==="decimal"))==null?void 0:t.value)??".",thousandSeparator:((n=i.find(s=>s.type==="group"))==null?void 0:n.value)??","}}catch{return{thousandSeparator:",",decimalSeparator:"."}}}function ce(e,t){if(!e)return t;const r=ae(e===!0?void 0:e);return{thousandSeparator:t.thousandSeparator??r.thousandSeparator,decimalSeparator:t.decimalSeparator??r.decimalSeparator}}function le(e){rt(e.decimalMaxLength),it(e.decimalMinLength),st(e.decimalMinLength,e.decimalMaxLength),at(e.formatOn),ct(e.thousandSeparator),lt(e.thousandStyle),ot(e.decimalSeparator),ht(e.thousandSeparator,e.decimalSeparator),M("enableCompactNotation",e.enableCompactNotation),M("enableNegative",e.enableNegative),M("enableLeadingZeros",e.enableLeadingZeros),M("rawValueMode",e.rawValueMode),ut(e.onChange)}function rt(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMaxLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMaxLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMaxLength must be non-negative. Received: ${e}`)}}function it(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMinLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMinLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMinLength must be non-negative. Received: ${e}`)}}function st(e,t){if(!(e===void 0||t===void 0)&&e>t)throw new Error(`decimalMinLength (${e}) cannot be greater than decimalMaxLength (${t}).`)}function at(e){if(e!==void 0&&e!==m.Blur&&e!==m.Change)throw new Error(`formatOn must be either ${m.Blur} or ${m.Change}. Received: ${JSON.stringify(e)}`)}function ct(e){if(e!==void 0){if(typeof e!="string")throw new Error(`thousandSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`thousandSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`thousandSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function lt(e){if(e!==void 0&&!Object.values(b).includes(e))throw new Error(`ThousandStyle must be one of: ${Object.values(b).map(t=>`'${t}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function ot(e){if(e!==void 0){if(typeof e!="string")throw new Error(`decimalSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`decimalSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`decimalSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function ht(e,t){if(!(e===void 0||t===void 0)&&e===t)throw new Error(`Decimal separator can't be same as thousand separator. thousandSeparator: ${e}, decimalSeparator: ${t}`)}function M(e,t){if(t!==void 0&&typeof t!="boolean")throw new Error(`${e} must be a boolean. Received: ${typeof t} (${JSON.stringify(t)})`)}function ut(e){if(e!==void 0&&typeof e!="function")throw new Error(`onChange must be a function or undefined. Received: ${typeof e} (${JSON.stringify(e)})`)}class dt{constructor(t,{decimalMaxLength:n=B,decimalMinLength:r=G,formatOn:i=k,thousandSeparator:s,thousandStyle:a=Z,decimalSeparator:c,locale:o,enableCompactNotation:l=W,enableNegative:h=U,enableLeadingZeros:g=P,rawValueMode:u=z,onChange:S,...p}){w(this,"element");w(this,"resolvedOptions");w(this,"rawValue","");w(this,"caretPositionBeforeChange");le({decimalMaxLength:n,decimalMinLength:r,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,enableCompactNotation:l,enableNegative:h,enableLeadingZeros:g,rawValueMode:u,onChange:S});const E={decimalMaxLength:n,decimalMinLength:r,onChange:S,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,locale:o,enableCompactNotation:l,enableNegative:h,enableLeadingZeros:g,rawValueMode:u,...p};if(this.resolvedOptions=this.getResolvedOptions(E),this.createInputElement(t,E),this.setupEventListeners(),this.resolvedOptions.rawValueMode&&this.element.value){const d=this.element.value,f=this.resolvedOptions.thousandSeparator?v(d,this.resolvedOptions.thousandSeparator):d;this.rawValue=f,this.element.value=this.formatValueForDisplay(f)}else if(this.element.value){const d=this.element.value;this.element.value=this.formatValueForDisplay(d)}}createInputElement(t,n){this.element=document.createElement("input"),this.element.setAttribute("type","text"),this.element.setAttribute("inputmode","decimal"),this.element.setAttribute("spellcheck","false"),this.element.setAttribute("autocomplete","off");const r=se(this.resolvedOptions.decimalSeparator,this.resolvedOptions.enableNegative);this.element.setAttribute("pattern",r);const{decimalMaxLength:i,decimalMinLength:s,formatOn:a,thousandSeparator:c,thousandStyle:o,decimalSeparator:l,locale:h,enableCompactNotation:g,enableNegative:u,enableLeadingZeros:S,rawValueMode:p,onChange:E,value:d,defaultValue:f,type:L,inputMode:N,spellcheck:oe,autocomplete:ft,...he}=n;Object.assign(this.element,he),d!==void 0?this.element.value=d:f!==void 0&&(this.element.defaultValue=f,this.element.value=f),t.appendChild(this.element)}setupEventListeners(){this.element.addEventListener("beforeinput",this.handleBeforeInput.bind(this)),this.element.addEventListener("input",this.handleChange.bind(this)),this.element.addEventListener("keydown",this.handleKeyDown.bind(this)),this.element.addEventListener("paste",this.handlePaste.bind(this)),this.resolvedOptions.formatOn===m.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)))}getResolvedOptions(t){const n=ce(t.locale,{thousandSeparator:t.thousandSeparator,decimalSeparator:t.decimalSeparator});return{decimalMaxLength:t.decimalMaxLength??B,decimalMinLength:t.decimalMinLength??G,formatOn:t.formatOn??k,thousandSeparator:n.thousandSeparator??fe,thousandStyle:t.thousandStyle??Z,decimalSeparator:n.decimalSeparator??C,enableCompactNotation:t.enableCompactNotation??W,enableNegative:t.enableNegative??U,enableLeadingZeros:t.enableLeadingZeros??P,rawValueMode:t.rawValueMode??z,onChange:t.onChange}}buildFormattingOptions(){return{formatOn:this.resolvedOptions.formatOn,thousandSeparator:this.resolvedOptions.thousandSeparator,ThousandStyle:this.resolvedOptions.thousandStyle,enableCompactNotation:this.resolvedOptions.enableCompactNotation,enableNegative:this.resolvedOptions.enableNegative,enableLeadingZeros:this.resolvedOptions.enableLeadingZeros,decimalSeparator:this.resolvedOptions.decimalSeparator,decimalMinLength:this.resolvedOptions.decimalMinLength,rawValueMode:this.resolvedOptions.rawValueMode}}handleValueChange(t,n){if(this.resolvedOptions.rawValueMode&&n!==void 0&&(this.rawValue=n),this.resolvedOptions.onChange){const r=this.resolvedOptions.rawValueMode?this.rawValue:t;this.resolvedOptions.onChange(r)}}formatValueForDisplay(t){if(!t)return t;const{thousandSeparator:n,thousandStyle:r,enableLeadingZeros:i,decimalSeparator:s}=this.resolvedOptions;return n&&r!==b.None?Q(t,n,r,i,s):t}handleBeforeInput(t){te(t,this.resolvedOptions.decimalMaxLength,this.buildFormattingOptions())}handleChange(t){const{formatted:n,raw:r}=re(t,this.resolvedOptions.decimalMaxLength,this.caretPositionBeforeChange,this.buildFormattingOptions());this.caretPositionBeforeChange=void 0,this.handleValueChange(n,r)}handleKeyDown(t){const n=t.target,{selectionStart:r,selectionEnd:i}=n,s=this.buildFormattingOptions(),a=ne(t,{formatOn:s.formatOn,thousandSeparator:s.thousandSeparator,ThousandStyle:s.ThousandStyle,decimalSeparator:s.decimalSeparator});a?this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0,endOffset:a.endOffset}:this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0}}handlePaste(t){const{formatted:n,raw:r}=ie(t,this.resolvedOptions.decimalMaxLength,this.buildFormattingOptions());this.handleValueChange(n,r);const i=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(i)}handleFocus(t){if(this.resolvedOptions.formatOn===m.Blur&&this.resolvedOptions.thousandSeparator){const n=t.target;n.value=v(n.value,this.resolvedOptions.thousandSeparator)}}handleBlur(t){const n=t.target,{thousandSeparator:r,thousandStyle:i}=this.resolvedOptions;if(r&&i!==b.None&&n.value){const s=this.formatValueForDisplay(n.value);n.value=s;const a=this.resolvedOptions.rawValueMode?v(s,r):void 0;this.handleValueChange(s,a)}}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(t){if(this.resolvedOptions.rawValueMode){const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t;this.rawValue=n,this.element.value=this.formatValueForDisplay(n)}else this.element.value=t}disable(){this.element.disabled=!0}enable(){this.element.disabled=!1}addEventListener(t,n){this.element.addEventListener(t,n)}removeEventListener(t,n){this.element.removeEventListener(t,n)}getElement(){return this.element}get value(){return this.getValue()}set value(t){this.setValue(t)}get valueAsNumber(){const t=this.getValue();if(!t)return NaN;const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t,r=this.resolvedOptions.decimalSeparator&&this.resolvedOptions.decimalSeparator!=="."?n.replace(new RegExp(y(this.resolvedOptions.decimalSeparator),"g"),"."):n;return parseFloat(r)}set valueAsNumber(t){if(isNaN(t)){this.setValue("");return}const n=t.toString();this.setValue(n)}}exports.FormatOn=m;exports.NumoraInput=dt;exports.ThousandStyle=b;exports.applyLocale=ce;exports.formatInputValue=I;exports.formatValueForDisplay=et;exports.getNumoraPattern=se;exports.getSeparatorsFromLocale=ae;exports.handleOnBeforeInputNumoraInput=te;exports.handleOnChangeNumoraInput=re;exports.handleOnKeyDownNumoraInput=ne;exports.handleOnPasteNumoraInput=ie;exports.removeThousandSeparators=v;exports.validateNumoraInputOptions=le;
|