@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 10.3 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{deepClone as n}from"../arcade/deepClone.js";import{ArcadeExecutionError as t}from"../arcade/executionError.js";import r from"../arcade/ImmutableArray.js";import{B as e,J as o,j as i,r as u,v as a,n as s,s as c,w as l,i as f,O as d}from"./languageUtils.js";import{isPromiseLike as h}from"../core/promiseUtils.js";import{isArray as w,isBoolean as g,isInteger as m}from"../support/guards.js";function F(F){function p(n,e,o){if(n instanceof r)return n.toArray();if(w(n))return n;throw new t(e,"InvalidParameter",o)}function y(n,t){const r=n.length,e=Math.floor(r/2);return 0===r?[]:1===r?[n[0]]:v(y(n.slice(0,e),t),y(n.slice(e,r),t),t)}function v(n,t,r){const e=[];for(;n.length>0||t.length>0;)if(n.length>0&&t.length>0){let o=r(n[0],t[0]);isNaN(o)&&(o=0),o<=0?(e.push(n[0]),n=n.slice(1)):(e.push(t[0]),t=t.slice(1))}else n.length>0?(e.push(n[0]),n=n.slice(1)):t.length>0&&(e.push(t[0]),t=t.slice(1));return e}async function P(n,t){const r=n.length,e=Math.floor(r/2);if(0===r)return[];if(1===r)return[n[0]];const o=[await P(n.slice(0,e),t),await P(n.slice(e,r),t)];return I(o[0],o[1],t,[])}async function I(n,t,r,e){const o=e;if(n.length>0||t.length>0){if(n.length>0&&t.length>0){let i=await r(n[0],t[0]);return isNaN(i)&&(i=1),i<=0?(o.push(n[0]),n=n.slice(1)):(o.push(t[0]),t=t.slice(1)),I(n,t,r,e)}return n.length>0?(o.push(n[0]),I(n=n.slice(1),t,r,e)):(o.push(t[0]),I(n,t=t.slice(1),r,e))}return e}function N(n,r,o,u){e(o,1,2,n,r);let s=o[0];if(i(s)&&(s=s.toArray()),!1===w(s))throw new t(n,"InvalidParameter",r);if(o.length>1){if(!1===f(o[1]))throw new t(n,"InvalidParameter",r);let e=s;const i=o[1].createFunction(n);return u?P(e,i):(e=y(e,(n,t)=>i(n,t)),e)}let l=s;if(0===l.length)return[];const h={};for(let t=0;t<l.length;t++){const n=d(l[t]);""!==n&&(h[n]=!0)}if(!0===h.Array||!0===h.Dictionary||!0===h.Feature||!0===h.Voxel||!0===h.Pixel||!0===h.Point||!0===h.Polygon||!0===h.Polyline||!0===h.Multipoint||!0===h.Extent||!0===h.Function)return l.slice();let g=0,m="";for(const t in h)g++,m=t;return l=g>1||"String"===m?y(l,(n,t)=>{if(null==n||n===a)return null==t||t===a?0:1;if(null==t||t===a)return-1;const r=c(n),e=c(t);return r<e?-1:r===e?0:1}):"Number"===m?y(l,(n,t)=>n-t):"Boolean"===m?y(l,(n,t)=>n===t?0:t?-1:1):"Date"===m?y(l,(n,t)=>n.toNumber()-t.toNumber()):l.slice(),l}F.functions.array=function(r,a){return F.standardFunction(r,a,(s,c,l)=>{if(e(l,0,2,r,a),0===l.length)return[];if(1===l.length&&null===l[0])return[];if(w(l[0])){if(2===l.length&&!1===g(l[1]))throw new t(r,"InvalidParameter",a);return!0===o(l[1],!1)?n(l[0]):l[0].slice()}if(i(l[0])){if(2===l.length&&!1===g(l[1]))throw new t(r,"InvalidParameter",a);return!0===o(l[1],!1)?n(l[0]):l[0].toArray().slice()}const f=u(l[0]);if(isNaN(f)||!1===m(f))throw new t(r,"InvalidParameter",a);const d=o(l[1],null),h=new Array(f);return h.fill(d),h})},F.functions.front=function(n,r){return F.standardFunction(n,r,(o,u,a)=>{if(e(a,1,1,n,r),i(a[0])){if(a[0].length()<=0)throw new t(n,"OutOfBounds",r);return a[0].get(0)}if(w(a[0])){if(a[0].length<=0)throw new t(n,"OutOfBounds",r);return a[0][0]}throw new t(n,"InvalidParameter",r)})},F.functions.back=function(n,r){return F.standardFunction(n,r,(o,u,a)=>{if(e(a,1,1,n,r),i(a[0])){if(a[0].length()<=0)throw new t(n,"OutOfBounds",r);return a[0].get(a[0].length()-1)}if(w(a[0])){if(a[0].length<=0)throw new t(n,"OutOfBounds",r);return a[0][a[0].length-1]}throw new t(n,"InvalidParameter",r)})},F.functions.push=function(n,r){return F.standardFunction(n,r,(o,i,u)=>{if(e(u,1,2,n,r),w(u[0]))return u[0][u[0].length]=u[1],u[0].length;throw new t(n,"InvalidParameter",r)})},F.functions.pop=function(n,r){return F.standardFunction(n,r,(o,i,u)=>{if(e(u,1,1,n,r),w(u[0])){if(u[0].length<=0)throw new t(n,"OutOfBounds",r);const e=u[0][u[0].length-1];return u[0].length=u[0].length-1,e}throw new t(n,"InvalidParameter",r)})},F.functions.erase=function(n,r){return F.standardFunction(n,r,(o,i,l)=>{if(e(l,2,2,n,r),w(l[0])){let e=u(l[1]);if(isNaN(e)||!1===m(e))throw new t(n,"InvalidParameter",r);const o=l[0];if(o.length<=0)throw new t(n,"OutOfBounds",r);if(e<0&&(e=o.length+e),e<0)throw new t(n,"OutOfBounds",r);if(e>=o.length)throw new t(n,"OutOfBounds",r);return o.splice(e,1),a}if(s(l[0]))return l[0].eraseField(c(l[1])),a;throw new t(n,"InvalidParameter",r)})},F.functions.insert=function(n,r){return F.standardFunction(n,r,(o,i,l)=>{if(e(l,3,3,n,r),w(l[0])){const e=u(l[1]);if(isNaN(e)||!1===m(e))throw new t(n,"InvalidParameter",r);const o=l[2],i=l[0];if(e>i.length)throw new t(n,"OutOfBounds",r);if(e<0&&e<-1*i.length)throw new t(n,"OutOfBounds",r);return e===i.length?(i[e]=o,a):(i.splice(e,0,o),a)}if(s(l[0]))return l[0].setField(c(l[1]),l[2]),a;throw new t(n,"InvalidParameter",r)})},F.functions.getkeys=function(n,r){return F.standardFunction(n,r,(n,r,o)=>{if(e(o,1,1,n,r),null==o[0])return[];if(s(o[0]))return o[0].keys();throw new t(n,"InvalidParameter",r)})},F.functions.getvalues=function(n,r){return F.standardFunction(n,r,(n,r,o)=>{if(e(o,1,1,n,r),null==o[0])return[];if(s(o[0]))return o[0].values();throw new t(n,"InvalidParameter",r)})},F.functions.resize=function(n,r){return F.standardFunction(n,r,(i,s,c)=>{if(e(c,2,3,n,r),w(c[0])){const e=u(c[1]);if(isNaN(e)||!1===m(e))throw new t(n,"InvalidParameter",r);if(e<0)throw new t(n,"InvalidParameter",r);const i=o(c[2],null),s=c[0];if(s.length>=e)return s.length=e,a;const l=s.length;s.length=e;for(let n=l;n<s.length;n++)s[n]=i;return a}throw new t(n,"InvalidParameter",r)})},F.functions.includes=function(n,r){return F.standardFunction(n,r,(o,u,a)=>{if(e(a,2,2,n,r),w(a[0])){const n=a[1];return!!a[0].some(t=>l(t,n))}if(i(a[0])){const n=a[1];return!!a[0].toArray().some(t=>l(t,n))}throw new t(n,"InvalidParameter",r)})},F.functions.slice=function(n,r){return F.standardFunction(n,r,(a,s,c)=>{if(e(c,1,3,n,r),w(c[0])){const e=u(o(c[1],0)),i=u(o(c[2],c[0].length));if(isNaN(e)||!1===m(e))throw new t(n,"InvalidParameter",r);if(isNaN(i)||!1===m(i))throw new t(n,"InvalidParameter",r);return c[0].slice(e,i)}if(i(c[0])){const e=c[0],i=u(o(c[1],0)),a=u(o(c[2],e.length()));if(isNaN(i)||!1===m(i))throw new t(n,"InvalidParameter",r);if(isNaN(a)||!1===m(a))throw new t(n,"InvalidParameter",r);return e.toArray().slice(i,a)}throw new t(n,"InvalidParameter",r)})},F.functions.splice=function(n,t){return F.standardFunction(n,t,(n,t,r)=>{const e=[];for(let o=0;o<r.length;o++)w(r[o])?e.push(...r[o]):i(r[o])?e.push(...r[o].toArray()):e.push(r[o]);return e})},F.functions.top=function(n,r){return F.standardFunction(n,r,(o,a,s)=>{if(e(s,2,2,n,r),w(s[0]))return u(s[1])>=s[0].length?s[0].slice():s[0].slice(0,u(s[1]));if(i(s[0]))return u(s[1])>=s[0].length()?s[0].slice():s[0].slice(0,u(s[1]));throw new t(n,"InvalidParameter",r)})},F.functions.first=function(n,t){return F.standardFunction(n,t,(r,o,u)=>(e(u,1,1,n,t),w(u[0])?0===u[0].length?null:u[0][0]:i(u[0])?0===u[0].length()?null:u[0].get(0):null))},"sync"===F.mode&&(F.functions.sort=function(n,t){return F.standardFunction(n,t,(t,r,e)=>N(n,r,e,!1))},F.functions.any=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){const t=i(n);if(g(t)&&!0===t)return!0}return!1})},F.functions.all=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){if(!0!==i(n))return!1}return!0})},F.functions.none=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){if(!0===i(n))return!1}return!0})},F.functions.reduce=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,3,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);return 2===o.length?0===u.length?null:u.reduce((n,t)=>{const r=i(n,t);return n=void 0!==r&&r!==a?r:null}):u.reduce((n,t)=>{const r=i(n,t);return n=void 0!==r&&r!==a?r:null},o[2])})},F.functions.map=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r),s=[];for(const n of u){const t=i(n);void 0!==t&&t!==a?s.push(t):s.push(null)}return s})},F.functions.filter=function(n,t){return F.standardFunction(n,t,(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r),a=[];for(const n of u){!0===i(n)&&a.push(n)}return a})}),"async"===F.mode&&(F.functions.sort=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,e)=>N(n,r,e,!0))},F.functions.any=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){const t=await i(n);let r=null;if(r=h(r)?await t:t,g(r)&&!0===r)return!0}return!1})},F.functions.all=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){const t=await i(n);let r=null;if(r=h(r)?await t:t,!0!==r)return!1}return!0})},F.functions.none=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r);for(const n of u){const t=await i(n);let r=null;if(r=h(r)?await t:t,!0===r)return!1}return!0})},F.functions.filter=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r),a=[];for(const n of u){const t=await i(n);let r=null;r=h(r)?await t:t,!0===r&&a.push(n)}return a})},F.functions.reduce=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,i)=>{e(i,2,3,n,r);const u=i[1].createFunction(n),s=p(i[0],n,r);let c;if(i.length>2){const n=o(i[2],null);c=s.reduce(async(n,t)=>{let r=await n;return void 0!==r&&r!==a||(r=null),u(r,t)},Promise.resolve(n))}else{if(0===s.length)return null;c=s.reduce(async(n,t,r)=>{if(r<=1)return u(n,t);let e=await n;return void 0!==e&&e!==a||(e=null),u(e,t)})}return c.then(n=>void 0!==n&&n!==a?n:null)})},F.functions.map=function(n,t){return F.standardFunctionAsync(n,t,async(t,r,o)=>{e(o,2,2,n,r);const i=o[1].createFunction(n),u=p(o[0],n,r),s=[];for(const n of u){const t=await i(n);let r=null;r=h(r)?await t:t,void 0!==r&&r!==a?s.push(r):s.push(null)}return s})})}const p=Object.freeze(Object.defineProperty({__proto__:null,registerFunctions:F},Symbol.toStringTag,{value:"Module"}));export{p as A,F as r};