numora 2.0.4 → 3.0.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 +67 -0
- package/dist/config.d.ts +11 -0
- package/dist/features/compact-notation.d.ts +17 -0
- package/dist/features/decimals.d.ts +21 -0
- package/dist/features/formatting/caret-position-utils.d.ts +54 -0
- package/dist/features/formatting/change-detection.d.ts +40 -0
- package/dist/features/formatting/character-equivalence.d.ts +9 -0
- package/dist/features/formatting/constants.d.ts +29 -0
- package/dist/features/formatting/cursor-boundary.d.ts +39 -0
- package/dist/features/formatting/cursor-position.d.ts +50 -0
- package/dist/features/formatting/digit-counting.d.ts +61 -0
- package/dist/features/formatting/index.d.ts +20 -0
- package/dist/features/formatting/large-number.d.ts +39 -0
- package/dist/features/formatting/numeric-formatting-utils.d.ts +24 -0
- package/dist/features/formatting/percent.d.ts +45 -0
- package/dist/features/formatting/subscript-notation.d.ts +20 -0
- package/dist/features/formatting/thousand-grouping.d.ts +34 -0
- package/dist/features/leading-zeros.d.ts +18 -0
- package/dist/features/mobile-keyboard-filtering.d.ts +18 -0
- package/dist/features/non-numeric-characters.d.ts +9 -0
- package/dist/features/sanitization.d.ts +44 -0
- package/dist/features/scientific-notation.d.ts +9 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +386 -388
- package/dist/types.d.ts +34 -0
- package/dist/utils/escape-reg-exp.d.ts +16 -0
- package/dist/utils/event-handlers.d.ts +31 -0
- package/dist/utils/format-utils.d.ts +28 -0
- package/dist/utils/input-pattern.d.ts +5 -0
- package/dist/validation.d.ts +20 -0
- package/package.json +2 -2
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { FormattingOptions, Separators } from '@/types';
|
|
2
|
+
/**
|
|
3
|
+
* Removes all occurrences of thousand separator from a string.
|
|
4
|
+
* Escapes special regex characters in the separator to ensure safe pattern matching.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The string to remove separators from
|
|
7
|
+
* @param thousandSeparator - The thousand separator character to remove
|
|
8
|
+
* @returns The string with all thousand separators removed
|
|
9
|
+
*/
|
|
10
|
+
export declare function removeThousandSeparators(value: string, thousandSeparator: string): string;
|
|
11
|
+
export interface SanitizationOptions {
|
|
12
|
+
enableCompactNotation?: boolean;
|
|
13
|
+
enableNegative?: boolean;
|
|
14
|
+
enableLeadingZeros?: boolean;
|
|
15
|
+
decimalSeparator?: string;
|
|
16
|
+
thousandSeparator?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Sanitizes numeric input by:
|
|
20
|
+
* 0. Filter mobile keyboard artifacts (non-breaking spaces, Unicode whitespace)
|
|
21
|
+
* 1. Remove thousand separators (formatting, not data)
|
|
22
|
+
* 2. (Optional) Expanding compact notation (e.g., 1k → 1000)
|
|
23
|
+
* 3. Expanding scientific notation (e.g., 1.5e-5 → 0.000015)
|
|
24
|
+
* 4. Removing non-numeric characters
|
|
25
|
+
* 5. Removing extra decimal points
|
|
26
|
+
* 6. (Optional) Removing leading zeros
|
|
27
|
+
*
|
|
28
|
+
* Note: Decimal separator conversion (comma ↔ dot) is handled in the keydown event
|
|
29
|
+
* (handleDecimalSeparatorKey), not here, to avoid converting thousand separators.
|
|
30
|
+
*
|
|
31
|
+
* @param value - The string value to sanitize
|
|
32
|
+
* @param options - Optional sanitization configuration
|
|
33
|
+
* @returns The sanitized numeric string
|
|
34
|
+
*/
|
|
35
|
+
export declare const sanitizeNumoraInput: (value: string, options?: SanitizationOptions) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Builds sanitization options from formatting options and separators.
|
|
38
|
+
*
|
|
39
|
+
* @param formattingOptions - Optional formatting options
|
|
40
|
+
* @param separators - Separator configuration
|
|
41
|
+
* @param shouldRemoveThousandSeparators - Whether to remove thousand separators
|
|
42
|
+
* @returns Sanitization options
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildSanitizationOptions(formattingOptions: FormattingOptions | undefined, separators: Separators, shouldRemoveThousandSeparators: boolean): SanitizationOptions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expands scientific notation to decimal notation using string manipulation only.
|
|
3
|
+
* Handles formats like: 1.5e-7, 2e+5, 1.23e-4, etc.
|
|
4
|
+
* Finds and expands scientific notation anywhere in the string.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The string value that may contain scientific notation
|
|
7
|
+
* @returns The expanded decimal string, or original value if not scientific notation
|
|
8
|
+
*/
|
|
9
|
+
export declare function expandScientificNotation(value: string): string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from './NumoraInput';
|
|
2
|
+
export { ThousandStyle, FormatOn } from './types';
|
|
3
|
+
export { handleOnChangeNumoraInput, handleOnPasteNumoraInput, handleOnKeyDownNumoraInput, } from './utils/event-handlers';
|
|
4
|
+
export { formatValueForDisplay, formatInputValue, } from './utils/format-utils';
|
|
5
|
+
export type { FormattingOptions, CaretPositionInfo } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Y=Object.defineProperty;var V=(e,t,n)=>t in e?Y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var L=(e,t,n)=>V(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var b=(e=>(e.Blur="blur",e.Change="change",e))(b||{}),S=(e=>(e.None="none",e.Thousand="thousand",e.Lakh="lakh",e.Wan="wan",e))(S||{});const x=2,O=0,R=b.Blur,_=",",P=S.None,E=".",F=!1,B=!1,W=!1,k=!1;function w(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function T(e){return{decimalSeparator:(e==null?void 0:e.decimalSeparator)??E,thousandSeparator:e==null?void 0:e.thousandSeparator}}function ee(e,t){const n=e.target;return n?n.value.includes(t):!1}function te(e,t,n,r){const{selectionStart:i,selectionEnd:a,value:s}=t,{key:o}=e,c=n==null?void 0:n.ThousandStyle;if(c!==S.None&&c!==void 0||o!==","&&o!==".")return!1;if(ee(e,r))return!0;if(o!==r){const l=i??0,h=a??l,d=s.slice(0,l)+r+s.slice(h);t.value=d;const u=l+1;return t.setSelectionRange(u,u),!0}return!1}const ne=(e,t,n=E)=>{const[r,i]=e.split(n);return i?`${r}${n}${i.slice(0,t)}`:e},re=(e,t=E)=>{const n=w(t),r=new RegExp(`(${n}.*?)${n}`,"g");return e.replace(r,`$1${t}`)},se=(e,t=0,n=E)=>{if(t===0)return e;if(!e||e==="0"||e===n||e==="-"||e===`-${n}`)return e==="-"||e===`-${n}`?`-${n}${"0".repeat(t)}`:e===n?`${n}${"0".repeat(t)}`:`${e}${n}${"0".repeat(t)}`;const r=e.includes(n),i=e.startsWith("-"),a=i?e.slice(1):e,[s,o=""]=a.split(n);if(!r){const c="0".repeat(t);return i?`-${s}${n}${c}`:`${s}${n}${c}`}if(o.length<t){const c=t-o.length,l=o+"0".repeat(c);return i?`-${s}${n}${l}`:`${s}${n}${l}`}return e},N={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}};function G(e,t,n,r=!1,i="."){if(!e||e==="0"||e===i||e==="-"||e===`-${i}`)return e;const a=e.includes(i),s=e.startsWith("-"),o=s?e.slice(1):e,[c,l]=o.split(i);if(!c){const u=l?`${i}${l}`:o;return s?`-${u}`:u}if(r&&c.startsWith("0")&&c.length>1){const u=c.match(/^(0+)/);if(u){const g=u[1],f=c.slice(g.length);if(f){const p=Z(f,t,n),$=g+p,D=s?"-":"";return a?l?`${D}${$}${i}${l}`:`${D}${$}${i}`:`${D}${$}`}}}const h=Z(c,t,n),d=s?"-":"";return a?l?`${d}${h}${i}${l}`:`${d}${h}${i}`:`${d}${h}`}function Z(e,t,n){if(e==="0"||e==="")return e;switch(n){case S.None:return e;case S.Thousand:return ie(e,t);case S.Lakh:return ae(e,t);case S.Wan:return oe(e,t);default:return e}}function ie(e,t){return U(e,t,N.thousand.size)}function ae(e,t){if(e.length<=N.lakh.firstGroup)return e;const n=e.split("").reverse(),r=[],i=n.slice(0,N.lakh.firstGroup).reverse().join("");r.push(i);for(let a=N.lakh.firstGroup;a<n.length;a+=N.lakh.restGroup)r.push(n.slice(a,a+N.lakh.restGroup).reverse().join(""));return r.reverse().join(t)}function oe(e,t){return U(e,t,N.wan.size)}function U(e,t,n){const r=e.split("").reverse(),i=[];for(let a=0;a<r.length;a+=n)i.push(r.slice(a,a+n).reverse().join(""));return i.reverse().join(t)}function ce(e,t,n){return(t==null?void 0:t.formatOn)==="change"&&t.thousandSeparator?G(e,t.thousandSeparator,t.ThousandStyle??S.None,t.enableLeadingZeros,(n==null?void 0:n.decimalSeparator)??E):e}function v(e,t,n,r="."){let i=0;for(let a=0;a<t&&a<e.length;a++){const s=e[a];s!==n&&s!==r&&i++}return i}function le(e,t,n,r="."){if(t===0)return 0;let i=0;for(let a=0;a<e.length;a++){const s=e[a];if(s!==n&&s!==r){if(i===t-1)return a+1;i++}}return e.length}function A(e,t,n,r="."){if(t===0)return 0;let i=0;for(let a=0;a<e.length;a++){const s=e[a];if(s!==n&&s!==r){if(i++,i===t)return a+1;if(i>t)return a}}return e.length}function j(e,t,n){return t<0||t>=e.length?!1:e[t]===n}function he(e,t={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:i="",suffix:a=""}=t,s=Array.from({length:e.length+1}).map(()=>!0);if(i&&s.fill(!1,0,i.length),a){const o=e.length-a.length;s.fill(!1,o+1,e.length+1)}for(let o=0;o<e.length;o++){const c=e[o];(n&&c===n||c===r)&&(s[o]=!1,o+1<e.length&&!/\d/.test(e[o+1])&&(s[o+1]=!1))}return s.some(o=>o)||s.fill(!0),s}function I(e,t,n,r){const i=e.length;if(t=Math.max(0,Math.min(t,i)),!n[t]){let a=t;for(;a<=i&&!n[a];)a++;let s=t;for(;s>=0&&!n[s];)s--;a<=i&&s>=0?t=t-s<a-t?s:a:a<=i?t=a:s>=0&&(t=s)}return(t===-1||t>i)&&(t=i),t}const J=(e,t)=>e===t;function ue(e,t,n,r,i,a,s=".",o={}){if(n<0)return 0;if(n>e.length||e===""||t==="")return t.length;const c=/(\d+\.?\d*)\s*[kmbMTOQaqiSxsxSpOoNn]$/i;if(c.test(e)&&!c.test(t)&&t.length>e.length&&n>=e.length-1)return t.length;const l=t.length<e.length,h=j(e,n,r),d=e.indexOf(s),u=t.indexOf(s);if(o.isCharacterEquivalent&&e!==t){const p=de(e,t,n,o.isCharacterEquivalent||J,a,o);if(p!==void 0)return p}if(l)return fe(e,t,n,r,h,d,u,a,s,o);const f=be(e,t,n,r,h,s);return o.boundary?I(t,f,o.boundary):f}function de(e,t,n,r,i,a){const s=e.length,o=t.length,c={},l=new Array(s);for(let f=0;f<s;f++){l[f]=-1;for(let p=0;p<o;p++){if(c[p])continue;if(r(e[f],t[p],{oldValue:e,newValue:t,typedRange:i,oldIndex:f,newIndex:p})){l[f]=p,c[p]=!0;break}}}let h=n;for(;h<s&&(l[h]===-1||!/\d/.test(e[h]));)h++;const d=h===s||l[h]===-1?o:l[h];for(h=n-1;h>=0&&l[h]===-1;)h--;const u=h===-1||l[h]===-1?0:l[h]+1;if(u>d)return d;const g=n-u<d-n?u:d;return a.boundary?I(t,g,a.boundary):g}function fe(e,t,n,r,i,a,s,o,c,l={}){if(i)return ge(e,t,n,r,s,c);const h=v(e,n,r,c),d=v(e,e.length,r,c),u=v(t,t.length,r,c),g=d-u,f=pe(e,n,r,h,g,a,o,c),p=a===-1||n<=a,$=Se(t,f,r,g,o,c,p,s);return l.boundary?I(t,$,l.boundary):$}function ge(e,t,n,r,i,a){const s=n+1;if(s<e.length){const o=v(e,s,r,a),c=le(t,o,r,a);return c<t.length&&t[c]!==r?c+1:c}return n}function pe(e,t,n,r,i,a,s,o){if(s){const{start:l,isDelete:h}=s;return h?v(e,l,n,o):Math.max(0,v(e,l,n,o))}return t>0&&e[t-1]===n&&i>0?r+1:r}function Se(e,t,n,r,i,a,s,o){if(s&&o!==-1){const l=e.substring(0,o),h=v(l,l.length,n,a);if(t<=h){const d=A(l,t,n,a);if(d>0&&d<l.length&&v(l,d,n,a)===t){if(l[d]===n&&i&&r>0&&d<l.length-1)return d+1;if(!i&&r>0&&d<l.length-1)return Math.min(d+1,l.length)}return d}}const c=A(e,t,n,a);if(c>0&&c<e.length&&v(e,c,n,a)===t){if(e[c]===n&&i&&r>0&&c<e.length-1)return c+1;if(!i&&r>0&&c<e.length-1)return Math.min(c+1,e.length)}return c}function be(e,t,n,r,i,a){const s=n>=e.length,o=v(e,n,r,a),c=v(e,e.length,r,a),l=v(t,t.length,r,a);if(s||o===c)return t.length;const h=l-c;let d=o;h>0&&!s&&o<c&&(d=o+1);const u=A(t,d,r,a);return i&&!j(t,u,r)?Math.max(0,u-1):u}function ve(e,t,n){const{selectionStart:r,selectionEnd:i,endOffset:a=0}=e;if(r!==i){const h=i-r;return{start:r,end:i,deletedLength:h,isDelete:!1}}if(a>0){const h=a;return{start:r,end:r+h,deletedLength:h,isDelete:!0}}const o=t.length,c=n.length,l=o-c;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 a=r-n+1,s=i-n+1;if(!(a===0&&s===0))return{start:n,end:r+1,deletedLength:a,isDelete:a>s}}function z(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 Ee(e,t,n){return e.selectionStart===0&&e.selectionEnd===e.value.length?null:(z(e,t),setTimeout(()=>{e.value===n&&e.selectionStart!==t&&z(e,t)},0))}function Ne(e){return Math.max(e.selectionStart,e.selectionEnd)}function Le(e,t,n){if((n==null?void 0:n.formatOn)!==b.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:i,value:a}=t;if(r===null||i===null||r!==i)return;const{key:s}=e,o=n.thousandSeparator;s==="Backspace"&&r>0&&a[r-1]===o&&t.setSelectionRange(r-1,r-1),s==="Delete"&&a[r]===o&&t.setSelectionRange(r+1,r+1)}function me(e,t,n,r,i,a,s){if(!i)return;const{selectionStart:o=0,selectionEnd:c=0,endOffset:l=0}=i;let h=ve({selectionStart:o,selectionEnd:c,endOffset:l},t,n);if(h||(h=$e(t,n)),!h)return;const d=he(n,{thousandSeparator:(s==null?void 0:s.thousandSeparator)??a.thousandSeparator,decimalSeparator:a.decimalSeparator}),u={thousandSeparator:(s==null?void 0:s.thousandSeparator)??a.thousandSeparator,isCharacterEquivalent:J,boundary:d},g=(s==null?void 0:s.thousandSeparator)??a.thousandSeparator??",",f=(s==null?void 0:s.ThousandStyle)??S.None,p=ue(t,n,r,g,f,h,a.decimalSeparator,u);Ee(e,p,n)}const we=(e,t=!1,n=".")=>{const r=w(n),i=new RegExp(`[^0-9${r}]`,"g");if(!t)return e.replace(i,"");const a=e.startsWith("-"),s=e.replace(i,"");if(a){if(s.length>0)return"-"+s;if(e==="-")return"-"}return s};function De(e){const t=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g;let n=e,r;const i=[];for(;(r=t.exec(e))!==null;){const a=r[0],s=r[1],o=parseInt(r[2],10);let c;if(o===0)c=s;else{const l=s.startsWith("-"),h=l?s.slice(1):s,[d,u=""]=h.split(".");o>0?c=Me(d,u,o):c=ye(d,u,Math.abs(o)),l&&(c="-"+c)}i.push({full:a,expanded:c})}for(const{full:a,expanded:s}of i)n=n.replace(a,s);return n}function Me(e,t,n){const r=e+t;if(r==="0"||r.match(/^0+$/))return"0";const i=t.length;if(n<=i){const s=r.slice(0,e.length+n),o=r.slice(e.length+n);return o?`${s}.${o}`:s}const a=n-i;return r+"0".repeat(a)}function ye(e,t,n){const r=e+t;if(r==="0"||r.match(/^0+$/))return"0";const s=e.length-n;if(s<=0){const o=Math.abs(s),c=`0.${"0".repeat(o)}${r}`;return C(c)}if(s<e.length){const o=r.slice(0,s),c=r.slice(s),l=`${o}.${c}`;return C(l)}return C(r)}function C(e){if(!e.includes("."))return e;if(e==="0"||e==="0.")return"0";if(e.startsWith("0.")){const t=e.replace(/\.?0+$/,"");return t==="0"?"0":t||"0"}if(e.startsWith("-0.")){const t=e.replace(/\.?0+$/,"");return t==="-0"||t==="0"?"0":t||"0"}return e.replace(/\.?0+$/,"")||e}function Ce(e){const t=/(\d+\.?\d*)\s*(Qa|Qi|Sx|Sp|[kmbMTO]|N)/gi;return e.replace(t,(n,r,i)=>{const a={k:3,K:3,m:6,M:6,b:9,B:9,T:12,t:12};let s;if(i.length>1){const g=i.charAt(0).toUpperCase()+i.slice(1).toLowerCase();s=a[i]||a[g]}else{const g=i.toLowerCase();s=a[i]||a[g]}if(!s)return n;const o=r.startsWith("-"),c=o?r.slice(1):r,[l,h=""]=c.split("."),d=l.replace(/^0+/,"")||"0";let u;if(h.length===0)u=d+"0".repeat(s);else if(h.length<=s){const g=s-h.length;u=d+h+"0".repeat(g)}else{const g=h.slice(0,s),f=h.slice(s);u=d+g+"."+f}return u=u.replace(/^(-?)0+([1-9])/,"$1$2"),u.match(/^-?0+$/)&&(u=o?"-0":"0"),u.includes(".")&&(u=u.replace(/\.?0+$/,"")),o&&!u.startsWith("-")?"-"+u:u})}function Ae(e){if(!e||e==="0"||e==="-0"||e==="-"||e===".")return e;const t=e.startsWith("-"),n=t?e.slice(1):e;if(!n||n==="0"||n===".")return e;if(n.includes(".")){const[i,a]=n.split(".");if(i&&i.length>0){const o=(i.replace(/^0+/,"")||"0")+"."+a;return t?"-"+o:o}return e}if(n.startsWith("0")&&n.length>1){const i=n.replace(/^0+/,"")||"0";return t?"-"+i:i}return e}function Te(e){return e.replace(/[\u00A0\u2000-\u200B\u202F\u205F\u3000]/g," ").replace(/\s/g,"")}function m(e,t){const n=w(t);return e.replace(new RegExp(n,"g"),"")}const Ie=(e,t)=>{let n=Te(e);return t!=null&&t.thousandSeparator&&(n=m(n,t.thousandSeparator)),t!=null&&t.enableCompactNotation&&(n=Ce(n)),n=De(n),n=we(n,t==null?void 0:t.enableNegative,t==null?void 0:t.decimalSeparator),n=re(n,(t==null?void 0:t.decimalSeparator)||E),t!=null&&t.enableLeadingZeros||(n=Ae(n)),n};function xe(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 y(e,t,n,r){const i=T(n),a=r??(n==null?void 0:n.formatOn)===b.Change,s=Ie(e,xe(n,i,a)),o=ne(s,t,i.decimalSeparator),c=(n==null?void 0:n.decimalMinLength)??0,l=se(o,c,i.decimalSeparator),h=l;return{formatted:ce(l,n,i),raw:h}}function Oe(e,t,n){const r=!!(n!=null&&n.thousandSeparator);return y(e,t,n,r)}function Re(e,t,n){if(e==="Backspace"||e==="Delete")return e==="Delete"&&t===n?{endOffset:1}:{endOffset:0}}function K(e,t){const{decimalSeparator:n}=T(t),r=e.target;if(te(e,r,t,n)){e.preventDefault();return}return Le(e,r,t),Re(e.key,r.selectionStart,r.selectionEnd)}function q(e,t,n,r){const i=e.target,a=i.value,s=Ne(i),o=T(r),c=(r==null?void 0:r.formatOn)===b.Change,{formatted:l,raw:h}=y(a,t,r,c);return i.value=l,a!==l&&me(i,a,l,s,n,o,r),{formatted:l,raw:h}}function _e(e,t,n,r){const i=r-n;return e+t+i}function H(e,t,n){var u;e.preventDefault();const r=e.target,{value:i,selectionStart:a,selectionEnd:s}=r,o=((u=e.clipboardData)==null?void 0:u.getData("text/plain"))||"",c=i.slice(0,a||0)+o+i.slice(s||0),{formatted:l,raw:h}=y(c,t,n,!0);r.value=l;const d=_e(a||0,o.length,c.length,l.length);return r.setSelectionRange(d,d),{formatted:l,raw:h}}function Pe(e,t){const n=w(e);return t?`^-?[0-9]*[${n}]?[0-9]*$`:`^[0-9]*[${n}]?[0-9]*$`}function Be(e){We(e.decimalMaxLength),ke(e.decimalMinLength),Ze(e.decimalMinLength,e.decimalMaxLength),ze(e.formatOn),Ge(e.thousandSeparator),Ue(e.thousandStyle),je(e.decimalSeparator),Je(e.thousandSeparator,e.decimalSeparator),M("enableCompactNotation",e.enableCompactNotation),M("enableNegative",e.enableNegative),M("enableLeadingZeros",e.enableLeadingZeros),M("rawValueMode",e.rawValueMode),Ke(e.onChange)}function We(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 ke(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 Ze(e,t){if(!(e===void 0||t===void 0)&&e>t)throw new Error(`decimalMinLength (${e}) cannot be greater than decimalMaxLength (${t}).`)}function ze(e){if(e!==void 0&&e!==b.Blur&&e!==b.Change)throw new Error(`formatOn must be either ${b.Blur} or ${b.Change}. Received: ${JSON.stringify(e)}`)}function Ge(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 Ue(e){if(e!==void 0&&!Object.values(S).includes(e))throw new Error(`ThousandStyle must be one of: ${Object.values(S).map(t=>`'${t}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function je(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 Je(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 Ke(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 qe{constructor(t,{decimalMaxLength:n=x,decimalMinLength:r=O,formatOn:i=R,thousandSeparator:a=_,thousandStyle:s=P,decimalSeparator:o=E,enableCompactNotation:c=!0,enableNegative:l=B,enableLeadingZeros:h=W,rawValueMode:d=k,onChange:u,...g}){L(this,"element");L(this,"options");L(this,"resolvedOptions");L(this,"rawValue","");L(this,"caretPositionBeforeChange");if(Be({decimalMaxLength:n,decimalMinLength:r,formatOn:i,thousandSeparator:a,thousandStyle:s,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,onChange:u}),this.options={decimalMaxLength:n,decimalMinLength:r,onChange:u,formatOn:i,thousandSeparator:a,thousandStyle:s,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,...g},this.resolvedOptions=this.getResolvedOptions(),this.createInputElement(t),this.setupEventListeners(),this.resolvedOptions.rawValueMode&&this.element.value){const f=this.element.value,p=this.resolvedOptions.thousandSeparator?m(f,this.resolvedOptions.thousandSeparator):f;this.rawValue=p,this.element.value=this.formatValueForDisplay(p)}else if(this.element.value){const f=this.element.value;this.element.value=this.formatValueForDisplay(f)}}createInputElement(t){if(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"),this.resolvedOptions.decimalSeparator!==void 0&&this.resolvedOptions.enableNegative!==void 0){const X=Pe(this.resolvedOptions.decimalSeparator,this.resolvedOptions.enableNegative);this.element.setAttribute("pattern",X)}const{decimalMaxLength:n,decimalMinLength:r,formatOn:i,thousandSeparator:a,thousandStyle:s,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,onChange:u,value:g,defaultValue:f,type:p,inputMode:$,spellcheck:D,autocomplete:He,...Q}=this.options;Object.assign(this.element,Q),g!==void 0?this.element.value=g: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===b.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)))}getResolvedOptions(){return{decimalMaxLength:this.options.decimalMaxLength??x,decimalMinLength:this.options.decimalMinLength??O,formatOn:this.options.formatOn??R,thousandSeparator:this.options.thousandSeparator??_,thousandStyle:this.options.thousandStyle??P,decimalSeparator:this.options.decimalSeparator??E,enableCompactNotation:this.options.enableCompactNotation??F,enableNegative:this.options.enableNegative??B,enableLeadingZeros:this.options.enableLeadingZeros??W,rawValueMode:this.options.rawValueMode??k,onChange:this.options.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:a}=this.resolvedOptions;return n&&r!==S.None?G(t,n,r,i,a):t}handleChange(t){const{formatted:n,raw:r}=q(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,a=this.buildFormattingOptions(),s=K(t,{formatOn:a.formatOn,thousandSeparator:a.thousandSeparator,ThousandStyle:a.ThousandStyle,decimalSeparator:a.decimalSeparator});s?this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0,endOffset:s.endOffset}:this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0}}handlePaste(t){const{formatted:n,raw:r}=H(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===b.Blur&&this.resolvedOptions.thousandSeparator){const n=t.target;n.value=m(n.value,this.resolvedOptions.thousandSeparator)}}handleBlur(t){const n=t.target,{thousandSeparator:r,thousandStyle:i}=this.resolvedOptions;if(r&&i!==S.None&&n.value){const a=n.value,s=this.formatValueForDisplay(n.value);n.value=s;const o=this.resolvedOptions.rawValueMode?m(s,r):void 0;if(this.handleValueChange(s,o),a!==s){const c=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(c);const l=new Event("change",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(l)}}}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(t){if(this.resolvedOptions.rawValueMode){const n=this.resolvedOptions.thousandSeparator?m(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?m(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=b;exports.NumoraInput=qe;exports.ThousandStyle=S;exports.formatValue=Oe;exports.handleOnChangeNumoraInput=q;exports.handleOnKeyDownNumoraInput=K;exports.handleOnPasteNumoraInput=H;exports.processAndFormatValue=y;
|
|
1
|
+
"use strict";var V=Object.defineProperty;var F=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var $=(e,t,n)=>F(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var b=(e=>(e.Blur="blur",e.Change="change",e))(b||{}),S=(e=>(e.None="none",e.Thousand="thousand",e.Lakh="lakh",e.Wan="wan",e))(S||{});const x=2,O=0,R=b.Blur,_=",",B=S.None,E=".",ee=!1,P=!1,W=!1,k=!1;function I(e){return{decimalSeparator:(e==null?void 0:e.decimalSeparator)??E,thousandSeparator:e==null?void 0:e.thousandSeparator}}function U(e,t){const n=e.startsWith("-"),r=n?e.slice(1):e,[s="",i=""]=r.split(t);return{sign:n?"-":"",integer:s,decimal:i}}function te(e,t){if(!e.value.includes(t))return!1;const{selectionStart:n,selectionEnd:r,value:s}=e;return!s.slice(n??0,r??0).includes(t)}function ne(e,t,n,r){const{key:s}=e;if(s!==","&&s!==".")return!1;if(te(t,r))return!0;if(s!==r){const{selectionStart:i,selectionEnd:a,value:o}=t,c=i??0,l=a??c;t.value=o.slice(0,c)+r+o.slice(l);const h=c+1;return t.setSelectionRange(h,h),!0}return!1}const re=(e,t,n=E)=>{const{sign:r,integer:s,decimal:i}=U(e,n);if(!e.includes(n))return e;const o=i.slice(0,t);return`${r}${s}${n}${o}`},ie=(e,t=E)=>{const n=e.indexOf(t);if(n===-1)return e;const r=e.slice(0,n+1),i=e.slice(n+1).split(",").join("").split(".").join("").split(t).join("");return r+i},se=(e,t=0,n=E)=>{if(t<=0)return e;const{sign:r,integer:s,decimal:i}=U(e,n);if(i.length>=t)return e.includes(n)?e:`${r}${s}${n}${i}`;const a=i.padEnd(t,"0");return`${r}${s}${n}${a}`},N={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}};function j(e,t,n=S.Thousand,r=!1,s="."){if(!e||e==="0"||e===s||e==="-"||e===`-${s}`)return e;const i=e.includes(s),a=e.startsWith("-"),o=a?e.slice(1):e,[c,l]=o.split(s);if(!c){const u=l?`${s}${l}`:o;return a?`-${u}`:u}if(r&&c.startsWith("0")&&c.length>1){const u=c.match(/^(0+)/);if(u){const g=u[1],f=c.slice(g.length);if(f){const p=Z(f,t,n),m=g+p,w=a?"-":"";return i?l?`${w}${m}${s}${l}`:`${w}${m}${s}`:`${w}${m}`}}}const h=Z(c,t,n),d=a?"-":"";return i?l?`${d}${h}${s}${l}`:`${d}${h}${s}`:`${d}${h}`}function Z(e,t,n){if(e==="0"||e==="")return e;switch(n){case S.None:return e;case S.Thousand:return ae(e,t);case S.Lakh:return oe(e,t);case S.Wan:return ce(e,t);default:return e}}function ae(e,t){return z(e,t,N.thousand.size)}function oe(e,t){if(e.length<=N.lakh.firstGroup)return e;const n=e.split("").reverse(),r=[],s=n.slice(0,N.lakh.firstGroup).reverse().join("");r.push(s);for(let i=N.lakh.firstGroup;i<n.length;i+=N.lakh.restGroup)r.push(n.slice(i,i+N.lakh.restGroup).reverse().join(""));return r.reverse().join(t)}function ce(e,t){return z(e,t,N.wan.size)}function z(e,t,n){const r=e.split("").reverse(),s=[];for(let i=0;i<r.length;i+=n)s.push(r.slice(i,i+n).reverse().join(""));return s.reverse().join(t)}function le(e,t,n){return(t==null?void 0:t.formatOn)==="change"&&t.thousandSeparator?j(e,t.thousandSeparator,t.ThousandStyle??S.None,t.enableLeadingZeros,(n==null?void 0:n.decimalSeparator)??E):e}function v(e,t,n,r="."){let s=0;for(let i=0;i<t&&i<e.length;i++)e[i]!==n&&s++;return s}function he(e,t,n,r="."){if(t===0)return 0;let s=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(s===t-1)return i+1;s++}return e.length}function A(e,t,n,r="."){if(t===0)return 0;let s=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(s++,s===t)return i+1;if(s>t)return i}return e.length}function J(e,t,n){return t<0||t>=e.length?!1:e[t]===n}function ue(e,t={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:s="",suffix:i=""}=t,a=Array.from({length:e.length+1}).map(()=>!0);if(s&&a.fill(!1,0,s.length),i){const o=e.length-i.length;a.fill(!1,o+1,e.length+1)}for(let o=0;o<e.length;o++){const c=e[o];(n&&c===n||c===r)&&(a[o]=!1,o+1<e.length&&!/\d/.test(e[o+1])&&(a[o+1]=!1))}return a.some(o=>o)||a.fill(!0),a}function T(e,t,n,r){const s=e.length;if(t=Math.max(0,Math.min(t,s)),!n[t]){let i=t;for(;i<=s&&!n[i];)i++;let a=t;for(;a>=0&&!n[a];)a--;i<=s&&a>=0?t=t-a<i-t?a:i:i<=s?t=i:a>=0&&(t=a)}return(t===-1||t>s)&&(t=s),t}const K=(e,t)=>e===t;function de(e,t,n,r,s,i,a=".",o={}){if(n<0)return 0;if(n>e.length||e===""||t==="")return t.length;const c=/(\d+\.?\d*)\s*[kmbMTOQaqiSxsxSpOoNn]$/i;if(c.test(e)&&!c.test(t)&&t.length>e.length&&n>=e.length-1)return t.length;const l=t.length<e.length,h=J(e,n,r),d=e.indexOf(a),u=t.indexOf(a);if(o.isCharacterEquivalent&&e!==t){const p=fe(e,t,n,o.isCharacterEquivalent||K,i,o);if(p!==void 0)return p}if(l)return ge(e,t,n,r,h,d,u,i,a,o);const f=ve(e,t,n,r,h,a);return o.boundary?T(t,f,o.boundary):f}function fe(e,t,n,r,s,i){const a=e.length,o=t.length,c={},l=new Array(a);for(let f=0;f<a;f++){l[f]=-1;for(let p=0;p<o;p++){if(c[p])continue;if(r(e[f],t[p],{oldValue:e,newValue:t,typedRange:s,oldIndex:f,newIndex:p})){l[f]=p,c[p]=!0;break}}}let h=n;for(;h<a&&(l[h]===-1||!/\d/.test(e[h]));)h++;const d=h===a||l[h]===-1?o:l[h];for(h=n-1;h>=0&&l[h]===-1;)h--;const u=h===-1||l[h]===-1?0:l[h]+1;if(u>d)return d;const g=n-u<d-n?u:d;return i.boundary?T(t,g,i.boundary):g}function ge(e,t,n,r,s,i,a,o,c,l={}){if(s)return pe(e,t,n,r,a,c);const h=v(e,n,r,c),d=v(e,e.length,r,c),u=v(t,t.length,r,c),g=d-u,f=Se(e,n,r,h,g,i,o,c),p=i===-1||n<=i,m=be(t,f,r,g,o,c,p,a);return l.boundary?T(t,m,l.boundary):m}function pe(e,t,n,r,s,i){const a=n+1;if(a<e.length){const o=v(e,a,r,i),c=he(t,o,r,i);return c<t.length&&t[c]!==r?c+1:c}return n}function Se(e,t,n,r,s,i,a,o){if(a){const{start:l,isDelete:h}=a;return h?v(e,l,n,o):Math.max(0,v(e,l,n,o))}return t>0&&e[t-1]===n&&s>0?r+1:r}function be(e,t,n,r,s,i,a,o){if(a&&o!==-1){const l=e.substring(0,o),h=v(l,l.length,n,i);if(t<=h){const d=A(l,t,n,i);if(d>0&&d<l.length&&v(l,d,n,i)===t){if(l[d]===n&&s&&r>0&&d<l.length-1)return d+1;if(!s&&r>0&&d<l.length-1)return Math.min(d+1,l.length)}return d}}const c=A(e,t,n,i);if(c>0&&c<e.length&&v(e,c,n,i)===t){if(e[c]===n&&s&&r>0&&c<e.length-1)return c+1;if(!s&&r>0&&c<e.length-1)return Math.min(c+1,e.length)}return c}function ve(e,t,n,r,s,i){const a=n>=e.length,o=v(e,n,r,i),c=v(e,e.length,r,i),l=v(t,t.length,r,i);if(a||o===c)return t.length;const h=l-c;let d=o;h>0&&!a&&o<c&&(d=o+1);const u=A(t,d,r,i);return s&&!J(t,u,r)?Math.max(0,u-1):u}function me(e,t,n){const{selectionStart:r,selectionEnd:s,endOffset:i=0}=e;if(r!==s){const h=s-r;return{start:r,end:s,deletedLength:h,isDelete:!1}}if(i>0){const h=i;return{start:r,end:r+h,deletedLength:h,isDelete:!0}}const o=t.length,c=n.length,l=o-c;if(!(l<=0))return{start:r,end:r+l,deletedLength:l,isDelete:!1}}function Ee(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,s=t.length-1;for(;r>=n&&s>=n&&e[r]===t[s];)r--,s--;const i=r-n+1,a=s-n+1;if(!(i===0&&a===0))return{start:n,end:r+1,deletedLength:i,isDelete:i>a}}function G(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 Ne(e,t,n){return e.selectionStart===0&&e.selectionEnd===e.value.length?null:(G(e,t),setTimeout(()=>{e.value===n&&e.selectionStart!==t&&G(e,t)},0))}function $e(e){return Math.max(e.selectionStart,e.selectionEnd)}function Le(e,t,n){if((n==null?void 0:n.formatOn)!==b.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:s,value:i}=t;if(r===null||s===null||r!==s)return;const{key:a}=e,o=n.thousandSeparator;a==="Backspace"&&r>0&&i[r-1]===o&&t.setSelectionRange(r-1,r-1),a==="Delete"&&i[r]===o&&t.setSelectionRange(r+1,r+1)}function we(e,t,n,r,s,i,a){if(!s)return;const{selectionStart:o=0,selectionEnd:c=0,endOffset:l=0}=s;let h=me({selectionStart:o,selectionEnd:c,endOffset:l},t,n);if(h||(h=Ee(t,n)),!h)return;const d=ue(n,{thousandSeparator:(a==null?void 0:a.thousandSeparator)??i.thousandSeparator,decimalSeparator:i.decimalSeparator}),u={thousandSeparator:(a==null?void 0:a.thousandSeparator)??i.thousandSeparator,isCharacterEquivalent:K,boundary:d},g=(a==null?void 0:a.thousandSeparator)??i.thousandSeparator??",",f=(a==null?void 0:a.ThousandStyle)??S.None,p=de(t,n,r,g,f,h,i.decimalSeparator,u);Ne(e,p,n)}function M(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}const De=(e,t=!1,n=".")=>{const r=M(n),s=new RegExp(`[^0-9${r}]`,"g");if(!t)return e.replace(s,"");const i=e.startsWith("-"),a=e.replace(s,"");if(i){if(a.length>0)return"-"+a;if(e==="-")return"-"}return a};function Me(e){const t=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g;let n=e,r;const s=[];for(;(r=t.exec(e))!==null;){const i=r[0],a=r[1],o=parseInt(r[2],10);let c;if(o===0)c=a;else{const l=a.startsWith("-"),h=l?a.slice(1):a,[d,u=""]=h.split(".");o>0?c=ye(d,u,o):c=Ce(d,u,Math.abs(o)),l&&(c="-"+c)}s.push({full:i,expanded:c})}for(const{full:i,expanded:a}of s)n=n.replace(i,a);return n}function ye(e,t,n){const r=e+t;if(r==="0"||r.match(/^0+$/))return"0";const s=t.length;if(n<=s){const a=r.slice(0,e.length+n),o=r.slice(e.length+n);return o?`${a}.${o}`:a}const i=n-s;return r+"0".repeat(i)}function Ce(e,t,n){const r=e+t;if(r==="0"||r.match(/^0+$/))return"0";const a=e.length-n;if(a<=0){const o=Math.abs(a),c=`0.${"0".repeat(o)}${r}`;return C(c)}if(a<e.length){const o=r.slice(0,a),c=r.slice(a),l=`${o}.${c}`;return C(l)}return C(r)}function C(e){if(!e.includes("."))return e;if(e==="0"||e==="0.")return"0";if(e.startsWith("0.")){const t=e.replace(/\.?0+$/,"");return t==="0"?"0":t||"0"}if(e.startsWith("-0.")){const t=e.replace(/\.?0+$/,"");return t==="-0"||t==="0"?"0":t||"0"}return e.replace(/\.?0+$/,"")||e}function Ae(e){const t=/(\d+\.?\d*)\s*(Qa|Qi|Sx|Sp|[kmbMTO]|N)/gi;return e.replace(t,(n,r,s)=>{const i={k:3,K:3,m:6,M:6,b:9,B:9,T:12,t:12};let a;if(s.length>1){const g=s.charAt(0).toUpperCase()+s.slice(1).toLowerCase();a=i[s]||i[g]}else{const g=s.toLowerCase();a=i[s]||i[g]}if(!a)return n;const o=r.startsWith("-"),c=o?r.slice(1):r,[l,h=""]=c.split("."),d=l.replace(/^0+/,"")||"0";let u;if(h.length===0)u=d+"0".repeat(a);else if(h.length<=a){const g=a-h.length;u=d+h+"0".repeat(g)}else{const g=h.slice(0,a),f=h.slice(a);u=d+g+"."+f}return u=u.replace(/^(-?)0+([1-9])/,"$1$2"),u.match(/^-?0+$/)&&(u=o?"-0":"0"),u.includes(".")&&(u=u.replace(/\.?0+$/,"")),o&&!u.startsWith("-")?"-"+u:u})}function Ie(e){if(!e||e==="0"||e==="-0"||e==="-"||e===".")return e;const t=e.startsWith("-"),n=t?e.slice(1):e;if(!n||n==="0"||n===".")return e;if(n.includes(".")){const[s,i]=n.split(".");if(s&&s.length>0){const o=(s.replace(/^0+/,"")||"0")+"."+i;return t?"-"+o:o}return e}if(n.startsWith("0")&&n.length>1){const s=n.replace(/^0+/,"")||"0";return t?"-"+s:s}return e}function Te(e){return e.replace(/[\u00A0\u2000-\u200B\u202F\u205F\u3000]/g," ").replace(/\s/g,"")}function L(e,t){const n=M(t);return e.replace(new RegExp(n,"g"),"")}const xe=(e,t)=>{let n=Te(e);return t!=null&&t.thousandSeparator&&(n=L(n,t.thousandSeparator)),t!=null&&t.enableCompactNotation&&(n=Ae(n)),n=Me(n),n=De(n,t==null?void 0:t.enableNegative,t==null?void 0:t.decimalSeparator),n=ie(n,(t==null?void 0:t.decimalSeparator)||E),t!=null&&t.enableLeadingZeros||(n=Ie(n)),n};function Oe(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 y(e,t,n,r){const s=I(n),i=r??(n==null?void 0:n.formatOn)===b.Change,a=xe(e,Oe(n,s,i)),o=re(a,t,s.decimalSeparator),c=(n==null?void 0:n.decimalMinLength)??0,l=se(o,c,s.decimalSeparator),h=l;return{formatted:le(l,n,s),raw:h}}function Re(e,t,n){const r=!!(n!=null&&n.thousandSeparator);return y(e,t,n,r)}function _e(e,t,n){if(e==="Backspace"||e==="Delete")return e==="Delete"&&t===n?{endOffset:1}:{endOffset:0}}function q(e,t){const{decimalSeparator:n}=I(t),r=e.target;if(ne(e,r,t,n)){e.preventDefault();return}return Le(e,r,t),_e(e.key,r.selectionStart,r.selectionEnd)}function H(e,t,n,r){const s=e.target,i=s.value,a=$e(s),o=I(r),c=(r==null?void 0:r.formatOn)===b.Change,{formatted:l,raw:h}=y(i,t,r,c);return s.value=l,i!==l&&we(s,i,l,a,n,o,r),{formatted:l,raw:h}}function Be(e,t,n,r){const s=r-n;return e+t+s}function Q(e,t,n){var u;e.preventDefault();const r=e.target,{value:s,selectionStart:i,selectionEnd:a}=r,o=((u=e.clipboardData)==null?void 0:u.getData("text/plain"))||"",c=s.slice(0,i||0)+o+s.slice(a||0),{formatted:l,raw:h}=y(c,t,n,!0);r.value=l;const d=Be(i||0,o.length,c.length,l.length);return r.setSelectionRange(d,d),{formatted:l,raw:h}}function Pe(e,t){const n=M(e);return t?`^-?[0-9]*[${n}]?[0-9]*$`:`^[0-9]*[${n}]?[0-9]*$`}function We(e){ke(e.decimalMaxLength),Ze(e.decimalMinLength),Ge(e.decimalMinLength,e.decimalMaxLength),Ue(e.formatOn),je(e.thousandSeparator),ze(e.thousandStyle),Je(e.decimalSeparator),Ke(e.thousandSeparator,e.decimalSeparator),D("enableCompactNotation",e.enableCompactNotation),D("enableNegative",e.enableNegative),D("enableLeadingZeros",e.enableLeadingZeros),D("rawValueMode",e.rawValueMode),qe(e.onChange)}function ke(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 Ze(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 Ge(e,t){if(!(e===void 0||t===void 0)&&e>t)throw new Error(`decimalMinLength (${e}) cannot be greater than decimalMaxLength (${t}).`)}function Ue(e){if(e!==void 0&&e!==b.Blur&&e!==b.Change)throw new Error(`formatOn must be either ${b.Blur} or ${b.Change}. Received: ${JSON.stringify(e)}`)}function je(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 ze(e){if(e!==void 0&&!Object.values(S).includes(e))throw new Error(`ThousandStyle must be one of: ${Object.values(S).map(t=>`'${t}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function Je(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 Ke(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 D(e,t){if(t!==void 0&&typeof t!="boolean")throw new Error(`${e} must be a boolean. Received: ${typeof t} (${JSON.stringify(t)})`)}function qe(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 He{constructor(t,{decimalMaxLength:n=x,decimalMinLength:r=O,formatOn:s=R,thousandSeparator:i=_,thousandStyle:a=B,decimalSeparator:o=E,enableCompactNotation:c=!0,enableNegative:l=P,enableLeadingZeros:h=W,rawValueMode:d=k,onChange:u,...g}){$(this,"element");$(this,"options");$(this,"resolvedOptions");$(this,"rawValue","");$(this,"caretPositionBeforeChange");if(We({decimalMaxLength:n,decimalMinLength:r,formatOn:s,thousandSeparator:i,thousandStyle:a,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,onChange:u}),this.options={decimalMaxLength:n,decimalMinLength:r,onChange:u,formatOn:s,thousandSeparator:i,thousandStyle:a,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,...g},this.resolvedOptions=this.getResolvedOptions(),this.createInputElement(t),this.setupEventListeners(),this.resolvedOptions.rawValueMode&&this.element.value){const f=this.element.value,p=this.resolvedOptions.thousandSeparator?L(f,this.resolvedOptions.thousandSeparator):f;this.rawValue=p,this.element.value=this.formatValueForDisplay(p)}else if(this.element.value){const f=this.element.value;this.element.value=this.formatValueForDisplay(f)}}createInputElement(t){if(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"),this.resolvedOptions.decimalSeparator!==void 0&&this.resolvedOptions.enableNegative!==void 0){const Y=Pe(this.resolvedOptions.decimalSeparator,this.resolvedOptions.enableNegative);this.element.setAttribute("pattern",Y)}const{decimalMaxLength:n,decimalMinLength:r,formatOn:s,thousandSeparator:i,thousandStyle:a,decimalSeparator:o,enableCompactNotation:c,enableNegative:l,enableLeadingZeros:h,rawValueMode:d,onChange:u,value:g,defaultValue:f,type:p,inputMode:m,spellcheck:w,autocomplete:Qe,...X}=this.options;Object.assign(this.element,X),g!==void 0?this.element.value=g: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===b.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)))}getResolvedOptions(){return{decimalMaxLength:this.options.decimalMaxLength??x,decimalMinLength:this.options.decimalMinLength??O,formatOn:this.options.formatOn??R,thousandSeparator:this.options.thousandSeparator??_,thousandStyle:this.options.thousandStyle??B,decimalSeparator:this.options.decimalSeparator??E,enableCompactNotation:this.options.enableCompactNotation??ee,enableNegative:this.options.enableNegative??P,enableLeadingZeros:this.options.enableLeadingZeros??W,rawValueMode:this.options.rawValueMode??k,onChange:this.options.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:s,decimalSeparator:i}=this.resolvedOptions;return n&&r!==S.None?j(t,n,r,s,i):t}handleChange(t){const{formatted:n,raw:r}=H(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:s}=n,i=this.buildFormattingOptions(),a=q(t,{formatOn:i.formatOn,thousandSeparator:i.thousandSeparator,ThousandStyle:i.ThousandStyle,decimalSeparator:i.decimalSeparator});a?this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:s??0,endOffset:a.endOffset}:this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:s??0}}handlePaste(t){const{formatted:n,raw:r}=Q(t,this.resolvedOptions.decimalMaxLength,this.buildFormattingOptions());this.handleValueChange(n,r);const s=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(s)}handleFocus(t){if(this.resolvedOptions.formatOn===b.Blur&&this.resolvedOptions.thousandSeparator){const n=t.target;n.value=L(n.value,this.resolvedOptions.thousandSeparator)}}handleBlur(t){const n=t.target,{thousandSeparator:r,thousandStyle:s}=this.resolvedOptions;if(r&&s!==S.None&&n.value){const i=n.value,a=this.formatValueForDisplay(n.value);n.value=a;const o=this.resolvedOptions.rawValueMode?L(a,r):void 0;if(this.handleValueChange(a,o),i!==a){const c=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(c);const l=new Event("change",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(l)}}}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(t){if(this.resolvedOptions.rawValueMode){const n=this.resolvedOptions.thousandSeparator?L(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?L(t,this.resolvedOptions.thousandSeparator):t,r=this.resolvedOptions.decimalSeparator&&this.resolvedOptions.decimalSeparator!=="."?n.replace(new RegExp(M(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=b;exports.NumoraInput=He;exports.ThousandStyle=S;exports.formatInputValue=y;exports.formatValueForDisplay=Re;exports.handleOnChangeNumoraInput=H;exports.handleOnKeyDownNumoraInput=q;exports.handleOnPasteNumoraInput=Q;
|