@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
6 lines • 7.08 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import{c as t,a as n}from"./p-68f223ec.js";import{n as e}from"./p-4929e834.js";import{c as s}from"./p-c8e46b02.js";const i=new RegExp("\\.(0+)?$"),r=new RegExp("0+$");class a{constructor(t){if(t instanceof a)return t;const[n,e]=function(t){const n=t.split(/[eE]/);if(1===n.length)return t;const e=+t;if(Number.isSafeInteger(e))return`${e}`;const s="-"===t.charAt(0),r=+n[1],a=n[0].split("."),u=(s?a[0].substring(1):a[0])||"",o=a[1]||"",h=r>0?`${u}${((t,n)=>{const e=n>t.length?`${t}${"0".repeat(n-t.length)}`:t;return`${e.slice(0,n)}.${e.slice(n)}`})(o,r)}`:`${((t,n)=>{const e=Math.abs(n)-t.length,s=e>0?`${"0".repeat(e)}${t}`:t;return`${s.slice(0,n)}.${s.slice(n)}`})(u,r)}${o}`;return`${s?"-":""}${"."===h.charAt(0)?"0":""}${h.replace(i,"").replace(c,"")}`}(t).split(".").concat("");this.value=BigInt(n+e.padEnd(a.DECIMALS,"0").slice(0,a.DECIMALS))+BigInt(a.ROUNDED&&e[a.DECIMALS]>="5"),this.isNegative="-"===t.charAt(0)}getIntegersAndDecimals(){const t=this.value.toString().replace("-","").padStart(a.DECIMALS+1,"0");return{integers:t.slice(0,-a.DECIMALS),decimals:t.slice(-a.DECIMALS).replace(r,"")}}toString(){const{integers:t,decimals:n}=this.getIntegersAndDecimals();return`${this.isNegative?"-":""}${t}${n.length?"."+n:""}`}formatToParts(t){const{integers:n,decimals:e}=this.getIntegersAndDecimals(),s=t.numberFormatter.formatToParts(BigInt(n));return this.isNegative&&s.unshift({type:"minusSign",value:t.minusSign}),e.length&&(s.push({type:"decimal",value:t.decimal}),e.split("").forEach((t=>s.push({type:"fraction",value:t})))),s}format(t){const{integers:n,decimals:e}=this.getIntegersAndDecimals();return`${this.isNegative?t.minusSign:""}${t.numberFormatter.format(BigInt(n))}${e.length?`${t.decimal}${e.split("").map((n=>t.numberFormatter.format(Number(n)))).join("")}`:""}`}add(t){return a.fromBigInt(this.value+new a(t).value)}subtract(t){return a.fromBigInt(this.value-new a(t).value)}multiply(t){return a._divRound(this.value*new a(t).value,a.SHIFT)}divide(t){return a._divRound(this.value*a.SHIFT,new a(t).value)}}function u(t){return!(!t||isNaN(Number(t)))}function o(t){return t&&(n=t,e.some((t=>n.includes(t))))?$(t,(t=>{let n=!1;const s=t.split("").filter(((t,s)=>t.match(/\./g)&&!n?(n=!0,!0):!(!t.match(/\-/g)||0!==s)||e.includes(t))).reduce(((t,n)=>t+n));return u(s)?new a(s).toString():""})):"";var n}a.DECIMALS=100,a.ROUNDED=!0,a.SHIFT=BigInt("1"+"0".repeat(a.DECIMALS)),a._divRound=(t,n)=>a.fromBigInt(t/n+(a.ROUNDED?t*BigInt(2)/n%BigInt(2):BigInt(0))),a.fromBigInt=t=>Object.assign(Object.create(a.prototype),{value:t,isNegative:t<BigInt(0)});const c=/^([-0])0+(?=\d)/,h=/(?!^\.)\.$/,l=/(?!^-)-/g,g=/^-\b0\b\.?0*$/,m=t=>$(t,(t=>{const n=t.replace(l,"").replace(h,"").replace(c,"$1");return u(n)?g.test(n)?n:new a(n).toString():t}));function $(t,n){if(!t)return t;const e=t.toLowerCase().indexOf("e")+1;return e?t.replace(/[eE]*$/g,"").substring(0,e).concat(t.slice(e).replace(/[eE]/g,"")).split(/[eE]/).map(((t,e)=>n(1===e?t.replace(/\./g,""):t))).join("e").replace(/^e/,"1e"):n(t)}const f="en",d=["ar","bg","bs","ca","cs","da","de","el",f,"es","et","fi","fr","he","hr","hu","id","it","ja","ko","lt","lv","no","nl","pl","pt-BR","pt-PT","ro","ru","sk","sl","sr","sv","th","tr","uk","vi","zh-CN","zh-HK","zh-TW"],b=["ar","bg","bs","ca","cs","da","de","de-AT","de-CH","el",f,"en-AU","en-CA","en-GB","es","es-MX","et","fi","fr","fr-CH","he","hi","hr","hu","id","it","it-CH","ja","ko","lt","lv","mk","no","nl","pl","pt","pt-PT","ro","ru","sk","sl","sr","sv","th","tr","uk","vi","zh-CN","zh-HK","zh-TW"],p=["arab","arabext","bali","beng","deva","fullwide","gujr","guru","hanidec","khmr","knda","laoo","latn","limb","mlym","mong","mymr","orya","tamldec","telu","thai","tibt"],w=t=>p.includes(t),v=(new Intl.NumberFormat).resolvedOptions().numberingSystem,I="arab"!==v&&w(v)?v:"latn",B=t=>w(t)?t:I;function E(t,n="cldr"){const e="cldr"===n?b:d;return t?e.includes(t)?t:"nb"===(t=t.toLowerCase())?"no":"t9n"===n&&"pt"===t?"pt-BR":(t.includes("-")&&(t=t.replace(/(\w+)-(\w+)/,((t,n,e)=>`${n}-${e.toUpperCase()}`)),e.includes(t)||(t=t.split("-")[0])),"zh"===t?"zh-CN":e.includes(t)?t:(console.warn(`Translations for the "${t}" locale are not available and will fall back to the default, English (en).`),f)):f}const N=new Set;function j(n){!function(n){n.effectiveLocale=function(n){return n.el.lang||t(n.el,"[lang]")?.lang||document.documentElement.lang||f}(n)}(n),0===N.size&&x?.observe(document.documentElement,{attributes:!0,attributeFilter:["lang"],subtree:!0}),N.add(n)}function k(t){N.delete(t),0===N.size&&x.disconnect()}const x=s("mutation",(e=>{e.forEach((e=>{const s=e.target;N.forEach((e=>{if(!n(s,e.el))return;const i=t(e.el,"[lang]");if(!i)return void(e.effectiveLocale=f);const r=i.lang;e.effectiveLocale=i.hasAttribute("lang")&&""===r?f:r}))}))})),y=new class{constructor(){this.delocalize=t=>this._numberFormatOptions?$(t,(t=>t.replace(new RegExp(`[${this._minusSign}]`,"g"),"-").replace(new RegExp(`[${this._group}]`,"g"),"").replace(new RegExp(`[${this._decimal}]`,"g"),".").replace(new RegExp(`[${this._digits.join("")}]`,"g"),this._getDigitIndex))):t,this.localize=t=>this._numberFormatOptions?$(t,(t=>u(t.trim())?new a(t.trim()).format(this).replace(new RegExp(`[${this._actualGroup}]`,"g"),this._group):t)):t}get group(){return this._group}get decimal(){return this._decimal}get minusSign(){return this._minusSign}get digits(){return this._digits}get numberFormatter(){return this._numberFormatter}get numberFormatOptions(){return this._numberFormatOptions}set numberFormatOptions(t){if(t.locale=E(t?.locale),t.numberingSystem=B(t?.numberingSystem),!this._numberFormatOptions&&t.locale===f&&t.numberingSystem===I&&2===Object.keys(t).length||JSON.stringify(this._numberFormatOptions)===JSON.stringify(t))return;this._numberFormatOptions=t,this._numberFormatter=new Intl.NumberFormat(this._numberFormatOptions.locale,this._numberFormatOptions),this._digits=[...new Intl.NumberFormat(this._numberFormatOptions.locale,{useGrouping:!1,numberingSystem:this._numberFormatOptions.numberingSystem}).format(9876543210)].reverse();const n=new Map(this._digits.map(((t,n)=>[t,n]))),e=new Intl.NumberFormat(this._numberFormatOptions.locale,{numberingSystem:this._numberFormatOptions.numberingSystem}).formatToParts(-12345678.9);this._actualGroup=e.find((t=>"group"===t.type)).value,this._group=0===this._actualGroup.trim().length?" ":this._actualGroup,this._decimal=e.find((t=>"decimal"===t.type)).value,this._minusSign=e.find((t=>"minusSign"===t.type)).value,this._getDigitIndex=t=>n.get(t)}};let R,S;function z(t,n){t=E(t),R||(R=new Map),S!==t&&(R.clear(),S=t);const e=function(t={}){return Object.entries(t).sort((([t],[n])=>t.localeCompare(n))).map((t=>`${t[0]}-${t[1]}`)).flat().join(":")}(n),s=R.get(e);if(s)return s;const i=new Intl.DateTimeFormat(t,n);return R.set(e,i),i}export{a as B,I as a,z as b,j as c,k as d,B as e,E as g,u as i,y as n,o as p,m as s}