@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 3.02 kB
JavaScript
import{FeatureSetError as t,FeatureSetErrorCodes as n}from"./errorsupport.js";import{isNumber as e,isString as r,equalityTest as a}from"./shared.js";import{isSingleField as c,predictType as u}from"./sqlUtils.js";function s(t){return t=+t,isFinite(t)?t-t%1||(t<0?-0:0===t?t:0):t}function i(t){let n=0;for(let e=0;e<t.length;e++)n+=t[e];return n/t.length}function l(t){const n=i(t);let e=0;for(let r=0;r<t.length;r++)e+=(n-t[r])**2;return e/t.length}function o(t){const n=i(t);let e=0;for(let r=0;r<t.length;r++)e+=(n-t[r])**2;return e/(t.length-1)}function h(t){let n=0;for(let e=0;e<t.length;e++)n+=t[e];return n}function f(t,n){const c=[],u={},s=[];for(let i=0;i<t.length;i++){if(void 0!==t[i]&&null!==t[i]){const n=t[i];if(e(n)||r(n))void 0===u[n]&&(c.push(n),u[n]=1);else{let t=!1;for(let e=0;e<s.length;e++)!0===a(s[e],n)&&(t=!0);!1===t&&(s.push(n),c.push(n))}}if(c.length>=n&&-1!==n)return c}return c}function m(t){switch(t.toLowerCase()){case"distinct":return"distinct";case"avg":case"mean":return"avg";case"min":return"min";case"sum":return"sum";case"max":return"max";case"stdev":case"stddev":return"stddev";case"var":case"variance":return"var";case"count":return"count"}return""}function g(t,n,e=1e3){switch(t.toLowerCase()){case"distinct":return f(n,e);case"avg":case"mean":return i(n);case"min":return Math.min.apply(Math,n);case"sum":return h(n);case"max":return Math.max.apply(Math,n);case"stdev":case"stddev":return Math.sqrt(l(n));case"var":case"variance":return l(n);case"count":return n.length}return 0}async function w(t,n,e){const r=await k(t,n,e,!0);return 0===r.length?null:Math.min.apply(Math,r)}async function d(t,n,e){const r=await k(t,n,e,!0);return 0===r.length?null:Math.max.apply(Math,r)}async function p(t,n,e){let r="";n&&!c(n)&&(r=u(n,t.fields));const a=await k(t,n,e,!0);if(0===a.length)return null;const l=i(a);return null===l?l:"integer"===r?s(l):l}async function v(t,n,e){const r=await k(t,n,e,!0);return 0===r.length?null:o(r)}async function y(t,n,e){const r=await k(t,n,e,!0);return 0===r.length?null:Math.sqrt(o(r))}async function x(t,n,e){const r=await k(t,n,e,!0);return 0===r.length?null:h(r)}async function M(t,n){return t.iterator(n).count()}async function k(e,r,a,c=!1){const u=e.iterator(a),s=[],i={ticker:0};let l=await u.next();for(;null!==l;){if(i.ticker++,a.aborted)throw new t(n.Cancelled);i.ticker%100==0&&(i.ticker=0,await new Promise((t=>{setTimeout(t,0)})));const e=r?.calculateValue(l);null===e?!1===c&&(s[s.length]=e):s[s.length]=e,l=await u.next()}return s}async function C(e,r,a=1e3,c=null){const u=e.iterator(c),s=[],i={},l={ticker:0};let o=await u.next();for(;null!==o;){if(l.ticker++,c&&c.aborted)throw new t(n.Cancelled);l.ticker%100==0&&(l.ticker=0,await new Promise((t=>{setTimeout(t,0)})));const e=r?.calculateValue(o);if(null!=e&&void 0===i[e]&&(s.push(e),i[e]=1),s.length>=a&&-1!==a)return s;o=await u.next()}return s}export{g as calculateStat,M as count,m as decodeStatType,C as distinct,d as max,p as mean,w as min,y as stdev,x as sum,v as variance};