UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 2.25 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import"../../../core/has.js";const e=new Set(["+","-","*","/","(",")"]);function t(t,r){(t=t.replaceAll(" ","")).startsWith("-")&&(t="0"+t),t.startsWith("+")&&(t=t.slice(1));const l=t.split(""),o=[],s=[];let a="";for(let i=0;i<l.length;i++){const t=l[i];e.has(t)?(a.length&&s.push(n(a,r)),o.push(t),a=""):a=a.concat(t)}return a.length&&s.push(n(a,r)),{ops:o,nums:s}}function n(e,t){return e.toLowerCase().startsWith("b")?t[parseInt(e.slice(1),10)-1]:parseFloat(e)}function r(e,t,n,r){if("number"==typeof n&&"number"==typeof r)return n+r;let l,o,s;"number"==typeof n?(s=r,l=s.length,o=new Float32Array(l),o.fill(n)):(l=n.length,o=n,r.constructor===Number?(s=new Float32Array(l),s.fill(r)):s=r);const a=new Float32Array(l);switch(t){case"+":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]+s[t]);break;case"-":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]-s[t]);break;case"*":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]*s[t]);break;case"/":for(let t=0;t<l;t++)(null==e||e[t])&&s[t]&&(a[t]=o[t]/s[t]);break;case"(":case")":throw new Error("encountered error with custom band index equation")}return a}function l(e,t){e.splice(t,1);let n=0,r=0;do{n=0,r=0;for(let t=0;t<e.length;t++)if("("===e[t])n=t;else if(")"===e[t]){r=t;break}r===n+1&&e.splice(n,2)}while(r===n+1);return e}function o(e){if(1===e.length)return{opIndex:0,numIndex:0};let t=0,n=0;for(let s=0;s<e.length;s++)if("("===e[s])t=s;else if(")"===e[s]){n=s;break}const r=0===n?e:e.slice(t+1,n);let l=-1;for(let s=0;s<r.length;s++)if("*"===r[s]||"/"===r[s]){l=s;break}if(l>-1)n>0&&(l+=t+1);else{for(let e=0;e<r.length;e++)if("+"===r[e]||"-"===r[e]){l=e;break}n>0&&(l+=t+1)}let o=0;for(let s=0;s<l;s++)"("===e[s]&&o++;return{opIndex:l,numIndex:l-o}}function s(e,n,s){let a,{ops:i,nums:f}=t(s,n);if(0===i.length){const e=1===f.length?f[0]:n[0];if(e instanceof Float32Array)return[e];const t=new Float32Array(n[0].length);return"number"==typeof e?t.fill(e):t.set(e),[t]}for(;i.length>0;){const{numIndex:t,opIndex:n}=o(i);if(a=r(e,i[n],f[t],f[t+1]),1===i.length)break;i=l(i,n),f.splice(t,2,a)}return[a]}export{s as calculateCustomBandIndex};