@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.27 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{escapeRegExpString as e}from"./string.js";import{getLocale as t}from"../intl/locale.js";const n={ar:[".",","],bg:[","," "],bs:[",","."],ca:[",","."],cs:[","," "],da:[",","."],de:[",","."],"de-ch":[".","’"],el:[",","."],en:[".",","],"en-au":[".",","],es:[",","."],"es-mx":[".",","],et:[","," "],fi:[","," "],fr:[","," "],"fr-ch":[","," "],he:[".",","],hi:[".",",","#,##,##0.###"],hr:[",","."],hu:[","," "],id:[",","."],it:[",","."],"it-ch":[".","’"],ja:[".",","],ko:[".",","],lt:[","," "],lv:[","," "],mk:[",","."],nb:[","," "],nl:[",","."],no:[","," "],pl:[","," "],pt:[",","."],"pt-pt":[","," "],ro:[",","."],ru:[","," "],sk:[","," "],sl:[",","."],sr:[",","."],sv:[","," "],th:[".",","],tr:[",","."],uk:[","," "],vi:[",","."],zh:[".",","]};function r(e=t()){let r=(e=e.toLowerCase())in n;if(!r){const t=e.split("-");t.length>1&&t[0]in n&&(e=t[0],r=!0),r||(e="en")}const[l,s,o="#,##0.###"]=n[e];return{decimal:l,group:s,pattern:o}}function l(e,t){const n=r((t={...t}).locale);t.customs=n;const l=t.pattern||n.pattern;return isNaN(e)||Math.abs(e)===1/0?null:o(e,l,t)}const s=/[#0,]*[#0](?:\.0*#*)?/;function o(e,t,n){const r=(n=n||{}).customs.group,l=n.customs.decimal,o=t.split(";"),a=o[0];if((t=o[e<0?1:0]||"-"+a).includes("%"))e*=100;else if(t.includes("‰"))e*=1e3;else{if(t.includes("¤"))throw new Error("currency notation not supported");if(t.includes("E"))throw new Error("exponential notation not supported")}const c=s,p=a.match(c);if(!p)throw new Error("unable to find a number expression in pattern: "+t);return!1===n.fractional&&(n.places=0),t.replace(c,i(e,p[0],{decimal:l,group:r,places:n.places,round:n.round}))}function i(e,t,n){!0===(n=n||{}).places&&(n.places=0),n.places===1/0&&(n.places=6);const r=t.split("."),l="string"==typeof n.places&&n.places.indexOf(",");let s=n.places;l?s=n.places.slice(l+1):+s>=0||(s=(r[1]||[]).length),n.round<0||(e=Number(e.toFixed(Number(s))));const o=String(Math.abs(e)).split("."),i=o[1]||"";if(r[1]||n.places){l&&(n.places=n.places.slice(0,Math.max(0,l)));const e=void 0!==n.places?n.places:r[1]&&r[1].lastIndexOf("0")+1;+e>i.length&&(o[1]=i.padEnd(Number(e),"0")),+s<i.length&&(o[1]=i.slice(0,Math.max(0,Number(s))))}else o[1]&&o.pop();const a=r[0].replace(",","");let c=a.indexOf("0");-1!==c&&(c=a.length-c,c>o[0].length&&(o[0]=o[0].padStart(c,"0")),a.includes("#")||(o[0]=o[0].slice(-c)));let p,u,d=r[0].lastIndexOf(",");if(-1!==d){p=r[0].length-d-1;const e=r[0].slice(0,d);d=e.lastIndexOf(","),-1!==d&&(u=e.length-d-1)}const f=[];for(let g=o[0];g;){const e=g.length-p;f.push(e>0?g.slice(Math.max(0,e)):g),g=e>0?g.slice(0,e):"",u&&(p=u,u=void 0)}return o[0]=f.reverse().join(n.group||","),o.join(n.decimal||".")}function a(e){return c(e).regexp}function c(t){const n=r((t=t||{}).locale),l=t.pattern||n.pattern,o=n.group,i=n.decimal;let a=1;if(l.includes("%"))a/=100;else if(l.includes("‰"))a/=1e3;else if(l.includes("¤"))throw new Error("currency notation not supported");const c=l.split(";");1===c.length&&c.push("-"+c[0]);const p=f(c,(n=>(n="(?:"+e(n,".")+")").replace(s,(e=>{const n={signed:!1,separator:t.strict?o:[o,""],fractional:t.fractional,decimal:i,exponent:!1},r=e.split(".");let l=t.places;1===r.length&&1!==a&&(r[1]="###"),1===r.length||0===l?n.fractional=!1:(void 0===l&&(l=t.pattern?r[1].lastIndexOf("0")+1:1/0),l&&null==t.fractional&&(n.fractional=!0),!t.places&&+l<r[1].length&&(l+=","+r[1].length),n.places=l);const s=r[0].split(",");return s.length>1&&(n.groupSize=s.pop().length,s.length>1&&(n.groupSize2=s.pop().length)),"("+u(n)+")"}))),!0);return{regexp:p.replaceAll(/[\xa0 ]/g,"[\\s\\xa0]"),group:o,decimal:i,factor:a}}function p(e,t){const n=c(t),r=new RegExp("^"+n.regexp+"$").exec(e);if(!r)return NaN;let l=r[1];if(!r[1]){if(!r[2])return NaN;l=r[2],n.factor*=-1}return l=l.replaceAll(new RegExp("["+n.group+"\\s\\xa0]","g"),"").replace(n.decimal,"."),Number(l)*n.factor}function u(e){"places"in(e=e||{})||(e.places=1/0),"string"!=typeof e.decimal&&(e.decimal="."),"fractional"in e&&!String(e.places).startsWith("0")||(e.fractional=[!0,!1]),"exponent"in e||(e.exponent=[!0,!1]),"eSigned"in e||(e.eSigned=[!0,!1]);const t=d(e),n=f(e.fractional,(t=>{let n="";return t&&0!==e.places&&(n="\\"+e.decimal,e.places===1/0?n="(?:"+n+"\\d+)?":n+="\\d{"+e.places+"}"),n}),!0);let r=t+n;return n&&(r="(?:(?:"+r+")|(?:"+n+"))"),r+f(e.exponent,(t=>t?"([eE]"+d({signed:e.eSigned})+")":""))}function d(t){"signed"in(t=t||{})||(t.signed=[!0,!1]),"separator"in t?"groupSize"in t||(t.groupSize=3):t.separator="";return f(t.signed,(e=>e?"[-+]":""),!0)+f(t.separator,(n=>{if(!n)return"(?:\\d+)";" "===(n=e(n))?n="\\s":" "===n&&(n="\\s\\xa0");const r=t.groupSize,l=t.groupSize2;if(l){const e="(?:0|[1-9]\\d{0,"+(l-1)+"}(?:["+n+"]\\d{"+l+"})*["+n+"]\\d{"+r+"})";return r-l>0?"(?:"+e+"|(?:0|[1-9]\\d{0,"+(r-1)+"}))":e}return"(?:0|[1-9]\\d{0,"+(r-1)+"}(?:["+n+"]\\d{"+r+"})*)"}),!0)}const f=(e,t,n)=>{if(!Array.isArray(e))return t(e);const r=[];for(let l=0;l<e.length;l++)r.push(t(e[l]));return g(r.join("|"),Boolean(n))},g=(e,t)=>"("+(t?"?:":"")+e+")";export{c as _parseInfo,l as format,r as getCustoms,p as parse,a as regexp};