@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.68 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{isDateString as e}from"../../../../core/date.js";import{_parseInfo as t}from"../../../../core/number.js";import{normalizeFieldName as n,getFieldDefaultLength as r}from"../../../support/fieldUtils.js";import{inferLocationInfo as i}from"../../../support/locationUtils.js";const o=/^\s*"([\S\s]*)"\s*$/,l=/""/g,s="\n",u=[","," ",";","|","\t"];function*c(e,t,n){let r=0;for(;r<=e.length;){const i=e.indexOf(t,r),o=e.slice(r,i>-1?i:void 0);r+=o.length+t.length,n&&!o.trim()||(yield o)}}function f(e){const t=e.includes("\r\n")?"\r\n":s;return c(e,t,!0)}function a(e,t){return c(e,t,!1)}function d(e,t,n){e=e.trim(),t=t?.trim();const r=[],o=Array.from(new Set([n?.delimiter,...u])).filter((e=>null!=e));for(const i of o){const n=g(e,i).length,o=g(t,i).length??n;n>1&&r.push({weight:Math.min(n,o),delimiter:i})}const l=r.sort((({weight:e},{weight:t})=>t-e)).map((({delimiter:e})=>e));for(const s of l){const t=m(e,s).names,r=i(t,n?.longitudeField,n?.latitudeField);if(r.longitudeFieldName&&r.latitudeFieldName)return{delimiter:s,locationInfo:r}}return{delimiter:l[0],locationInfo:null}}function*p(e,t,n,r=()=>Object.create(null)){const i=f(e);i.next();let u="",c="",d=0,p=r(),m=0;e:for(const f of i){const e=a(f,n);for(const i of e)if(u+=c+i,c="",d+=h(i),d%2==0){if(d>0){const e=o.exec(u);if(!e){p=r(),m=0,u="",d=0;continue e}p[t[m]]=e[1].replaceAll(l,'"'),m++}else p[t[m]]=u,m++;u="",d=0}else c=n;0===d?(yield p,p=r(),m=0):c=s}}function m(e,t){const r=g(e,t).filter((e=>null!=e)),i=r.map((e=>n(e)));for(let n=i.length-1;n>=0;n--)i[n]||(i.splice(n,1),r.splice(n,1));return{names:i,aliases:r}}function g(e,t){if(!e?.length)return[];const n=[];let r="",i="",s=0;const u=a(e,t);for(const c of u)if(r+=i+c,i="",s+=h(c),s%2==0){if(s>0){const e=o.exec(r);e&&n.push(e[1].replaceAll(l,'"'))}else n.push(r);r="",s=0}else i=t;return n}function h(e){let t=0,n=0;for(n=e.indexOf('"',n);n>=0;)t++,n=e.indexOf('"',n+1);return t}function N(e,t,n,i,o){const l=[],s=p(e,n,t),u=[];for(const r of s){if(10===u.length)break;u.push(r)}for(let c=0;c<n.length;c++){const e=n[c],t=i[c];if(e===o.longitudeFieldName||e===o.latitudeFieldName)l.push({name:e,type:"esriFieldTypeDouble",alias:t});else{let n;switch(b(u.map((t=>t[e])))){case"integer":n="esriFieldTypeInteger";break;case"double":n="esriFieldTypeDouble";break;case"date":n="esriFieldTypeDate";break;default:n="esriFieldTypeString"}l.push({name:e,type:n,alias:t,length:r(n)})}}return l}function b(t){if(!t.length)return"string";const n=/[^+\-.,0-9]/;return t.map((t=>{if(""!==t){if(!n.test(t)){let e=x(t);if(!isNaN(e))return/[.,]/.test(t)||!Number.isInteger(e)||e>214783647||e<-214783648?"double":"integer";if(t.includes("E")){if(e=Number(t),!Number.isNaN(e))return"double";if(t.includes(",")&&(t=t.replace(",","."),e=Number(t),!Number.isNaN(e)))return"double"}}return e(t)?"date":"string"}})).reduce(((e,t)=>void 0===e?t:void 0===t?e:e===t?t:"string"===e||"string"===t?"string":"double"===e||"double"===t?"double":void 0))}const x=function(){const e=t(),n=new RegExp("^"+e.regexp+"$"),r=new RegExp("["+e.group+"\\s\\xa0]","g"),i=e.factor;return t=>{const o=n.exec(t);if(e.factor=i,!o)return NaN;let l=o[1];if(!o[1]){if(!o[2])return NaN;l=o[2],e.factor*=-1}return l=l.replace(r,"").replace(e.decimal,"."),+l*e.factor}}();function y(e){return JSON.parse(JSON.stringify(e))}export{m as extractFieldNamesAndAliasesFromRow,d as inferDelimiterAndLocationInfo,b as inferFieldType,N as inferFields,x as parseNumber,p as parseRows,a as readRowParts,f as readRows,y as severSlicedString,g as splitSingleRow};