@webority/mobile-core 0.190.1 → 0.191.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseHundredthMinor=exports.formatTime=exports.formatRelative=exports.formatPercent=exports.formatNumber=exports.formatMinor=exports.formatHundredthMinor=exports.formatDateTime=exports.formatDate=void 0;var _formatConfig=require("./formatConfig.js"),_intlCache=require("./intlCache.js");const MONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],IST_OFFSET_MINUTES=330,IST_TIME_ZONE="Asia/Kolkata",IST_NAMES=["IST",IST_TIME_ZONE,"Asia/Calcutta"],DATE_ONLY=/^\d{4}-\d{2}-\d{2}$/,pad2=t=>t<10?`0${t}`:`${t}`,emptyOf=t=>t??(0,_formatConfig.getFormatterConfig)().empty,toInstant=t=>{if(null==t||""===t)return null;const e=t instanceof Date?t:new Date("string"==typeof t?t.trim():t);return Number.isNaN(e.getTime())?null:e},isDateOnly=t=>"string"==typeof t&&DATE_ONLY.test(t.trim()),gmtLabel=t=>{if(0===t)return"GMT";const e=t>0?"+":"-",r=Math.abs(t),n=r%60;return`GMT${e}${Math.floor(r/60)}${0===n?"":`:${pad2(n)}`}`},shifted=(t,e,r)=>{const n=new Date(t.getTime()+6e4*e);return{year:n.getUTCFullYear(),month:n.getUTCMonth(),day:n.getUTCDate(),hour:n.getUTCHours(),minute:n.getUTCMinutes(),label:r}},inIanaZone=(t,e)=>{const r=(0,_intlCache.dateTimeFormat)("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",hour12:!1}).format(t),n=r.match(/\d+/g)??[];if(5!==n.length)throw new Error(`Unexpected date text from Intl for zone ${e}: ${r}`);const[o="",i="",a="",m="",s=""]=n,l=Number.parseInt(a,10),c=Number.parseInt(o,10)-1,u=Number.parseInt(i,10),f=Number.parseInt(m,10)%24,g=Number.parseInt(s,10),h=Date.UTC(l,c,u,f,g),d=Math.round((h-6e4*Math.floor(t.getTime()/6e4))/6e4);return{year:l,month:c,day:u,hour:f,minute:g,label:gmtLabel(d)}},wallClock=(t,e)=>IST_NAMES.includes(e)?shifted(t,330,"IST"):"UTC"===e?shifted(t,0,"UTC"):"viewer"===e?{year:t.getFullYear(),month:t.getMonth(),day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),label:gmtLabel(-t.getTimezoneOffset())}:inIanaZone(t,e),hourCycleOf=t=>t.hourCycle??12,dayMonthYear=t=>`${pad2(t.day)}-${MONTHS[t.month]}-${t.year}`,numericDate=(t,e)=>{const r=pad2(t.day),n=pad2(t.month+1),o=String(t.year).padStart(4,"0");return"DayMonthYear"===e?`${r}/${n}/${o}`:"MonthDayYear"===e?`${n}/${r}/${o}`:`${o}-${n}-${r}`},clockTime=(t,e)=>24===e?`${pad2(t.hour)}:${pad2(t.minute)}`:`${t.hour%12||12}:${pad2(t.minute)} ${t.hour<12?"AM":"PM"}`,labelled=(t,e)=>e??!IST_NAMES.includes(t),withLabel=(t,e,r,n)=>labelled(r,n)?`${t} ${e.label}`:t,intlZone=t=>{if("viewer"!==t)return IST_NAMES.includes(t)?IST_TIME_ZONE:t},digitSets=new Map,localeDigits=t=>{const e=digitSets.get(t);if(void 0!==e)return e;const r=new Intl.NumberFormat(t,{useGrouping:!1}),n=new Set;for(let t=0;t<=9;t++)n.add(r.format(t));return digitSets.set(t,n),n},digitsIn=(t,e)=>Array.from(t).filter(t=>e.has(t)).join(""),hasFormatToParts=t=>"function"==typeof t.formatToParts,localeDayMonthYear=(t,e,r)=>{const n=(0,_intlCache.dateTimeFormat)(e,{day:"2-digit",month:"short",year:"numeric",timeZone:r});if(!hasFormatToParts(n)){const n=localeDigits(e);return`${digitsIn((0,_intlCache.dateTimeFormat)(e,{day:"2-digit",timeZone:r}).format(t),n)}-${(0,_intlCache.dateTimeFormat)(e,{month:"short",timeZone:r}).format(t).trim().replace(".","")}-${digitsIn((0,_intlCache.dateTimeFormat)(e,{year:"numeric",timeZone:r}).format(t),n)}`}const o=n.formatToParts(t),part=t=>o.find(e=>e.type===t)?.value??"";return`${part("day")}-${part("month").replace(".","")}-${part("year")}`},dateText=(t,e)=>{const r=(0,_formatConfig.getFormatterConfig)().dateOrder??"DayMonthNameYear";if("DayMonthNameYear"!==r)return numericDate(wallClock(t,e),r);const n=(0,_formatConfig.configuredLocale)();return n?localeDayMonthYear(t,n,intlZone(e)):dayMonthYear(wallClock(t,e))},localeClock=(t,e,r,n)=>(0,_intlCache.dateTimeFormat)(e,{hour:"2-digit",minute:"2-digit",timeZone:intlZone(r),timeZoneName:labelled(r,n.zoneLabel)?"short":void 0,hourCycle:void 0===n.hourCycle?void 0:24===n.hourCycle?"h23":"h12"}).format(t),formatDate=(t,e={})=>{const r=toInstant(t);return r?dateText(r,isDateOnly(t)?"UTC":e.zone??(0,_formatConfig.getFormatterConfig)().zone):emptyOf(e.empty)};exports.formatDate=formatDate;const formatTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??(0,_formatConfig.getFormatterConfig)().zone,o=(0,_formatConfig.configuredLocale)();if(o)return localeClock(r,o,n,e);const i=wallClock(r,n);return withLabel(clockTime(i,hourCycleOf(e)),i,n,e.zoneLabel)};exports.formatTime=formatTime;const formatDateTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??(0,_formatConfig.getFormatterConfig)().zone,o=(0,_formatConfig.configuredLocale)();if(o)return`${dateText(r,n)}, ${localeClock(r,o,n,e)}`;const i=wallClock(r,n);return withLabel(`${dateText(r,n)}, ${clockTime(i,hourCycleOf(e))}`,i,n,e.zoneLabel)};exports.formatDateTime=formatDateTime;const SECOND=1e3,MINUTE=6e4,HOUR=36e5,DAY=24*HOUR,WEEK=7*DAY,MONTH=30*DAY,YEAR=365*DAY,INTL_UNITS=[["year",YEAR],["month",MONTH],["day",DAY],["hour",HOUR],["minute",6e4]],formatRelative=(t,e={})=>{const r=toInstant(t),n=toInstant(e.now??Date.now());if(!r||!n)return emptyOf(e.empty);const o=r.getTime()-n.getTime(),i=Math.abs(o),a=(0,_formatConfig.configuredLocale)();if(null!==a||!0===e.intl){const t=(0,_intlCache.relativeTimeFormat)(Intl.RelativeTimeFormat,a??(0,_formatConfig.getFormatterConfig)().locale),[e,r]=INTL_UNITS.find(([,t])=>i>=t)??["second",1e3];return t.format(Math.round(o/r),e)}if(i<6e4)return"just now";const[m,s]=i<HOUR?["minute",6e4]:i<DAY?["hour",HOUR]:i<WEEK?["day",DAY]:i<MONTH?["week",WEEK]:i<YEAR?["month",MONTH]:["year",YEAR],l=Math.floor(i/s),c=`${l} ${m}${1===l?"":"s"}`;return o<0?`${c} ago`:`in ${c}`};exports.formatRelative=formatRelative;const toNumber=t=>{if(null==t)return null;if("string"==typeof t&&""===t.trim())return null;const e="number"==typeof t?t:Number(t);return Number.isFinite(e)?e:null},clampDigits=t=>Math.min(20,Math.max(0,Math.trunc(t))),shiftDecimal=(t,e)=>/e/i.test(String(t))?t*10**e:Number(`${t}e${e}`),houseSeparators=()=>(0,_formatConfig.numberSeparators)()??{decimal:".",group:","},groupInteger=(t,e,r)=>{if("none"===e||t.length<=3)return t;if("international"===e)return t.replace(/\B(?=(\d{3})+(?!\d))/g,r);return`${t.slice(0,-3).replace(/\B(?=(\d{2})+(?!\d))/g,r)}${r}${t.slice(-3)}`},plainDigits=(t,e)=>(0,_intlCache.numberFormat)("en-US",{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:e}).format(t),decimalText=(t,e,r,n)=>{const o=Math.max(clampDigits(r),clampDigits(e)),i=clampDigits(e),a=plainDigits(Math.abs(t),o),[m="0",s=""]=a.split(".");let l=s.padEnd(i,"0");for(;l.length>i&&l.endsWith("0");)l=l.slice(0,-1);const c=houseSeparators(),u=groupInteger(m,n,c.group)+(l?`${c.decimal}${l}`:"");return t<0&&/[1-9]/.test(a)?`-${u}`:u},HOUSE_NUMBER_KEYS=new Set(["grouping","minimumFractionDigits","maximumFractionDigits","locale","empty","scale"]),intlNumberOptions=t=>{if(!Object.entries(t).some(([t,e])=>void 0!==e&&!HOUSE_NUMBER_KEYS.has(t)))return null;const{grouping:e,locale:r,empty:n,scale:o,...i}=t;return"none"===e&&void 0===i.useGrouping?{...i,useGrouping:!1}:i},localeOf=t=>t.locale??(0,_formatConfig.getFormatterConfig)().locale,intlTextLocale=t=>{const e=(0,_formatConfig.configuredLocale)();return null===e||"indian"===t.grouping||"international"===t.grouping?null:t.locale??e},fractionDigits=(t,e)=>{const r=clampDigits(t);return{minimumFractionDigits:r,maximumFractionDigits:Math.max(r,clampDigits(e))}},groupingOption=t=>"none"===t?{useGrouping:!1}:{},formatNumber=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n)return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(localeOf(e),n),r);const o=intlTextLocale(e);return o?(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(o,{...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??3),...groupingOption(e.grouping)}),r):decimalText(r,e.minimumFractionDigits??0,e.maximumFractionDigits??3,e.grouping??(0,_formatConfig.groupingForLocale)(localeOf(e)))};exports.formatNumber=formatNumber;const formatPercent=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n){const t="percent"===e.scale?shiftDecimal(r,-2):r;return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(localeOf(e),{style:"percent",...n}),t)}const o=intlTextLocale(e);if(o){const t="percent"===e.scale?shiftDecimal(r,-2):r;return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(o,{style:"percent",...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0),...groupingOption(e.grouping)}),t)}const i="percent"===e.scale?r:shiftDecimal(r,2);return`${decimalText(i,e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0,e.grouping??(0,_formatConfig.groupingForLocale)(localeOf(e)))}%`};exports.formatPercent=formatPercent;const SYMBOLS={INR:"₹",USD:"$",EUR:"€",GBP:"£"},MINOR_DIGITS={JPY:0,KRW:0,VND:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3},formatMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=Math.round(Math.abs(r)),a=String(i).padStart(o+1,"0"),m=a.slice(0,a.length-o);let s=a.slice(a.length-o);e.dropZeroDecimals&&/^0*$/.test(s)&&(s="");const l=intlTextLocale({grouping:e.grouping});if(l)try{const t=Number(`${r<0&&0!==i?"-":""}${m}.${a.slice(a.length-o)||"0"}`);return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(l,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(s?o:0,o),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatMinor rejected currency "${n}" or locale "${l}": ${t.message}`,{cause:t});throw t}const c=e.grouping??("INR"===n?"indian":"international"),u=houseSeparators(),f=groupInteger(m,c,u.group)+(s?`${u.decimal}${s}`:""),g=r<0&&0!==i?"-":"";if(!1===e.symbol)return`${g}${f}`;const h=SYMBOLS[n];return h?`${g}${h}${f}`:`${g}${n} ${f}`};exports.formatMinor=formatMinor;const HUNDREDTH_EXTRA_DIGITS=2,RUPEE_SIGN="₹",trimHundredths=(t,e)=>{let r=t.length;for(;r>e&&"0"===t.slice(r-1,r);)r-=1;return t.slice(0,r)},formatHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);if(!Number.isSafeInteger(r))throw new RangeError(`formatHundredthMinor needs a whole number of hundredths within the safe integer range, got ${r}.`);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=o+2,a=String(Math.abs(r)).padStart(i+1,"0"),m=a.slice(a.length-i),s=a.slice(0,a.length-i);let l=trimHundredths(m,o);e.dropZeroDecimals&&/^0*$/.test(l)&&(l="");const c=r<0?"-":"",u=intlTextLocale({grouping:e.grouping});if(u)try{const t=Number(`${c}${s}.${m}`);return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(u,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(l.length,i),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatHundredthMinor rejected currency "${n}" or locale "${u}": ${t.message}`,{cause:t});throw t}const f=e.grouping??("INR"===n?"indian":"international"),g=houseSeparators(),h=groupInteger(s,f,g.group)+(l?`${g.decimal}${l}`:"");if(!1===e.symbol)return`${c}${h}`;const d=SYMBOLS[n];return d?`${c}${d}${h}`:`${c}${n} ${h}`};exports.formatHundredthMinor=formatHundredthMinor;const wholeDigitsOf=(t,e)=>{let r="",n=!0;for(const o of t)if(e.includes(o)){if(n)return null;n=!0}else{if(!/\d/.test(o))return null;r+=o,n=!1}return n&&""!==t?null:r},parseHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('parseHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=e.currency.toUpperCase(),n=(MINOR_DIGITS[r]??2)+2,refuse=e=>new RangeError(`parseHundredthMinor cannot read "${t}": ${e}`);let o=t.trim();if(""===o)throw refuse("the text is empty");let i=!1;o.startsWith("-")&&(i=!0,o=o.slice(1).trim());const a=["₹",SYMBOLS[r]];for(const t of a)if(t&&o.startsWith(t)){o=o.slice(t.length).trim();break}const m=houseSeparators(),s=[",",m.group].filter(t=>t!==m.decimal),l=o.split(m.decimal);if(l.length>2)throw refuse("more than one decimal separator");const c=wholeDigitsOf(l[0]??"",s);if(null===c)throw refuse("a character that is not a digit, a group separator or the decimal separator");const u=2===l.length,f=l[1]??"";if(u&&!/^\d+$/.test(f))throw refuse("the decimal part must be one or more digits");if(f.length>n)throw refuse(`more than ${n} decimal places, the most this currency takes`);const g=c+f.padEnd(n,"0");if(""===g)throw refuse("it has no digits");const h=Number(g);if(!Number.isSafeInteger(h))throw refuse("the amount is beyond the safe integer range");return i&&0!==h?-h:h};exports.parseHundredthMinor=parseHundredthMinor;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseHundredthMinor=exports.formatTime=exports.formatRelative=exports.formatPercent=exports.formatNumber=exports.formatMinor=exports.formatHundredthMinor=exports.formatDateTime=exports.formatDate=void 0;var _formatConfig=require("./formatConfig.js"),_intlCache=require("./intlCache.js");const MONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],IST_OFFSET_MINUTES=330,IST_TIME_ZONE="Asia/Kolkata",IST_NAMES=["IST",IST_TIME_ZONE,"Asia/Calcutta"],DATE_ONLY=/^\d{4}-\d{2}-\d{2}$/,pad2=t=>t<10?`0${t}`:`${t}`,emptyOf=t=>t??(0,_formatConfig.getFormatterConfig)().empty,toInstant=t=>{if(null==t||""===t)return null;const e=t instanceof Date?t:new Date("string"==typeof t?t.trim():t);return Number.isNaN(e.getTime())?null:e},isDateOnly=t=>"string"==typeof t&&DATE_ONLY.test(t.trim()),gmtLabel=t=>{if(0===t)return"GMT";const e=t>0?"+":"-",r=Math.abs(t),n=r%60;return`GMT${e}${Math.floor(r/60)}${0===n?"":`:${pad2(n)}`}`},shifted=(t,e,r)=>{const n=new Date(t.getTime()+6e4*e);return{year:n.getUTCFullYear(),month:n.getUTCMonth(),day:n.getUTCDate(),hour:n.getUTCHours(),minute:n.getUTCMinutes(),label:r}},inIanaZone=(t,e)=>{const r=(0,_intlCache.dateTimeFormat)("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",hour12:!1}).format(t),n=r.match(/\d+/g)??[];if(5!==n.length)throw new Error(`Unexpected date text from Intl for zone ${e}: ${r}`);const[o="",i="",a="",m="",s=""]=n,l=Number.parseInt(a,10),c=Number.parseInt(o,10)-1,u=Number.parseInt(i,10),f=Number.parseInt(m,10)%24,g=Number.parseInt(s,10),h=Date.UTC(l,c,u,f,g),p=Math.round((h-6e4*Math.floor(t.getTime()/6e4))/6e4);return{year:l,month:c,day:u,hour:f,minute:g,label:gmtLabel(p)}},wallClock=(t,e)=>IST_NAMES.includes(e)?shifted(t,330,"IST"):"UTC"===e?shifted(t,0,"UTC"):"viewer"===e?{year:t.getFullYear(),month:t.getMonth(),day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),label:gmtLabel(-t.getTimezoneOffset())}:inIanaZone(t,e),hourCycleOf=t=>t.hourCycle??12,dayMonthYear=t=>`${pad2(t.day)}-${MONTHS[t.month]}-${t.year}`,numericDate=(t,e)=>{const r=pad2(t.day),n=pad2(t.month+1),o=String(t.year).padStart(4,"0");return"DayMonthYear"===e?`${r}/${n}/${o}`:"MonthDayYear"===e?`${n}/${r}/${o}`:`${o}-${n}-${r}`},clockTime=(t,e)=>24===e?`${pad2(t.hour)}:${pad2(t.minute)}`:`${t.hour%12||12}:${pad2(t.minute)} ${t.hour<12?"AM":"PM"}`,labelled=(t,e)=>e??!IST_NAMES.includes(t),withLabel=(t,e,r,n)=>labelled(r,n)?`${t} ${e.label}`:t,intlZone=t=>{if("viewer"!==t)return IST_NAMES.includes(t)?IST_TIME_ZONE:t},digitSets=new Map,localeDigits=t=>{const e=digitSets.get(t);if(void 0!==e)return e;const r=new Intl.NumberFormat(t,{useGrouping:!1}),n=new Set;for(let t=0;t<=9;t++)n.add(r.format(t));return digitSets.set(t,n),n},digitsIn=(t,e)=>Array.from(t).filter(t=>e.has(t)).join(""),hasFormatToParts=t=>"function"==typeof t.formatToParts,localeDayMonthYear=(t,e,r)=>{const n=(0,_intlCache.dateTimeFormat)(e,{day:"2-digit",month:"short",year:"numeric",timeZone:r});if(!hasFormatToParts(n)){const n=localeDigits(e);return`${digitsIn((0,_intlCache.dateTimeFormat)(e,{day:"2-digit",timeZone:r}).format(t),n)}-${(0,_intlCache.dateTimeFormat)(e,{month:"short",timeZone:r}).format(t).trim().replace(".","")}-${digitsIn((0,_intlCache.dateTimeFormat)(e,{year:"numeric",timeZone:r}).format(t),n)}`}const o=n.formatToParts(t),part=t=>o.find(e=>e.type===t)?.value??"";return`${part("day")}-${part("month").replace(".","")}-${part("year")}`},dateText=(t,e)=>{const r=(0,_formatConfig.getFormatterConfig)().dateOrder??"DayMonthNameYear";if("DayMonthNameYear"!==r)return numericDate(wallClock(t,e),r);const n=(0,_formatConfig.configuredLocale)();return n?localeDayMonthYear(t,n,intlZone(e)):dayMonthYear(wallClock(t,e))},localeClock=(t,e,r,n)=>(0,_intlCache.dateTimeFormat)(e,{hour:"2-digit",minute:"2-digit",timeZone:intlZone(r),timeZoneName:labelled(r,n.zoneLabel)?"short":void 0,hourCycle:void 0===n.hourCycle?void 0:24===n.hourCycle?"h23":"h12"}).format(t),formatDate=(t,e={})=>{const r=toInstant(t);return r?dateText(r,isDateOnly(t)?"UTC":e.zone??(0,_formatConfig.getFormatterConfig)().zone):emptyOf(e.empty)};exports.formatDate=formatDate;const formatTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??(0,_formatConfig.getFormatterConfig)().zone,o=(0,_formatConfig.configuredLocale)();if(o)return localeClock(r,o,n,e);const i=wallClock(r,n);return withLabel(clockTime(i,hourCycleOf(e)),i,n,e.zoneLabel)};exports.formatTime=formatTime;const formatDateTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??(0,_formatConfig.getFormatterConfig)().zone,o=(0,_formatConfig.configuredLocale)();if(o)return`${dateText(r,n)}, ${localeClock(r,o,n,e)}`;const i=wallClock(r,n);return withLabel(`${dateText(r,n)}, ${clockTime(i,hourCycleOf(e))}`,i,n,e.zoneLabel)};exports.formatDateTime=formatDateTime;const SECOND=1e3,MINUTE=6e4,HOUR=36e5,DAY=24*HOUR,WEEK=7*DAY,MONTH=30*DAY,YEAR=365*DAY,INTL_UNITS=[["year",YEAR],["month",MONTH],["day",DAY],["hour",HOUR],["minute",6e4]],formatRelative=(t,e={})=>{const r=toInstant(t),n=toInstant(e.now??Date.now());if(!r||!n)return emptyOf(e.empty);const o=r.getTime()-n.getTime(),i=Math.abs(o),a=(0,_formatConfig.configuredLocale)();if((null!==a||!0===e.intl)&&"function"==typeof Intl.RelativeTimeFormat){const t=(0,_intlCache.relativeTimeFormat)(Intl.RelativeTimeFormat,a??(0,_formatConfig.getFormatterConfig)().locale),[e,r]=INTL_UNITS.find(([,t])=>i>=t)??["second",1e3];return t.format(Math.round(o/r),e)}if(i<6e4)return"just now";const[m,s]=i<HOUR?["minute",6e4]:i<DAY?["hour",HOUR]:i<WEEK?["day",DAY]:i<MONTH?["week",WEEK]:i<YEAR?["month",MONTH]:["year",YEAR],l=Math.floor(i/s),c=`${l} ${m}${1===l?"":"s"}`;return o<0?`${c} ago`:`in ${c}`};exports.formatRelative=formatRelative;const toNumber=t=>{if(null==t)return null;if("string"==typeof t&&""===t.trim())return null;const e="number"==typeof t?t:Number(t);return Number.isFinite(e)?e:null},clampDigits=t=>Math.min(20,Math.max(0,Math.trunc(t))),shiftDecimal=(t,e)=>/e/i.test(String(t))?t*10**e:Number(`${t}e${e}`),houseSeparators=()=>(0,_formatConfig.numberSeparators)()??{decimal:".",group:","},groupInteger=(t,e,r)=>{if("none"===e||t.length<=3)return t;if("international"===e)return t.replace(/\B(?=(\d{3})+(?!\d))/g,r);return`${t.slice(0,-3).replace(/\B(?=(\d{2})+(?!\d))/g,r)}${r}${t.slice(-3)}`},plainDigits=(t,e)=>(0,_intlCache.numberFormat)("en-US",{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:e}).format(t),decimalText=(t,e,r,n)=>{const o=Math.max(clampDigits(r),clampDigits(e)),i=clampDigits(e),a=plainDigits(Math.abs(t),o),[m="0",s=""]=a.split(".");let l=s.padEnd(i,"0");for(;l.length>i&&l.endsWith("0");)l=l.slice(0,-1);const c=houseSeparators(),u=groupInteger(m,n,c.group)+(l?`${c.decimal}${l}`:"");return t<0&&/[1-9]/.test(a)?`-${u}`:u},HOUSE_NUMBER_KEYS=new Set(["grouping","minimumFractionDigits","maximumFractionDigits","locale","empty","scale"]),intlNumberOptions=t=>{if(!Object.entries(t).some(([t,e])=>void 0!==e&&!HOUSE_NUMBER_KEYS.has(t)))return null;const{grouping:e,locale:r,empty:n,scale:o,...i}=t;return"none"===e&&void 0===i.useGrouping?{...i,useGrouping:!1}:i},localeOf=t=>t.locale??(0,_formatConfig.getFormatterConfig)().locale,intlTextLocale=t=>{const e=(0,_formatConfig.configuredLocale)();return null===e||"indian"===t.grouping||"international"===t.grouping?null:t.locale??e},fractionDigits=(t,e)=>{const r=clampDigits(t);return{minimumFractionDigits:r,maximumFractionDigits:Math.max(r,clampDigits(e))}},groupingOption=t=>"none"===t?{useGrouping:!1}:{},formatNumber=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n)return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(localeOf(e),n),r);const o=intlTextLocale(e);return o?(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(o,{...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??3),...groupingOption(e.grouping)}),r):decimalText(r,e.minimumFractionDigits??0,e.maximumFractionDigits??3,e.grouping??(0,_formatConfig.groupingForLocale)(localeOf(e)))};exports.formatNumber=formatNumber;const formatPercent=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n){const t="percent"===e.scale?shiftDecimal(r,-2):r;return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(localeOf(e),{style:"percent",...n}),t)}const o=intlTextLocale(e);if(o){const t="percent"===e.scale?shiftDecimal(r,-2):r;return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(o,{style:"percent",...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0),...groupingOption(e.grouping)}),t)}const i="percent"===e.scale?r:shiftDecimal(r,2);return`${decimalText(i,e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0,e.grouping??(0,_formatConfig.groupingForLocale)(localeOf(e)))}%`};exports.formatPercent=formatPercent;const SYMBOLS={INR:"₹",USD:"$",EUR:"€",GBP:"£"},MINOR_DIGITS={JPY:0,KRW:0,VND:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3},formatMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=Math.round(Math.abs(r)),a=String(i).padStart(o+1,"0"),m=a.slice(0,a.length-o);let s=a.slice(a.length-o);e.dropZeroDecimals&&/^0*$/.test(s)&&(s="");const l=intlTextLocale({grouping:e.grouping});if(l)try{const t=Number(`${r<0&&0!==i?"-":""}${m}.${a.slice(a.length-o)||"0"}`);return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(l,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(s?o:0,o),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatMinor rejected currency "${n}" or locale "${l}": ${t.message}`,{cause:t});throw t}const c=e.grouping??("INR"===n?"indian":"international"),u=houseSeparators(),f=groupInteger(m,c,u.group)+(s?`${u.decimal}${s}`:""),g=r<0&&0!==i?"-":"";if(!1===e.symbol)return`${g}${f}`;const h=SYMBOLS[n];return h?`${g}${h}${f}`:`${g}${n} ${f}`};exports.formatMinor=formatMinor;const HUNDREDTH_EXTRA_DIGITS=2,RUPEE_SIGN="₹",trimHundredths=(t,e)=>{let r=t.length;for(;r>e&&"0"===t.slice(r-1,r);)r-=1;return t.slice(0,r)},formatHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);if(!Number.isSafeInteger(r))throw new RangeError(`formatHundredthMinor needs a whole number of hundredths within the safe integer range, got ${r}.`);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=o+2,a=String(Math.abs(r)).padStart(i+1,"0"),m=a.slice(a.length-i),s=a.slice(0,a.length-i);let l=trimHundredths(m,o);e.dropZeroDecimals&&/^0*$/.test(l)&&(l="");const c=r<0?"-":"",u=intlTextLocale({grouping:e.grouping});if(u)try{const t=Number(`${c}${s}.${m}`);return(0,_formatConfig.styledNumber)((0,_intlCache.numberFormat)(u,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(l.length,i),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatHundredthMinor rejected currency "${n}" or locale "${u}": ${t.message}`,{cause:t});throw t}const f=e.grouping??("INR"===n?"indian":"international"),g=houseSeparators(),h=groupInteger(s,f,g.group)+(l?`${g.decimal}${l}`:"");if(!1===e.symbol)return`${c}${h}`;const p=SYMBOLS[n];return p?`${c}${p}${h}`:`${c}${n} ${h}`};exports.formatHundredthMinor=formatHundredthMinor;const wholeDigitsOf=(t,e)=>{let r="",n=!0;for(const o of t)if(e.includes(o)){if(n)return null;n=!0}else{if(!/\d/.test(o))return null;r+=o,n=!1}return n&&""!==t?null:r},parseHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('parseHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=e.currency.toUpperCase(),n=(MINOR_DIGITS[r]??2)+2,refuse=e=>new RangeError(`parseHundredthMinor cannot read "${t}": ${e}`);let o=t.trim();if(""===o)throw refuse("the text is empty");let i=!1;o.startsWith("-")&&(i=!0,o=o.slice(1).trim());const a=["₹",SYMBOLS[r]];for(const t of a)if(t&&o.startsWith(t)){o=o.slice(t.length).trim();break}const m=houseSeparators(),s=[",",m.group].filter(t=>t!==m.decimal),l=o.split(m.decimal);if(l.length>2)throw refuse("more than one decimal separator");const c=wholeDigitsOf(l[0]??"",s);if(null===c)throw refuse("a character that is not a digit, a group separator or the decimal separator");const u=2===l.length,f=l[1]??"";if(u&&!/^\d+$/.test(f))throw refuse("the decimal part must be one or more digits");if(f.length>n)throw refuse(`more than ${n} decimal places, the most this currency takes`);const g=c+f.padEnd(n,"0");if(""===g)throw refuse("it has no digits");const h=Number(g);if(!Number.isSafeInteger(h))throw refuse("the amount is beyond the safe integer range");return i&&0!==h?-h:h};exports.parseHundredthMinor=parseHundredthMinor;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";import{configuredLocale,getFormatterConfig,groupingForLocale,numberSeparators,styledNumber}from"./formatConfig.js";import{dateTimeFormat,numberFormat,relativeTimeFormat}from"./intlCache.js";const MONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],IST_OFFSET_MINUTES=330,IST_TIME_ZONE="Asia/Kolkata",IST_NAMES=["IST",IST_TIME_ZONE,"Asia/Calcutta"],DATE_ONLY=/^\d{4}-\d{2}-\d{2}$/,pad2=t=>t<10?`0${t}`:`${t}`,emptyOf=t=>t??getFormatterConfig().empty,toInstant=t=>{if(null==t||""===t)return null;const e=t instanceof Date?t:new Date("string"==typeof t?t.trim():t);return Number.isNaN(e.getTime())?null:e},isDateOnly=t=>"string"==typeof t&&DATE_ONLY.test(t.trim()),gmtLabel=t=>{if(0===t)return"GMT";const e=t>0?"+":"-",r=Math.abs(t),n=r%60;return`GMT${e}${Math.floor(r/60)}${0===n?"":`:${pad2(n)}`}`},shifted=(t,e,r)=>{const n=new Date(t.getTime()+6e4*e);return{year:n.getUTCFullYear(),month:n.getUTCMonth(),day:n.getUTCDate(),hour:n.getUTCHours(),minute:n.getUTCMinutes(),label:r}},inIanaZone=(t,e)=>{const r=dateTimeFormat("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",hour12:!1}).format(t),n=r.match(/\d+/g)??[];if(5!==n.length)throw new Error(`Unexpected date text from Intl for zone ${e}: ${r}`);const[o="",i="",a="",s="",c=""]=n,m=Number.parseInt(a,10),l=Number.parseInt(o,10)-1,u=Number.parseInt(i,10),g=Number.parseInt(s,10)%24,p=Number.parseInt(c,10),d=Date.UTC(m,l,u,g,p),f=Math.round((d-6e4*Math.floor(t.getTime()/6e4))/6e4);return{year:m,month:l,day:u,hour:g,minute:p,label:gmtLabel(f)}},wallClock=(t,e)=>IST_NAMES.includes(e)?shifted(t,330,"IST"):"UTC"===e?shifted(t,0,"UTC"):"viewer"===e?{year:t.getFullYear(),month:t.getMonth(),day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),label:gmtLabel(-t.getTimezoneOffset())}:inIanaZone(t,e),hourCycleOf=t=>t.hourCycle??12,dayMonthYear=t=>`${pad2(t.day)}-${MONTHS[t.month]}-${t.year}`,numericDate=(t,e)=>{const r=pad2(t.day),n=pad2(t.month+1),o=String(t.year).padStart(4,"0");return"DayMonthYear"===e?`${r}/${n}/${o}`:"MonthDayYear"===e?`${n}/${r}/${o}`:`${o}-${n}-${r}`},clockTime=(t,e)=>24===e?`${pad2(t.hour)}:${pad2(t.minute)}`:`${t.hour%12||12}:${pad2(t.minute)} ${t.hour<12?"AM":"PM"}`,labelled=(t,e)=>e??!IST_NAMES.includes(t),withLabel=(t,e,r,n)=>labelled(r,n)?`${t} ${e.label}`:t,intlZone=t=>{if("viewer"!==t)return IST_NAMES.includes(t)?IST_TIME_ZONE:t},digitSets=new Map,localeDigits=t=>{const e=digitSets.get(t);if(void 0!==e)return e;const r=new Intl.NumberFormat(t,{useGrouping:!1}),n=new Set;for(let t=0;t<=9;t++)n.add(r.format(t));return digitSets.set(t,n),n},digitsIn=(t,e)=>Array.from(t).filter(t=>e.has(t)).join(""),hasFormatToParts=t=>"function"==typeof t.formatToParts,localeDayMonthYear=(t,e,r)=>{const n=dateTimeFormat(e,{day:"2-digit",month:"short",year:"numeric",timeZone:r});if(!hasFormatToParts(n)){const n=localeDigits(e);return`${digitsIn(dateTimeFormat(e,{day:"2-digit",timeZone:r}).format(t),n)}-${dateTimeFormat(e,{month:"short",timeZone:r}).format(t).trim().replace(".","")}-${digitsIn(dateTimeFormat(e,{year:"numeric",timeZone:r}).format(t),n)}`}const o=n.formatToParts(t),part=t=>o.find(e=>e.type===t)?.value??"";return`${part("day")}-${part("month").replace(".","")}-${part("year")}`},dateText=(t,e)=>{const r=getFormatterConfig().dateOrder??"DayMonthNameYear";if("DayMonthNameYear"!==r)return numericDate(wallClock(t,e),r);const n=configuredLocale();return n?localeDayMonthYear(t,n,intlZone(e)):dayMonthYear(wallClock(t,e))},localeClock=(t,e,r,n)=>dateTimeFormat(e,{hour:"2-digit",minute:"2-digit",timeZone:intlZone(r),timeZoneName:labelled(r,n.zoneLabel)?"short":void 0,hourCycle:void 0===n.hourCycle?void 0:24===n.hourCycle?"h23":"h12"}).format(t);export const formatDate=(t,e={})=>{const r=toInstant(t);return r?dateText(r,isDateOnly(t)?"UTC":e.zone??getFormatterConfig().zone):emptyOf(e.empty)};export const formatTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??getFormatterConfig().zone,o=configuredLocale();if(o)return localeClock(r,o,n,e);const i=wallClock(r,n);return withLabel(clockTime(i,hourCycleOf(e)),i,n,e.zoneLabel)};export const formatDateTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??getFormatterConfig().zone,o=configuredLocale();if(o)return`${dateText(r,n)}, ${localeClock(r,o,n,e)}`;const i=wallClock(r,n);return withLabel(`${dateText(r,n)}, ${clockTime(i,hourCycleOf(e))}`,i,n,e.zoneLabel)};const SECOND=1e3,MINUTE=6e4,HOUR=36e5,DAY=24*HOUR,WEEK=7*DAY,MONTH=30*DAY,YEAR=365*DAY,INTL_UNITS=[["year",YEAR],["month",MONTH],["day",DAY],["hour",HOUR],["minute",6e4]];export const formatRelative=(t,e={})=>{const r=toInstant(t),n=toInstant(e.now??Date.now());if(!r||!n)return emptyOf(e.empty);const o=r.getTime()-n.getTime(),i=Math.abs(o),a=configuredLocale();if(null!==a||!0===e.intl){const t=relativeTimeFormat(Intl.RelativeTimeFormat,a??getFormatterConfig().locale),[e,r]=INTL_UNITS.find(([,t])=>i>=t)??["second",1e3];return t.format(Math.round(o/r),e)}if(i<6e4)return"just now";const[s,c]=i<HOUR?["minute",6e4]:i<DAY?["hour",HOUR]:i<WEEK?["day",DAY]:i<MONTH?["week",WEEK]:i<YEAR?["month",MONTH]:["year",YEAR],m=Math.floor(i/c),l=`${m} ${s}${1===m?"":"s"}`;return o<0?`${l} ago`:`in ${l}`};const toNumber=t=>{if(null==t)return null;if("string"==typeof t&&""===t.trim())return null;const e="number"==typeof t?t:Number(t);return Number.isFinite(e)?e:null},clampDigits=t=>Math.min(20,Math.max(0,Math.trunc(t))),shiftDecimal=(t,e)=>/e/i.test(String(t))?t*10**e:Number(`${t}e${e}`),houseSeparators=()=>numberSeparators()??{decimal:".",group:","},groupInteger=(t,e,r)=>{if("none"===e||t.length<=3)return t;if("international"===e)return t.replace(/\B(?=(\d{3})+(?!\d))/g,r);return`${t.slice(0,-3).replace(/\B(?=(\d{2})+(?!\d))/g,r)}${r}${t.slice(-3)}`},plainDigits=(t,e)=>numberFormat("en-US",{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:e}).format(t),decimalText=(t,e,r,n)=>{const o=Math.max(clampDigits(r),clampDigits(e)),i=clampDigits(e),a=plainDigits(Math.abs(t),o),[s="0",c=""]=a.split(".");let m=c.padEnd(i,"0");for(;m.length>i&&m.endsWith("0");)m=m.slice(0,-1);const l=houseSeparators(),u=groupInteger(s,n,l.group)+(m?`${l.decimal}${m}`:"");return t<0&&/[1-9]/.test(a)?`-${u}`:u},HOUSE_NUMBER_KEYS=new Set(["grouping","minimumFractionDigits","maximumFractionDigits","locale","empty","scale"]),intlNumberOptions=t=>{if(!Object.entries(t).some(([t,e])=>void 0!==e&&!HOUSE_NUMBER_KEYS.has(t)))return null;const{grouping:e,locale:r,empty:n,scale:o,...i}=t;return"none"===e&&void 0===i.useGrouping?{...i,useGrouping:!1}:i},localeOf=t=>t.locale??getFormatterConfig().locale,intlTextLocale=t=>{const e=configuredLocale();return null===e||"indian"===t.grouping||"international"===t.grouping?null:t.locale??e},fractionDigits=(t,e)=>{const r=clampDigits(t);return{minimumFractionDigits:r,maximumFractionDigits:Math.max(r,clampDigits(e))}},groupingOption=t=>"none"===t?{useGrouping:!1}:{};export const formatNumber=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n)return styledNumber(numberFormat(localeOf(e),n),r);const o=intlTextLocale(e);return o?styledNumber(numberFormat(o,{...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??3),...groupingOption(e.grouping)}),r):decimalText(r,e.minimumFractionDigits??0,e.maximumFractionDigits??3,e.grouping??groupingForLocale(localeOf(e)))};export const formatPercent=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n){const t="percent"===e.scale?shiftDecimal(r,-2):r;return styledNumber(numberFormat(localeOf(e),{style:"percent",...n}),t)}const o=intlTextLocale(e);if(o){const t="percent"===e.scale?shiftDecimal(r,-2):r;return styledNumber(numberFormat(o,{style:"percent",...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0),...groupingOption(e.grouping)}),t)}const i="percent"===e.scale?r:shiftDecimal(r,2);return`${decimalText(i,e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0,e.grouping??groupingForLocale(localeOf(e)))}%`};const SYMBOLS={INR:"₹",USD:"$",EUR:"€",GBP:"£"},MINOR_DIGITS={JPY:0,KRW:0,VND:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3};export const formatMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=Math.round(Math.abs(r)),a=String(i).padStart(o+1,"0"),s=a.slice(0,a.length-o);let c=a.slice(a.length-o);e.dropZeroDecimals&&/^0*$/.test(c)&&(c="");const m=intlTextLocale({grouping:e.grouping});if(m)try{const t=Number(`${r<0&&0!==i?"-":""}${s}.${a.slice(a.length-o)||"0"}`);return styledNumber(numberFormat(m,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(c?o:0,o),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatMinor rejected currency "${n}" or locale "${m}": ${t.message}`,{cause:t});throw t}const l=e.grouping??("INR"===n?"indian":"international"),u=houseSeparators(),g=groupInteger(s,l,u.group)+(c?`${u.decimal}${c}`:""),p=r<0&&0!==i?"-":"";if(!1===e.symbol)return`${p}${g}`;const d=SYMBOLS[n];return d?`${p}${d}${g}`:`${p}${n} ${g}`};const HUNDREDTH_EXTRA_DIGITS=2,RUPEE_SIGN="₹",trimHundredths=(t,e)=>{let r=t.length;for(;r>e&&"0"===t.slice(r-1,r);)r-=1;return t.slice(0,r)};export const formatHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);if(!Number.isSafeInteger(r))throw new RangeError(`formatHundredthMinor needs a whole number of hundredths within the safe integer range, got ${r}.`);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=o+2,a=String(Math.abs(r)).padStart(i+1,"0"),s=a.slice(a.length-i),c=a.slice(0,a.length-i);let m=trimHundredths(s,o);e.dropZeroDecimals&&/^0*$/.test(m)&&(m="");const l=r<0?"-":"",u=intlTextLocale({grouping:e.grouping});if(u)try{const t=Number(`${l}${c}.${s}`);return styledNumber(numberFormat(u,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(m.length,i),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatHundredthMinor rejected currency "${n}" or locale "${u}": ${t.message}`,{cause:t});throw t}const g=e.grouping??("INR"===n?"indian":"international"),p=houseSeparators(),d=groupInteger(c,g,p.group)+(m?`${p.decimal}${m}`:"");if(!1===e.symbol)return`${l}${d}`;const f=SYMBOLS[n];return f?`${l}${f}${d}`:`${l}${n} ${d}`};const wholeDigitsOf=(t,e)=>{let r="",n=!0;for(const o of t)if(e.includes(o)){if(n)return null;n=!0}else{if(!/\d/.test(o))return null;r+=o,n=!1}return n&&""!==t?null:r};export const parseHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('parseHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=e.currency.toUpperCase(),n=(MINOR_DIGITS[r]??2)+2,refuse=e=>new RangeError(`parseHundredthMinor cannot read "${t}": ${e}`);let o=t.trim();if(""===o)throw refuse("the text is empty");let i=!1;o.startsWith("-")&&(i=!0,o=o.slice(1).trim());const a=["₹",SYMBOLS[r]];for(const t of a)if(t&&o.startsWith(t)){o=o.slice(t.length).trim();break}const s=houseSeparators(),c=[",",s.group].filter(t=>t!==s.decimal),m=o.split(s.decimal);if(m.length>2)throw refuse("more than one decimal separator");const l=wholeDigitsOf(m[0]??"",c);if(null===l)throw refuse("a character that is not a digit, a group separator or the decimal separator");const u=2===m.length,g=m[1]??"";if(u&&!/^\d+$/.test(g))throw refuse("the decimal part must be one or more digits");if(g.length>n)throw refuse(`more than ${n} decimal places, the most this currency takes`);const p=l+g.padEnd(n,"0");if(""===p)throw refuse("it has no digits");const d=Number(p);if(!Number.isSafeInteger(d))throw refuse("the amount is beyond the safe integer range");return i&&0!==d?-d:d};
|
|
1
|
+
"use strict";import{configuredLocale,getFormatterConfig,groupingForLocale,numberSeparators,styledNumber}from"./formatConfig.js";import{dateTimeFormat,numberFormat,relativeTimeFormat}from"./intlCache.js";const MONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],IST_OFFSET_MINUTES=330,IST_TIME_ZONE="Asia/Kolkata",IST_NAMES=["IST",IST_TIME_ZONE,"Asia/Calcutta"],DATE_ONLY=/^\d{4}-\d{2}-\d{2}$/,pad2=t=>t<10?`0${t}`:`${t}`,emptyOf=t=>t??getFormatterConfig().empty,toInstant=t=>{if(null==t||""===t)return null;const e=t instanceof Date?t:new Date("string"==typeof t?t.trim():t);return Number.isNaN(e.getTime())?null:e},isDateOnly=t=>"string"==typeof t&&DATE_ONLY.test(t.trim()),gmtLabel=t=>{if(0===t)return"GMT";const e=t>0?"+":"-",r=Math.abs(t),n=r%60;return`GMT${e}${Math.floor(r/60)}${0===n?"":`:${pad2(n)}`}`},shifted=(t,e,r)=>{const n=new Date(t.getTime()+6e4*e);return{year:n.getUTCFullYear(),month:n.getUTCMonth(),day:n.getUTCDate(),hour:n.getUTCHours(),minute:n.getUTCMinutes(),label:r}},inIanaZone=(t,e)=>{const r=dateTimeFormat("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",hour12:!1}).format(t),n=r.match(/\d+/g)??[];if(5!==n.length)throw new Error(`Unexpected date text from Intl for zone ${e}: ${r}`);const[o="",i="",a="",s="",m=""]=n,c=Number.parseInt(a,10),l=Number.parseInt(o,10)-1,u=Number.parseInt(i,10),g=Number.parseInt(s,10)%24,p=Number.parseInt(m,10),f=Date.UTC(c,l,u,g,p),d=Math.round((f-6e4*Math.floor(t.getTime()/6e4))/6e4);return{year:c,month:l,day:u,hour:g,minute:p,label:gmtLabel(d)}},wallClock=(t,e)=>IST_NAMES.includes(e)?shifted(t,330,"IST"):"UTC"===e?shifted(t,0,"UTC"):"viewer"===e?{year:t.getFullYear(),month:t.getMonth(),day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),label:gmtLabel(-t.getTimezoneOffset())}:inIanaZone(t,e),hourCycleOf=t=>t.hourCycle??12,dayMonthYear=t=>`${pad2(t.day)}-${MONTHS[t.month]}-${t.year}`,numericDate=(t,e)=>{const r=pad2(t.day),n=pad2(t.month+1),o=String(t.year).padStart(4,"0");return"DayMonthYear"===e?`${r}/${n}/${o}`:"MonthDayYear"===e?`${n}/${r}/${o}`:`${o}-${n}-${r}`},clockTime=(t,e)=>24===e?`${pad2(t.hour)}:${pad2(t.minute)}`:`${t.hour%12||12}:${pad2(t.minute)} ${t.hour<12?"AM":"PM"}`,labelled=(t,e)=>e??!IST_NAMES.includes(t),withLabel=(t,e,r,n)=>labelled(r,n)?`${t} ${e.label}`:t,intlZone=t=>{if("viewer"!==t)return IST_NAMES.includes(t)?IST_TIME_ZONE:t},digitSets=new Map,localeDigits=t=>{const e=digitSets.get(t);if(void 0!==e)return e;const r=new Intl.NumberFormat(t,{useGrouping:!1}),n=new Set;for(let t=0;t<=9;t++)n.add(r.format(t));return digitSets.set(t,n),n},digitsIn=(t,e)=>Array.from(t).filter(t=>e.has(t)).join(""),hasFormatToParts=t=>"function"==typeof t.formatToParts,localeDayMonthYear=(t,e,r)=>{const n=dateTimeFormat(e,{day:"2-digit",month:"short",year:"numeric",timeZone:r});if(!hasFormatToParts(n)){const n=localeDigits(e);return`${digitsIn(dateTimeFormat(e,{day:"2-digit",timeZone:r}).format(t),n)}-${dateTimeFormat(e,{month:"short",timeZone:r}).format(t).trim().replace(".","")}-${digitsIn(dateTimeFormat(e,{year:"numeric",timeZone:r}).format(t),n)}`}const o=n.formatToParts(t),part=t=>o.find(e=>e.type===t)?.value??"";return`${part("day")}-${part("month").replace(".","")}-${part("year")}`},dateText=(t,e)=>{const r=getFormatterConfig().dateOrder??"DayMonthNameYear";if("DayMonthNameYear"!==r)return numericDate(wallClock(t,e),r);const n=configuredLocale();return n?localeDayMonthYear(t,n,intlZone(e)):dayMonthYear(wallClock(t,e))},localeClock=(t,e,r,n)=>dateTimeFormat(e,{hour:"2-digit",minute:"2-digit",timeZone:intlZone(r),timeZoneName:labelled(r,n.zoneLabel)?"short":void 0,hourCycle:void 0===n.hourCycle?void 0:24===n.hourCycle?"h23":"h12"}).format(t);export const formatDate=(t,e={})=>{const r=toInstant(t);return r?dateText(r,isDateOnly(t)?"UTC":e.zone??getFormatterConfig().zone):emptyOf(e.empty)};export const formatTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??getFormatterConfig().zone,o=configuredLocale();if(o)return localeClock(r,o,n,e);const i=wallClock(r,n);return withLabel(clockTime(i,hourCycleOf(e)),i,n,e.zoneLabel)};export const formatDateTime=(t,e={})=>{const r=toInstant(t);if(!r)return emptyOf(e.empty);const n=e.zone??getFormatterConfig().zone,o=configuredLocale();if(o)return`${dateText(r,n)}, ${localeClock(r,o,n,e)}`;const i=wallClock(r,n);return withLabel(`${dateText(r,n)}, ${clockTime(i,hourCycleOf(e))}`,i,n,e.zoneLabel)};const SECOND=1e3,MINUTE=6e4,HOUR=36e5,DAY=24*HOUR,WEEK=7*DAY,MONTH=30*DAY,YEAR=365*DAY,INTL_UNITS=[["year",YEAR],["month",MONTH],["day",DAY],["hour",HOUR],["minute",6e4]];export const formatRelative=(t,e={})=>{const r=toInstant(t),n=toInstant(e.now??Date.now());if(!r||!n)return emptyOf(e.empty);const o=r.getTime()-n.getTime(),i=Math.abs(o),a=configuredLocale();if((null!==a||!0===e.intl)&&"function"==typeof Intl.RelativeTimeFormat){const t=relativeTimeFormat(Intl.RelativeTimeFormat,a??getFormatterConfig().locale),[e,r]=INTL_UNITS.find(([,t])=>i>=t)??["second",1e3];return t.format(Math.round(o/r),e)}if(i<6e4)return"just now";const[s,m]=i<HOUR?["minute",6e4]:i<DAY?["hour",HOUR]:i<WEEK?["day",DAY]:i<MONTH?["week",WEEK]:i<YEAR?["month",MONTH]:["year",YEAR],c=Math.floor(i/m),l=`${c} ${s}${1===c?"":"s"}`;return o<0?`${l} ago`:`in ${l}`};const toNumber=t=>{if(null==t)return null;if("string"==typeof t&&""===t.trim())return null;const e="number"==typeof t?t:Number(t);return Number.isFinite(e)?e:null},clampDigits=t=>Math.min(20,Math.max(0,Math.trunc(t))),shiftDecimal=(t,e)=>/e/i.test(String(t))?t*10**e:Number(`${t}e${e}`),houseSeparators=()=>numberSeparators()??{decimal:".",group:","},groupInteger=(t,e,r)=>{if("none"===e||t.length<=3)return t;if("international"===e)return t.replace(/\B(?=(\d{3})+(?!\d))/g,r);return`${t.slice(0,-3).replace(/\B(?=(\d{2})+(?!\d))/g,r)}${r}${t.slice(-3)}`},plainDigits=(t,e)=>numberFormat("en-US",{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:e}).format(t),decimalText=(t,e,r,n)=>{const o=Math.max(clampDigits(r),clampDigits(e)),i=clampDigits(e),a=plainDigits(Math.abs(t),o),[s="0",m=""]=a.split(".");let c=m.padEnd(i,"0");for(;c.length>i&&c.endsWith("0");)c=c.slice(0,-1);const l=houseSeparators(),u=groupInteger(s,n,l.group)+(c?`${l.decimal}${c}`:"");return t<0&&/[1-9]/.test(a)?`-${u}`:u},HOUSE_NUMBER_KEYS=new Set(["grouping","minimumFractionDigits","maximumFractionDigits","locale","empty","scale"]),intlNumberOptions=t=>{if(!Object.entries(t).some(([t,e])=>void 0!==e&&!HOUSE_NUMBER_KEYS.has(t)))return null;const{grouping:e,locale:r,empty:n,scale:o,...i}=t;return"none"===e&&void 0===i.useGrouping?{...i,useGrouping:!1}:i},localeOf=t=>t.locale??getFormatterConfig().locale,intlTextLocale=t=>{const e=configuredLocale();return null===e||"indian"===t.grouping||"international"===t.grouping?null:t.locale??e},fractionDigits=(t,e)=>{const r=clampDigits(t);return{minimumFractionDigits:r,maximumFractionDigits:Math.max(r,clampDigits(e))}},groupingOption=t=>"none"===t?{useGrouping:!1}:{};export const formatNumber=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n)return styledNumber(numberFormat(localeOf(e),n),r);const o=intlTextLocale(e);return o?styledNumber(numberFormat(o,{...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??3),...groupingOption(e.grouping)}),r):decimalText(r,e.minimumFractionDigits??0,e.maximumFractionDigits??3,e.grouping??groupingForLocale(localeOf(e)))};export const formatPercent=(t,e={})=>{const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=intlNumberOptions(e);if(n){const t="percent"===e.scale?shiftDecimal(r,-2):r;return styledNumber(numberFormat(localeOf(e),{style:"percent",...n}),t)}const o=intlTextLocale(e);if(o){const t="percent"===e.scale?shiftDecimal(r,-2):r;return styledNumber(numberFormat(o,{style:"percent",...fractionDigits(e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0),...groupingOption(e.grouping)}),t)}const i="percent"===e.scale?r:shiftDecimal(r,2);return`${decimalText(i,e.minimumFractionDigits??0,e.maximumFractionDigits??e.minimumFractionDigits??0,e.grouping??groupingForLocale(localeOf(e)))}%`};const SYMBOLS={INR:"₹",USD:"$",EUR:"€",GBP:"£"},MINOR_DIGITS={JPY:0,KRW:0,VND:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3};export const formatMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=Math.round(Math.abs(r)),a=String(i).padStart(o+1,"0"),s=a.slice(0,a.length-o);let m=a.slice(a.length-o);e.dropZeroDecimals&&/^0*$/.test(m)&&(m="");const c=intlTextLocale({grouping:e.grouping});if(c)try{const t=Number(`${r<0&&0!==i?"-":""}${s}.${a.slice(a.length-o)||"0"}`);return styledNumber(numberFormat(c,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(m?o:0,o),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatMinor rejected currency "${n}" or locale "${c}": ${t.message}`,{cause:t});throw t}const l=e.grouping??("INR"===n?"indian":"international"),u=houseSeparators(),g=groupInteger(s,l,u.group)+(m?`${u.decimal}${m}`:""),p=r<0&&0!==i?"-":"";if(!1===e.symbol)return`${p}${g}`;const f=SYMBOLS[n];return f?`${p}${f}${g}`:`${p}${n} ${g}`};const HUNDREDTH_EXTRA_DIGITS=2,RUPEE_SIGN="₹",trimHundredths=(t,e)=>{let r=t.length;for(;r>e&&"0"===t.slice(r-1,r);)r-=1;return t.slice(0,r)};export const formatHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('formatHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=toNumber(t);if(null===r)return emptyOf(e.empty);if(!Number.isSafeInteger(r))throw new RangeError(`formatHundredthMinor needs a whole number of hundredths within the safe integer range, got ${r}.`);const n=e.currency.toUpperCase(),o=MINOR_DIGITS[n]??2,i=o+2,a=String(Math.abs(r)).padStart(i+1,"0"),s=a.slice(a.length-i),m=a.slice(0,a.length-i);let c=trimHundredths(s,o);e.dropZeroDecimals&&/^0*$/.test(c)&&(c="");const l=r<0?"-":"",u=intlTextLocale({grouping:e.grouping});if(u)try{const t=Number(`${l}${m}.${s}`);return styledNumber(numberFormat(u,{...!1===e.symbol?{}:{style:"currency",currency:n},...fractionDigits(c.length,i),...groupingOption(e.grouping)}),t)}catch(t){if(t instanceof RangeError)throw new RangeError(`formatHundredthMinor rejected currency "${n}" or locale "${u}": ${t.message}`,{cause:t});throw t}const g=e.grouping??("INR"===n?"indian":"international"),p=houseSeparators(),f=groupInteger(m,g,p.group)+(c?`${p.decimal}${c}`:"");if(!1===e.symbol)return`${l}${f}`;const d=SYMBOLS[n];return d?`${l}${d}${f}`:`${l}${n} ${f}`};const wholeDigitsOf=(t,e)=>{let r="",n=!0;for(const o of t)if(e.includes(o)){if(n)return null;n=!0}else{if(!/\d/.test(o))return null;r+=o,n=!1}return n&&""!==t?null:r};export const parseHundredthMinor=(t,e)=>{if(!e?.currency)throw new TypeError('parseHundredthMinor needs options.currency, an ISO 4217 code such as "INR".');const r=e.currency.toUpperCase(),n=(MINOR_DIGITS[r]??2)+2,refuse=e=>new RangeError(`parseHundredthMinor cannot read "${t}": ${e}`);let o=t.trim();if(""===o)throw refuse("the text is empty");let i=!1;o.startsWith("-")&&(i=!0,o=o.slice(1).trim());const a=["₹",SYMBOLS[r]];for(const t of a)if(t&&o.startsWith(t)){o=o.slice(t.length).trim();break}const s=houseSeparators(),m=[",",s.group].filter(t=>t!==s.decimal),c=o.split(s.decimal);if(c.length>2)throw refuse("more than one decimal separator");const l=wholeDigitsOf(c[0]??"",m);if(null===l)throw refuse("a character that is not a digit, a group separator or the decimal separator");const u=2===c.length,g=c[1]??"";if(u&&!/^\d+$/.test(g))throw refuse("the decimal part must be one or more digits");if(g.length>n)throw refuse(`more than ${n} decimal places, the most this currency takes`);const p=l+g.padEnd(n,"0");if(""===p)throw refuse("it has no digits");const f=Number(p);if(!Number.isSafeInteger(f))throw refuse("the amount is beyond the safe integer range");return i&&0!==f?-f:f};
|
|
@@ -121,7 +121,7 @@ export declare const formatDateTime: (value: DateValue, options?: FormatDateTime
|
|
|
121
121
|
* 59 minutes never reads as "60 minutes". With a configured locale, or
|
|
122
122
|
* `{ intl: true }`, the phrase is Intl's in that locale instead, with numeric
|
|
123
123
|
* "auto" and rounded counts: "yesterday", "in 2 days" at 36 hours ahead, "10 days ago",
|
|
124
|
-
* "30 seconds ago", "now".
|
|
124
|
+
* "30 seconds ago", "now". An engine without Intl.RelativeTimeFormat (Hermes on Android) always gets the English phrase.
|
|
125
125
|
*/
|
|
126
126
|
export declare const formatRelative: (value: DateValue, options?: FormatRelativeOptions) => string;
|
|
127
127
|
/**
|
|
@@ -121,7 +121,7 @@ export declare const formatDateTime: (value: DateValue, options?: FormatDateTime
|
|
|
121
121
|
* 59 minutes never reads as "60 minutes". With a configured locale, or
|
|
122
122
|
* `{ intl: true }`, the phrase is Intl's in that locale instead, with numeric
|
|
123
123
|
* "auto" and rounded counts: "yesterday", "in 2 days" at 36 hours ahead, "10 days ago",
|
|
124
|
-
* "30 seconds ago", "now".
|
|
124
|
+
* "30 seconds ago", "now". An engine without Intl.RelativeTimeFormat (Hermes on Android) always gets the English phrase.
|
|
125
125
|
*/
|
|
126
126
|
export declare const formatRelative: (value: DateValue, options?: FormatRelativeOptions) => string;
|
|
127
127
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority/mobile-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.191.1",
|
|
4
4
|
"description": "Platform layer for Webority React Native apps: HTTP clients, auth/token storage, config, logging, formatters, validators, network status, permissions, storage, version checks and over-the-air updates. No UI.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -404,7 +404,7 @@
|
|
|
404
404
|
}
|
|
405
405
|
},
|
|
406
406
|
"dependencies": {
|
|
407
|
-
"@webority/api-contract": "0.
|
|
407
|
+
"@webority/api-contract": "0.191.1",
|
|
408
408
|
"axios": "^1.19.0",
|
|
409
409
|
"libphonenumber-js": "^1.12.41"
|
|
410
410
|
},
|