diff --git a/dist/js/libs/oj/debug/ojconverter-nativenumber.js b/dist/js/libs/oj/debug/ojconverter-nativenumber.js index 92ba9091..96e6b484 100644 --- a/dist/js/libs/oj/debug/ojconverter-nativenumber.js +++ b/dist/js/libs/oj/debug/ojconverter-nativenumber.js @@ -391,6 +391,7 @@ define(['exports', 'ojs/ojconfig', 'ojs/ojconverter-preferences'], function (exp lenientParse: options.lenientParse ?? 'full', roundDuringParse: options.roundDuringParse ?? false, roundingMode: options.roundingMode ?? 'HALF_UP', + signDisplay: options.signDisplay ?? 'auto', separators: { decimal: options.separators?.decimal ?? getLocaleData(locale).decimalSeparator, group: options.separators?.group ?? _getDefaultGroupingSeparator(native, locale) @@ -450,6 +451,9 @@ define(['exports', 'ojs/ojconfig', 'ojs/ojconverter-preferences'], function (exp default: throw new Error('Invalid format style'); } + if (options.signDisplay !== undefined) { + nativeOpts.signDisplay = options.signDisplay; + } return nativeOpts; } function _getDefaultFractionDigits(options, locale) { diff --git a/dist/js/libs/oj/debug_esm/ojconverter-nativenumber.js b/dist/js/libs/oj/debug_esm/ojconverter-nativenumber.js index 6437688f..cd831a17 100644 --- a/dist/js/libs/oj/debug_esm/ojconverter-nativenumber.js +++ b/dist/js/libs/oj/debug_esm/ojconverter-nativenumber.js @@ -392,6 +392,7 @@ function getResolvedAndNativeOptions(options, locale) { lenientParse: options.lenientParse ?? 'full', roundDuringParse: options.roundDuringParse ?? false, roundingMode: options.roundingMode ?? 'HALF_UP', + signDisplay: options.signDisplay ?? 'auto', separators: { decimal: options.separators?.decimal ?? getLocaleData(locale).decimalSeparator, group: options.separators?.group ?? _getDefaultGroupingSeparator(native, locale) @@ -451,6 +452,9 @@ function _getNativeOptions(options) { default: throw new Error('Invalid format style'); } + if (options.signDisplay !== undefined) { + nativeOpts.signDisplay = options.signDisplay; + } return nativeOpts; } function _getDefaultFractionDigits(options, locale) { diff --git a/dist/js/libs/oj/min/ojconverter-nativenumber.js b/dist/js/libs/oj/min/ojconverter-nativenumber.js index 044bb543..c59f3eaf 100644 --- a/dist/js/libs/oj/min/ojconverter-nativenumber.js +++ b/dist/js/libs/oj/min/ojconverter-nativenumber.js @@ -5,5 +5,5 @@ * as shown at https://oss.oracle.com/licenses/upl/ * @ignore */ -define(["exports","ojs/ojconfig","ojs/ojconverter-preferences"],function(e,t,n){"use strict";const i=".",r="-",o=/^([+|-])?(\d+\.\d+|\.?\d+)(?:[e|E])([+|-])?(\d+)$/,a=/^([+|-])?(\d+\.\d+|\.?\d+)$/,s=/0+$/,c=/^0+/;function u(e){let t=e.match(a);if(t){return{negative:t[1]===r,...m(t[2])}}if(t=e.match(o),t){const e=t[1]===r,n=m(t[2]);let i=parseInt(t[4]);return i=t[3]===r?-i:i,{negative:e,...l(n,i)}}throw new Error("Not a valid number")}function l(e,t){let{whole:n,decimal:i}=e;if(0===t||""===n&&""===i)return e;if(t>0){const e=Math.min(t,i.length),r=Math.max(t-e,0);n=n+i.substring(0,e)+"0".repeat(r),e>0&&(i=i.substring(e))}else{const e=Math.min(-t,n.length),r=Math.max(-t-e,0),o=n.length-e;i="0".repeat(r)+n.substring(o)+i,e>0&&(n=n.substring(0,o))}return n=g(n,!1),i=g(i,!0),{whole:n,decimal:i}}function m(e){let t="",n="";const r=e.split(i);if(1==r.length){const o=r[0];e.startsWith(i)?n=o:t=o}else t=r[0],n=r[1];return{whole:g(t,!1),decimal:g(n,!0)}}function g(e,t){return e.replace(t?s:c,"")}function d(e){let t=f.get(e);return t||(t=function(e){const t=function(e){const t=new Intl.NumberFormat(e,{useGrouping:!1,maximumFractionDigits:0}),n=new Map,i=t.format(9876543210);for(let e=0;e<10;e++){const t=i[e],r=(9-e).toString();n.set(t,r)}return n}(e),n=function(e){return p(e,{maximumFractionDigits:1,minimumFractionDigits:1},1.1,"decimal",".")}(e),i=h(e,!1),r=h(e,!0),o=function(e){return p(e,{style:"percent"},1,"percentSign","%")}(e),a=function(e){return p(e,{notation:"scientific"},100,"exponentSeparator","E")}(e),s=function(e){const t={style:"currency",currency:"USD",currencySign:"accounting"},n=new Intl.NumberFormat(e,t).formatToParts(-1)[0].value;return n}(e);return{localToAsciiNumbers:t,decimalSeparator:n,negativeSign:i,positiveSign:r,percentSign:o,exponentSeparator:a,negativeAccountingSign:s}}(e),f.set(e,t)),t}function h(e,t){const n={signDisplay:"always"};return t?p(e,n,1,"plusSign","+"):p(e,n,-1,"minusSign","-")}function p(e,t,n,i,r){const o=new Intl.NumberFormat(e,t).formatToParts(n);return o.find(e=>e.type===i)?.value??r}const f=new Map,y=new Map([["HALF_UP","halfExpand"],["HALF_DOWN","halfTrunc"],["HALF_EVEN","halfEven"],["UP","expand"],["DOWN","trunc"],["CEILING","ceil"],["FLOOR","floor"]]),v=/0+$/;function w(e,t,n){let{whole:i,negative:r,decimal:o}=e;const a=o.length-t;if(a>0){const e=(i||"0")+o,s=[];let c=e.length-2,u=parseInt(e[c+1]);for(let t=0;c>=0;c--){let i=parseInt(e[c]);10===u&&(u=0,i++),t=5?1:0);case"HALF_DOWN":return e+(t>5?1:0);case"HALF_EVEN":return e+(5==t&&e%2==0||t<5?0:1);case"UP":case"CEILING":case"FLOOR":return e+1;default:throw new Error("Invalid rounding mode "+i)}}const D=new Set(["0","1","2","3","4","5","6","7","8","9"]);function I(e,t,n){const i=d(n),{decimal:r,group:o}=t.separators,a=".",{negativeSign:s,positiveSign:c,percentSign:m,localToAsciiNumbers:g,exponentSeparator:h,negativeAccountingSign:p}=i;let f,y=0,v="";"currency"===t.style&&"accounting"===t.currencySign&&p!==s&&(e=function(e){const t=e.trim(),n="(",i=")",r="(",o=")",a=t.startsWith(n)||t.startsWith(r),s=t.endsWith(i)||t.endsWith(o);if(a&&s){return"-"+t.slice(1,-1)}return e}(e));for(let n of e)switch(n){case s:case"-":v+="-";break;case r:v+=a;break;case a:o===a||e.includes(r)||(v+=a);break;case h:case"E":case"e":v+="E";break;case m:case"%":case c:case"+":case o:break;default:const i=g.get(n);i?v+=i:(N(t),D.has(n)&&(v+=n))}"percent"===t.style&&(y=-2);try{f=u(v)}catch(e){_()}0!==y&&(f={negative:f.negative,...l(f,y)}),t.roundDuringParse&&(f={negative:f.negative,...w(f,t.maximumFractionDigits,t.roundingMode)});const F=f.whole||"0",b=""!==f.decimal;return v=(f.negative?"-":"")+F+(b?"."+f.decimal:""),v}function N(e){"none"===e.lenientParse&&_()}function _(){throw new Error("Not a valid number",{cause:"userInput"})}function S(e,t){const n=function(e){let t;switch(e.style){case"decimal":case void 0:t=function(e){const t=e.decimalFormat??"standard",n={style:"decimal"};switch(t){case"short":n.notation="compact",n.compactDisplay="short";break;case"long":n.notation="compact",n.compactDisplay="long";break;case"standard":break;default:throw new Error("Invalid decimalFormat: "+t)}return n}(e);break;case"currency":t=function(e){const t=e.currencyFormat??"standard",n=e.currency;if(!n)throw new Error("Currency option is required for currency style");const i={style:"currency",currency:n,currencyDisplay:e.currencyDisplay??"symbol",currencySign:e.currencySign??"standard"};switch(t){case"short":i.notation="compact",i.compactDisplay="short";break;case"standard":break;default:throw new Error("Invalid currencyFormat: "+t)}return i}(e);break;case"percent":t={style:"percent"};break;case"unit":const n=e.unit;if("byte"!==n&&"bit"!==n)throw new Error("invalid unit name");t={style:"unit",unitDisplay:"short",unit:n};break;default:throw new Error("Invalid format style")}return t}(e),{defaultMinFractionDigits:i,defaultMaxFractionDigits:r}=function(e,t){const n=new Intl.NumberFormat(t,e).resolvedOptions().minimumFractionDigits??0,i=new Intl.NumberFormat(t,e).resolvedOptions().maximumFractionDigits??0;return{defaultMinFractionDigits:n,defaultMaxFractionDigits:i}}(n,t),o=e.minimumFractionDigits??Math.min(i,e.maximumFractionDigits??Number.MAX_SAFE_INTEGER),a=e.maximumFractionDigits??Math.max(o,r);if(o>a)throw new Error("maximumFractionDigits value is out of range");const s={locale:t,minimumIntegerDigits:e.minimumIntegerDigits??0,minimumFractionDigits:o,maximumFractionDigits:a,useGrouping:e.useGrouping??void 0!==O(n,t,!1),lenientParse:e.lenientParse??"full",roundDuringParse:e.roundDuringParse??!1,roundingMode:e.roundingMode??"HALF_UP",separators:{decimal:e.separators?.decimal??d(t).decimalSeparator,group:e.separators?.group??O(n,t)}};let c;switch(e.style){case"decimal":case void 0:c={style:"decimal",decimalFormat:e.decimalFormat??"standard",virtualKeyboardHint:M(s.useGrouping,s.separators.decimal,s.separators.group),...s};break;case"currency":c={style:"currency",currency:e.currency,currencyFormat:e.currencyFormat??"standard",currencyDisplay:e.currencyDisplay??"symbol",currencySign:e.currencySign??"standard",customCurrencyCode:e.customCurrencyCode,customCurrencySymbol:e.customCurrencySymbol,...s};break;case"percent":c={style:"percent",...s};break;case"unit":c={style:"unit",unit:e.unit,...s}}return{resolved:c,native:n}}function O(e,t,n=!0){const i=n?{...e,useGrouping:!0}:e,r=new Intl.NumberFormat(t,i).formatToParts(1e6);return r.find(e=>"group"===e.type)?.value}function M(e,t,n){return e&&"."===t&&""===n?"number":e||"."!==t?"text":"number"}const x=[1e3,10**6,10**9,10**12,10**15],E=[1e3,10**6,10**9,10**12],A=[1e3,10**6,10**9,10**12],P=["byte","kilobyte","megabyte","gigabyte","terabyte","petabyte"],C=["bit","kilobit","megabit","gigabit","terabit"];function R(e,t,n){const{scaled:i,scaleIndex:r}=W(e,t,x,n);return{scaled:i,unit:P[r]}}function k(e,t,n){const{scaled:i,scaleIndex:r}=W(e,t,E,n);return{scaled:i,unit:C[r]}}function G(e,t){const{scaled:n,scaleIndex:i}=H(e,x,t);return{scaled:n,unit:P[i]}}function L(e,t){const{scaled:n,scaleIndex:i}=H(e,E,t);return{scaled:n,unit:C[i]}}function W(e,t,n,i,r=!1){let{whole:o,negative:a,decimal:s}=e;const c=BigInt(o),u=n.length;let m=-1;for(let e=0;e=BigInt(n[e]);e++,m++);let g=3*(m+1),d=l({whole:o,decimal:s},-g),h=w({whole:d.whole,decimal:d.decimal,negative:a},t(d.decimal.length),i.roundingMode);return m=t[n];n++,o++);let a=-3*(o+1),s=e*10**a,c=parseFloat(i.format(s));return o"currency"===e.type?{...e,value:t}:e)}const n=function(e){return e.filter(e=>"currency"!==e.type)}(e);return function(e){return e.filter(e=>!("literal"===e.type&&""===e.value.trim()))}(n)}(e,n?t.customCurrencyCode:t.customCurrencySymbol)}}return e}function T(e,t,n,i){if(void 0===n&&void 0===i)return e.formatToParts(t);return e.formatToParts(t).map(e=>"group"===e.type?{...e,value:i??e.value}:"decimal"===e.type?{...e,value:n??e.value}:e)}e.BigDecimalStringConverter=class{constructor(e){const i=e??{style:"decimal"},r=n.getMergedNumberPreferencesWithOptions(i);this.options=r,this.locale=this.options.locale??t.getLocale()}format(e){if("string"!=typeof e)throw new Error("the value must be a string");const t=u(e),{native:n,resolved:i}=this._getResolvedAndNativeOptions();switch(i.style){case"decimal":case"currency":const e="compact"===n.notation?"compact":null;return this._format(t,n,e);case"percent":return this._format(t,n,"percent");case"unit":return this._format(t,n,i.unit)}}parse(e){return I(e,this.resolvedOptions(),this.locale)}resolvedOptions(){return this._getResolvedAndNativeOptions().resolved}getHint(){return null}_format(e,t={},n){let i=t;const r=this.resolvedOptions(),{minimumFractionDigits:o,maximumFractionDigits:a}=r;function s(e){return Math.min(Math.max(o,e),a)}let{decimal:c,whole:u,negative:m}=e;if("compact"===n){const{scaled:t}=function(e,t,n){return W(e,t,A,n,!0)}(e,s,r);u=t.whole,c=t.decimal}else if("percent"===n){const t=function(e,t,n){let{whole:i,negative:r,decimal:o}=e;const a=l({whole:i,decimal:o},2);return w({whole:a.whole,decimal:a.decimal,negative:r},n(a.decimal.length),t.roundingMode)}(e,r,s);u=t.whole,c=t.decimal}else if("byte"===n||"bit"===n){const t="byte"===n?R:k,{scaled:o,unit:a}=t(e,s,r);u=o.whole,c=o.decimal,i={...i,unit:a}}else{const e=s(c.length);if(e0||i>0?Math.max(i,1):0},c=T(new Intl.NumberFormat(this.locale,s),n?-1:1,o?.separators?.decimal,o?.separators?.group),u={minimumIntegerDigits:o.minimumIntegerDigits};u.useGrouping=o.useGrouping;const l=this._formatWithCustomSeparators(new Intl.NumberFormat(this.locale,u),BigInt(e));let m;if(a>0){const e=this._getFractionPart(t,i),n={useGrouping:!1,minimumIntegerDigits:this._getMinimumIntegerDigitsForFraction(e.length)};m=this._formatDecimalPart(n,e,e.length)}let g="";for(let e of c){const t=e.type;g+="fraction"===t?m??e.value:"integer"===t?l:e.value}return g}_formatWithCustomSeparators(e,t){const n=this.options?.separators?.decimal,i=this.options?.separators?.group;if(void 0===n&&void 0===i)return e.format(t);const r=T(e,t,n,i);let o="";for(let e of r)o+=e.value;return o}_getFractionPart(e,t){const n=t-e.length;return n>0&&e.length>0?e+"0".repeat(n):e}_getMinimumIntegerDigitsForFraction(e){return Math.min(e,20)}_formatDecimalPart(e,t,n){let i=new Intl.NumberFormat(this.locale,e).format(BigInt(t));const r=n-i.length;if(r>0){i=new Intl.NumberFormat(this.locale).format(0).repeat(r)+i}return i}_getResolvedAndNativeOptions(){return this.cachedResolvedAndNativeOptions||(this.cachedResolvedAndNativeOptions=S(this.options,this.locale)),this.cachedResolvedAndNativeOptions}},e.NumberConverter=class{constructor(e){const i=e??{style:"decimal"},r=n.getMergedNumberPreferencesWithOptions(i);this.options=r,this.locale=this.options.locale??t.getLocale()}format(e){let t=e;if("number"!=typeof t)throw new Error("the value must be a number");const{resolved:n,native:i}=this._getResolvedAndNativeOptions(),r=this.options,o={...i,minimumIntegerDigits:r.minimumIntegerDigits,minimumFractionDigits:r.minimumFractionDigits,maximumFractionDigits:r.maximumFractionDigits,useGrouping:r.useGrouping,roundingMode:F(n)};if("unit"===n.style){const i="bit"===n.unit?L:G,{scaled:a,unit:s}=i(e,r);t=a,o.unit=s}const a=new Intl.NumberFormat(this.locale,o),s=r?.separators?.decimal,c=r?.separators?.group;let u=T(a,t,s,c);u=B(u,r);let l="";for(let e of u)l+=`${e.value}`;return l}parse(e){return new Number(I(e,this.resolvedOptions(),this.locale)).valueOf()}resolvedOptions(){return this._getResolvedAndNativeOptions().resolved}getHint(){return null}_getResolvedAndNativeOptions(){return this.cachedResolvedAndNativeOptions||(this.cachedResolvedAndNativeOptions=S(this.options,this.locale)),this.cachedResolvedAndNativeOptions}},e.USER_INPUT_ERROR="userInput",Object.defineProperty(e,"__esModule",{value:!0})}); +define(["exports","ojs/ojconfig","ojs/ojconverter-preferences"],function(e,t,n){"use strict";const i=".",r="-",o=/^([+|-])?(\d+\.\d+|\.?\d+)(?:[e|E])([+|-])?(\d+)$/,a=/^([+|-])?(\d+\.\d+|\.?\d+)$/,s=/0+$/,c=/^0+/;function u(e){let t=e.match(a);if(t){return{negative:t[1]===r,...m(t[2])}}if(t=e.match(o),t){const e=t[1]===r,n=m(t[2]);let i=parseInt(t[4]);return i=t[3]===r?-i:i,{negative:e,...l(n,i)}}throw new Error("Not a valid number")}function l(e,t){let{whole:n,decimal:i}=e;if(0===t||""===n&&""===i)return e;if(t>0){const e=Math.min(t,i.length),r=Math.max(t-e,0);n=n+i.substring(0,e)+"0".repeat(r),e>0&&(i=i.substring(e))}else{const e=Math.min(-t,n.length),r=Math.max(-t-e,0),o=n.length-e;i="0".repeat(r)+n.substring(o)+i,e>0&&(n=n.substring(0,o))}return n=g(n,!1),i=g(i,!0),{whole:n,decimal:i}}function m(e){let t="",n="";const r=e.split(i);if(1==r.length){const o=r[0];e.startsWith(i)?n=o:t=o}else t=r[0],n=r[1];return{whole:g(t,!1),decimal:g(n,!0)}}function g(e,t){return e.replace(t?s:c,"")}function d(e){let t=f.get(e);return t||(t=function(e){const t=function(e){const t=new Intl.NumberFormat(e,{useGrouping:!1,maximumFractionDigits:0}),n=new Map,i=t.format(9876543210);for(let e=0;e<10;e++){const t=i[e],r=(9-e).toString();n.set(t,r)}return n}(e),n=function(e){return p(e,{maximumFractionDigits:1,minimumFractionDigits:1},1.1,"decimal",".")}(e),i=h(e,!1),r=h(e,!0),o=function(e){return p(e,{style:"percent"},1,"percentSign","%")}(e),a=function(e){return p(e,{notation:"scientific"},100,"exponentSeparator","E")}(e),s=function(e){const t={style:"currency",currency:"USD",currencySign:"accounting"},n=new Intl.NumberFormat(e,t).formatToParts(-1)[0].value;return n}(e);return{localToAsciiNumbers:t,decimalSeparator:n,negativeSign:i,positiveSign:r,percentSign:o,exponentSeparator:a,negativeAccountingSign:s}}(e),f.set(e,t)),t}function h(e,t){const n={signDisplay:"always"};return t?p(e,n,1,"plusSign","+"):p(e,n,-1,"minusSign","-")}function p(e,t,n,i,r){const o=new Intl.NumberFormat(e,t).formatToParts(n);return o.find(e=>e.type===i)?.value??r}const f=new Map,y=new Map([["HALF_UP","halfExpand"],["HALF_DOWN","halfTrunc"],["HALF_EVEN","halfEven"],["UP","expand"],["DOWN","trunc"],["CEILING","ceil"],["FLOOR","floor"]]),v=/0+$/;function w(e,t,n){let{whole:i,negative:r,decimal:o}=e;const a=o.length-t;if(a>0){const e=(i||"0")+o,s=[];let c=e.length-2,u=parseInt(e[c+1]);for(let t=0;c>=0;c--){let i=parseInt(e[c]);10===u&&(u=0,i++),t=5?1:0);case"HALF_DOWN":return e+(t>5?1:0);case"HALF_EVEN":return e+(5==t&&e%2==0||t<5?0:1);case"UP":case"CEILING":case"FLOOR":return e+1;default:throw new Error("Invalid rounding mode "+i)}}const D=new Set(["0","1","2","3","4","5","6","7","8","9"]);function I(e,t,n){const i=d(n),{decimal:r,group:o}=t.separators,a=".",{negativeSign:s,positiveSign:c,percentSign:m,localToAsciiNumbers:g,exponentSeparator:h,negativeAccountingSign:p}=i;let f,y=0,v="";"currency"===t.style&&"accounting"===t.currencySign&&p!==s&&(e=function(e){const t=e.trim(),n="(",i=")",r="(",o=")",a=t.startsWith(n)||t.startsWith(r),s=t.endsWith(i)||t.endsWith(o);if(a&&s){return"-"+t.slice(1,-1)}return e}(e));for(let n of e)switch(n){case s:case"-":v+="-";break;case r:v+=a;break;case a:o===a||e.includes(r)||(v+=a);break;case h:case"E":case"e":v+="E";break;case m:case"%":case c:case"+":case o:break;default:const i=g.get(n);i?v+=i:(N(t),D.has(n)&&(v+=n))}"percent"===t.style&&(y=-2);try{f=u(v)}catch(e){_()}0!==y&&(f={negative:f.negative,...l(f,y)}),t.roundDuringParse&&(f={negative:f.negative,...w(f,t.maximumFractionDigits,t.roundingMode)});const F=f.whole||"0",b=""!==f.decimal;return v=(f.negative?"-":"")+F+(b?"."+f.decimal:""),v}function N(e){"none"===e.lenientParse&&_()}function _(){throw new Error("Not a valid number",{cause:"userInput"})}function S(e,t){const n=function(e){let t;switch(e.style){case"decimal":case void 0:t=function(e){const t=e.decimalFormat??"standard",n={style:"decimal"};switch(t){case"short":n.notation="compact",n.compactDisplay="short";break;case"long":n.notation="compact",n.compactDisplay="long";break;case"standard":break;default:throw new Error("Invalid decimalFormat: "+t)}return n}(e);break;case"currency":t=function(e){const t=e.currencyFormat??"standard",n=e.currency;if(!n)throw new Error("Currency option is required for currency style");const i={style:"currency",currency:n,currencyDisplay:e.currencyDisplay??"symbol",currencySign:e.currencySign??"standard"};switch(t){case"short":i.notation="compact",i.compactDisplay="short";break;case"standard":break;default:throw new Error("Invalid currencyFormat: "+t)}return i}(e);break;case"percent":t={style:"percent"};break;case"unit":const n=e.unit;if("byte"!==n&&"bit"!==n)throw new Error("invalid unit name");t={style:"unit",unitDisplay:"short",unit:n};break;default:throw new Error("Invalid format style")}return void 0!==e.signDisplay&&(t.signDisplay=e.signDisplay),t}(e),{defaultMinFractionDigits:i,defaultMaxFractionDigits:r}=function(e,t){const n=new Intl.NumberFormat(t,e).resolvedOptions().minimumFractionDigits??0,i=new Intl.NumberFormat(t,e).resolvedOptions().maximumFractionDigits??0;return{defaultMinFractionDigits:n,defaultMaxFractionDigits:i}}(n,t),o=e.minimumFractionDigits??Math.min(i,e.maximumFractionDigits??Number.MAX_SAFE_INTEGER),a=e.maximumFractionDigits??Math.max(o,r);if(o>a)throw new Error("maximumFractionDigits value is out of range");const s={locale:t,minimumIntegerDigits:e.minimumIntegerDigits??0,minimumFractionDigits:o,maximumFractionDigits:a,useGrouping:e.useGrouping??void 0!==O(n,t,!1),lenientParse:e.lenientParse??"full",roundDuringParse:e.roundDuringParse??!1,roundingMode:e.roundingMode??"HALF_UP",signDisplay:e.signDisplay??"auto",separators:{decimal:e.separators?.decimal??d(t).decimalSeparator,group:e.separators?.group??O(n,t)}};let c;switch(e.style){case"decimal":case void 0:c={style:"decimal",decimalFormat:e.decimalFormat??"standard",virtualKeyboardHint:M(s.useGrouping,s.separators.decimal,s.separators.group),...s};break;case"currency":c={style:"currency",currency:e.currency,currencyFormat:e.currencyFormat??"standard",currencyDisplay:e.currencyDisplay??"symbol",currencySign:e.currencySign??"standard",customCurrencyCode:e.customCurrencyCode,customCurrencySymbol:e.customCurrencySymbol,...s};break;case"percent":c={style:"percent",...s};break;case"unit":c={style:"unit",unit:e.unit,...s}}return{resolved:c,native:n}}function O(e,t,n=!0){const i=n?{...e,useGrouping:!0}:e,r=new Intl.NumberFormat(t,i).formatToParts(1e6);return r.find(e=>"group"===e.type)?.value}function M(e,t,n){return e&&"."===t&&""===n?"number":e||"."!==t?"text":"number"}const x=[1e3,10**6,10**9,10**12,10**15],E=[1e3,10**6,10**9,10**12],A=[1e3,10**6,10**9,10**12],P=["byte","kilobyte","megabyte","gigabyte","terabyte","petabyte"],C=["bit","kilobit","megabit","gigabit","terabit"];function R(e,t,n){const{scaled:i,scaleIndex:r}=W(e,t,x,n);return{scaled:i,unit:P[r]}}function k(e,t,n){const{scaled:i,scaleIndex:r}=W(e,t,E,n);return{scaled:i,unit:C[r]}}function G(e,t){const{scaled:n,scaleIndex:i}=H(e,x,t);return{scaled:n,unit:P[i]}}function L(e,t){const{scaled:n,scaleIndex:i}=H(e,E,t);return{scaled:n,unit:C[i]}}function W(e,t,n,i,r=!1){let{whole:o,negative:a,decimal:s}=e;const c=BigInt(o),u=n.length;let m=-1;for(let e=0;e=BigInt(n[e]);e++,m++);let g=3*(m+1),d=l({whole:o,decimal:s},-g),h=w({whole:d.whole,decimal:d.decimal,negative:a},t(d.decimal.length),i.roundingMode);return m=t[n];n++,o++);let a=-3*(o+1),s=e*10**a,c=parseFloat(i.format(s));return o"currency"===e.type?{...e,value:t}:e)}const n=function(e){return e.filter(e=>"currency"!==e.type)}(e);return function(e){return e.filter(e=>!("literal"===e.type&&""===e.value.trim()))}(n)}(e,n?t.customCurrencyCode:t.customCurrencySymbol)}}return e}function T(e,t,n,i){if(void 0===n&&void 0===i)return e.formatToParts(t);return e.formatToParts(t).map(e=>"group"===e.type?{...e,value:i??e.value}:"decimal"===e.type?{...e,value:n??e.value}:e)}e.BigDecimalStringConverter=class{constructor(e){const i=e??{style:"decimal"},r=n.getMergedNumberPreferencesWithOptions(i);this.options=r,this.locale=this.options.locale??t.getLocale()}format(e){if("string"!=typeof e)throw new Error("the value must be a string");const t=u(e),{native:n,resolved:i}=this._getResolvedAndNativeOptions();switch(i.style){case"decimal":case"currency":const e="compact"===n.notation?"compact":null;return this._format(t,n,e);case"percent":return this._format(t,n,"percent");case"unit":return this._format(t,n,i.unit)}}parse(e){return I(e,this.resolvedOptions(),this.locale)}resolvedOptions(){return this._getResolvedAndNativeOptions().resolved}getHint(){return null}_format(e,t={},n){let i=t;const r=this.resolvedOptions(),{minimumFractionDigits:o,maximumFractionDigits:a}=r;function s(e){return Math.min(Math.max(o,e),a)}let{decimal:c,whole:u,negative:m}=e;if("compact"===n){const{scaled:t}=function(e,t,n){return W(e,t,A,n,!0)}(e,s,r);u=t.whole,c=t.decimal}else if("percent"===n){const t=function(e,t,n){let{whole:i,negative:r,decimal:o}=e;const a=l({whole:i,decimal:o},2);return w({whole:a.whole,decimal:a.decimal,negative:r},n(a.decimal.length),t.roundingMode)}(e,r,s);u=t.whole,c=t.decimal}else if("byte"===n||"bit"===n){const t="byte"===n?R:k,{scaled:o,unit:a}=t(e,s,r);u=o.whole,c=o.decimal,i={...i,unit:a}}else{const e=s(c.length);if(e0||i>0?Math.max(i,1):0},c=T(new Intl.NumberFormat(this.locale,s),n?-1:1,o?.separators?.decimal,o?.separators?.group),u={minimumIntegerDigits:o.minimumIntegerDigits};u.useGrouping=o.useGrouping;const l=this._formatWithCustomSeparators(new Intl.NumberFormat(this.locale,u),BigInt(e));let m;if(a>0){const e=this._getFractionPart(t,i),n={useGrouping:!1,minimumIntegerDigits:this._getMinimumIntegerDigitsForFraction(e.length)};m=this._formatDecimalPart(n,e,e.length)}let g="";for(let e of c){const t=e.type;g+="fraction"===t?m??e.value:"integer"===t?l:e.value}return g}_formatWithCustomSeparators(e,t){const n=this.options?.separators?.decimal,i=this.options?.separators?.group;if(void 0===n&&void 0===i)return e.format(t);const r=T(e,t,n,i);let o="";for(let e of r)o+=e.value;return o}_getFractionPart(e,t){const n=t-e.length;return n>0&&e.length>0?e+"0".repeat(n):e}_getMinimumIntegerDigitsForFraction(e){return Math.min(e,20)}_formatDecimalPart(e,t,n){let i=new Intl.NumberFormat(this.locale,e).format(BigInt(t));const r=n-i.length;if(r>0){i=new Intl.NumberFormat(this.locale).format(0).repeat(r)+i}return i}_getResolvedAndNativeOptions(){return this.cachedResolvedAndNativeOptions||(this.cachedResolvedAndNativeOptions=S(this.options,this.locale)),this.cachedResolvedAndNativeOptions}},e.NumberConverter=class{constructor(e){const i=e??{style:"decimal"},r=n.getMergedNumberPreferencesWithOptions(i);this.options=r,this.locale=this.options.locale??t.getLocale()}format(e){let t=e;if("number"!=typeof t)throw new Error("the value must be a number");const{resolved:n,native:i}=this._getResolvedAndNativeOptions(),r=this.options,o={...i,minimumIntegerDigits:r.minimumIntegerDigits,minimumFractionDigits:r.minimumFractionDigits,maximumFractionDigits:r.maximumFractionDigits,useGrouping:r.useGrouping,roundingMode:F(n)};if("unit"===n.style){const i="bit"===n.unit?L:G,{scaled:a,unit:s}=i(e,r);t=a,o.unit=s}const a=new Intl.NumberFormat(this.locale,o),s=r?.separators?.decimal,c=r?.separators?.group;let u=T(a,t,s,c);u=B(u,r);let l="";for(let e of u)l+=`${e.value}`;return l}parse(e){return new Number(I(e,this.resolvedOptions(),this.locale)).valueOf()}resolvedOptions(){return this._getResolvedAndNativeOptions().resolved}getHint(){return null}_getResolvedAndNativeOptions(){return this.cachedResolvedAndNativeOptions||(this.cachedResolvedAndNativeOptions=S(this.options,this.locale)),this.cachedResolvedAndNativeOptions}},e.USER_INPUT_ERROR="userInput",Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=ojconverter-nativenumber.js.map \ No newline at end of file diff --git a/dist/types/ojconverter-nativenumber/types.d.ts b/dist/types/ojconverter-nativenumber/types.d.ts index 05465e0e..a1ec13a2 100644 --- a/dist/types/ojconverter-nativenumber/types.d.ts +++ b/dist/types/ojconverter-nativenumber/types.d.ts @@ -5,6 +5,7 @@ type Separators = Partial<{ decimal: string; group: string; }>; +export type SignDisplay = 'auto' | 'always' | 'exceptZero' | 'negative' | 'never'; type SharedConverterOptions = Partial<{ minimumIntegerDigits: number; minimumFractionDigits: number; @@ -12,6 +13,7 @@ type SharedConverterOptions = Partial<{ useGrouping: boolean; roundingMode: RoundingMode; roundDuringParse: boolean; + signDisplay: SignDisplay; separators: Separators; lenientParse: 'full' | 'none'; locale: BCP47Locale;