UNPKG

@nextgis/utils

Version:
3 lines (2 loc) 9.61 kB
var Utils=function(t){"use strict";function e(t){return function(t,e=[]){for(;null!==t;t=Object.getPrototypeOf(t)){const n=Object.getOwnPropertyNames(t);for(let t=0;t<n.length;t++)-1==e.indexOf(n[t])&&e.push(n[t])}return e}(t)}function n(t,e){return t.length===e.length&&t.every(function(t,n){return t===e[n]})}var r=Object.defineProperty,o=(t,e,n)=>((t,e,n)=>e in t?r(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n)(t,e+"",n);class i{constructor(t){o(this,"silent",!0),t&&this.copy(t)}static copy(t){return(new i).copy(t)}copy(t){try{return navigator.clipboard?navigator.clipboard.writeText(t):window.clipboardData?window.clipboardData.setData("text",t):this.copyToClipboard(t),this.silent||console.log("Copied to Clipboard"),!0}catch(e){this.silent||console.log("Please copy manually")}return!1}copyToClipboard(t){const e=document.createElement("input");e.value=t;try{document.body.appendChild(e),this.copyNodeContentsToClipboard(e)}finally{document.body.removeChild(e)}}copyNodeContentsToClipboard(t){t.select(),t.setSelectionRange(0,99999),document.execCommand("copy")}}function c(t,e=10){let n;function r(...o){r.clear(),n=setTimeout(()=>{n=null,t.apply(this,o)},e)}return r.clear=function(){n&&(clearTimeout(n),n=null)},r}function a(t){return null!=t}var s=Object.defineProperty,u=(t,e,n)=>((t,e,n)=>e in t?s(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n)(t,e+"",n);const f=6371;function l(t,e){p(t,t=>{if("coordinates"in t)if("Polygon"===t.type||"MultiLineString"===t.type)for(const n of t.coordinates)n.forEach(t=>e(t));else if("MultiPolygon"===t.type)for(const n of t.coordinates)n.forEach(t=>t.forEach(t=>e(t)));else if("Point"===t.type)e(t.coordinates);else if("MultiPoint"===t.type||"LineString"===t.type)for(const n of t.coordinates)e(n);return t})}function p(t,e){if("FeatureCollection"===t.type)for(const n of t.features)e(n.geometry);else"Feature"===t.type?e(t.geometry):"coordinates"in t&&e(t)}function y(t){return{type:"Polygon",coordinates:[g(t)]}}function g(t){const e=[t[0],t[1]];return[e,[t[2],t[1]],[t[2],t[3]],[t[0],t[3]],e]}const d=Math.PI/180,h=180/Math.PI;function b(t,e,n=10,r=6){const o=n/f*h,i=o/Math.cos(e*d),c=[];for(let a=0;a<r+1;a++){const n=Math.PI*(a/(r/2)),s=t+i*Math.cos(n),u=e+o*Math.sin(n);c.push([s,u])}return c}const m=Math.PI/180,j=180/Math.PI;function P(t,e,n,r){if(t instanceof Array)return e instanceof Array&&e.sort().join("")===t.sort().join("");if(t instanceof Date)return e instanceof Date&&""+t==""+e;if(t instanceof Function){if(!(e instanceof Function))return!1}else if(t instanceof Object)return e instanceof Object&&(t===n?e===r:O(t,e));return t===e}function O(t,e){const n=Object.keys(t).sort(),r=Object.keys(e).sort();if(n.length!==r.length)return!1;if(n.join("")!==r.join(""))return!1;for(let o=0;o<n.length;o++){if(!P(t[n[o]],e[r[o]],t,e))return!1}return!0}const M="undefined"!=typeof window&&void 0!==window.document,A=M?"browser":"node";function v(t,e){return("string"==typeof e||"number"==typeof e)&&e in t}function w(t){return"boolean"==typeof t||"number"==typeof t||"string"==typeof t||null===t||(S(t)?E(t):!!L(t)&&C(t))}function C(t){return!!L(t)&&t.every(w)}function E(t){if(S(t))for(const e in t)if(!w(e))return!1;return!1}function S(t){return"[object Object]"===Object.prototype.toString.call(t)}function L(t){return"[object Array]"===Object.prototype.toString.call(t)}return t.Clipboard=i,t.DebounceDecorator=function(t=10){return function(e,n,r){return{configurable:!0,enumerable:r.enumerable,get:function(){return Object.defineProperty(this,n,{configurable:!0,enumerable:r.enumerable,value:c(r.value,t)}),this[n]}}}},t.EARTHS_RADIUS=f,t.Events=class{constructor(t){this.emitter=t,u(this,"_eventsStatus",{})}setEventStatus(t,e){this._eventsStatus[t]=e}onLoad(t){const e=(Array.isArray(t)?t:[t]).map(t=>new Promise(e=>{if(this.getEventStatus(t))e(this);else{this.emitter.once(t,()=>{this.setEventStatus(t,!0),e(this)})}}));return Promise.all(e).then(()=>this)}getEventStatus(t){const e=this._eventsStatus[t];return void 0!==e&&!!e}},t.allProperties=e,t.applyMixins=function(t,n,r={}){const o=e(t.prototype),i=void 0===r.replace||r.replace;n.forEach(e=>{Object.getOwnPropertyNames(e.prototype).forEach(n=>{const r=-1!==o.indexOf(n);if(!i&&!r||i){const r=Object.getOwnPropertyDescriptor(e.prototype,n);r&&Object.defineProperty(t.prototype,n,r)}})})},t.arrayChunk=function(t,e){return Array.from({length:Math.ceil(t.length/e)},(n,r)=>t.slice(r*e,r*e+e))},t.arrayCompare=function(t,e){return n(t=Array.from(t).sort(),e=Array.from(e).sort())},t.arrayCompareStrict=function(t,e){return n(t=Array.from(t),e=Array.from(e))},t.arrayUnique=function(t){return t.filter((t,e,n)=>n.indexOf(t)==e)},t.camelize=function(t,e=/[_.\- ]/){const n=t.split(e);let r="";for(let o=0;o<n.length;o++){const t=n[o];r+=t.charAt(0).toUpperCase()+t.slice(1)}return r},t.capitalize=function(t){return(t=String(t).toLowerCase())[0].toUpperCase()+t.slice(1)},t.checkExtent=function(t){const[e,n,r,o]=t,i=e<r&&n<o;if(!i){console.log("Error: The extent coordinates are not in the correct order."),console.log(`Received extent: [${e}, ${n}, ${r}, ${o}]`);const t=[Math.min(e,r),Math.min(n,o),Math.max(e,r),Math.max(o,n)];console.log(`Expected order: [${t.join(", ")}]`)}const c=e>=-180&&r<=180,a=n>=-90&&o<=90;return c&&a||console.log("Warning: The coordinates may not be within valid geographic ranges."),i&&c&&a},t.coordinatesCount=function(t){let e=0;return l(t,()=>e++),e},t.debounce=c,t.debugLog=function(t){return!1},t.deepmerge=function t(e,n,r=!1){let o=e;const i=n,c=Array.isArray(i);let a=c&&[]||{};return c&&Array.isArray(i)?r?(o=o||[],a=a.concat(o),i.forEach((e,n)=>{void 0===a[n]?a[n]=e:"object"==typeof e?a[n]=t(o[n],e,r):-1===o.indexOf(e)&&a.push(e)})):a=i:(o&&"object"==typeof o&&Object.keys(o).forEach(function(t){a[t]=o[t]}),Object.keys(i).forEach(function(e){a[e]="object"==typeof i[e]&&i[e]&&"object"==typeof o[e]&&"object"==typeof i[e]?t(o[e],i[e],r):i[e]})),a},t.defined=a,t.degrees2Radian=function(t){return t*Math.PI/180},t.degrees2meters=function(t,e){e=e>85.06?85.06:e<-85.06?-85.06:e;const n=20037508.34*t/180;let r=Math.log(Math.tan((90+e)*Math.PI/360))/(Math.PI/180);return r=20037508.34*r/180,[n,r]},t.deprecatedMapClick=function(t){if(!t.lngLat&&t.latLng){t.lngLat=[t.latLng.lng,t.latLng.lat]}return t},t.deprecatedWarn=function(t){console.warn(`DEPRECATED WARN: ${t}`)},t.eachCoordinates=l,t.eachGeometry=p,t.fixUrlStr=function(t){return t.replace(/([^:]\/)\/+/g,"$1")},t.flatten=function(t,e={}){var n;const r=null==(n=e.flatArray)||n,o={};return function t(e,n){if(Object(e)!==e)o[n]=e;else if(Array.isArray(e)&&r){const r=e.length;for(let o=0;o<r;o++)t(e[o],n+"["+o+"]");0===r&&(o[n]=[])}else{let r=!0;for(const o in e)r=!1,t(e[o],n?n+"."+o:o);r&&n&&(o[n]={})}}(t,""),o},t.full=function(t){return"string"==typeof t?!!t:a(t)},t.getBoundsCoordinates=g,t.getBoundsFeature=function(t){return{type:"Feature",properties:{},geometry:y(t)}},t.getBoundsPolygon=y,t.getCircleFeature=function(t,e,n=10,r=6){return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[b(t,e,n,r)]}}},t.getCirclePolygonCoordinates=b,t.getCoordinates=function(t){const e=[];return l(t,t=>e.push(t)),e},t.getGlobalVariable=function(){return M?window:global},t.getIdentifyRadius=function(t,e,n){return(n=null!=n?n:10)*(40075016.686*Math.abs(Math.cos(180*t[1]/Math.PI))/Math.pow(2,e+8))*5e-4},t.getPolygons=function(t){const e=[];return p(t,t=>{if("coordinates"in t)if("Polygon"===t.type)t.coordinates.forEach(t=>e.push(t));else if("MultiPolygon"===t.type)for(const n of t.coordinates)for(const t of n)e.push(t);return t}),e},t.getSquarePolygonCoordinates=function(t,e,n=10){const r=n/f*j,o=r/Math.cos(e*m),i=[t-o,e+r];return[i,[t+o,e+r],[t+o,e-r],[t-o,e-r],i]},t.isAnyJson=w,t.isArray=L,t.isBrowser=M,t.isJsonArray=C,t.isJsonMap=E,t.isLngLatBoundsArray=function(t){return Array.isArray(t)&&4===t.length&&t.every(t=>"number"==typeof t)},t.isObjKey=v,t.isObject=S,t.keyInObj=function(t,e){return v(t,e)},t.latLngToLngLatArray=function(t){return[t.lng,t.lat]},t.lngLatArrayToLatLng=function(t){return{lat:t[1],lng:t[0]}},t.meters2degrees=function(t,e){return[180*t/20037508.34,360*Math.atan(Math.exp(e*Math.PI/20037508.34))/Math.PI-90]},t.mixinProperties=function(t,e,n){n.forEach(n=>{const r=Object.getOwnPropertyDescriptor(e.prototype,n);r&&Object.defineProperty(t.prototype,n,r)})},t.numberWithSpaces=function(t){const e=t.toString().split(".");return e[0]=e[0].replace(/\B(?=(\d{3})+(?!\d))/g," "),e.join(".")},t.objectAssign=function(t,...e){for(const n of e)for(const e of Object.getOwnPropertyNames(n))t[e]=n[e]},t.objectDeepEqual=O,t.objectRemoveEmpty=function t(e){const n={};return Object.keys(e).forEach(r=>{e[r]instanceof Array||e[r]!==Object(e[r])?void 0!==e[r]&&(n[r]=e[r]):n[r]=t(e[r])}),n},t.reEscape=function(t){return t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&")},t.round=function(t,e){const n=e?Number("1e+"+e):1;return Math.round((t+Number.EPSILON)*n)/n},t.sleep=function(t=0){return new Promise(e=>setTimeout(e,t))},t.type=A,t.unflatten=function(t){if(Object(t)!==t||Array.isArray(t))return t;const e=/\.?([^.[\]]+)|\[(\d+)\]/g,n={};for(const r in t){let o,i=n,c="";for(;o=e.exec(r);)i=i[c]||(i[c]=o[2]?[]:{}),c=o[2]||o[1];i[c]=t[r]}return n[""]||n},t.updateUrlParams=function(t,e){const n=new URL(t),r=new URLSearchParams(n.search);Object.entries(e).forEach(([t,e])=>{void 0===e?r.delete(t):r.set(t,e)}),r.set("timestamp",String((new Date).getTime()));const o=[...r.entries()].map(([t,e])=>`${t}=${e}`).join("&");return`${n.origin}${n.pathname}?${o}`},t}({}); //# sourceMappingURL=utils.global.prod.js.map