@iota/area-codes
Version:
IOTA Area Codes (IAC) JavaScript
2 lines (1 loc) • 9.68 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).IotaAreaCodes={})}(this,function(e){"use strict";var t=function(){function e(){}return e.NORMAL=10,e.EXTRA=11,e}();var r,i=function(e,t){return e(t={exports:{}},t.exports),t.exports}(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});const r=90,i=180;class n{constructor(e,t,n,o,d){this.latitudeLo=e,this.longitudeLo=t,this.latitudeHi=n,this.longitudeHi=o,this.codeLength=d,this.latitudeCenter=Math.min(e+(n-e)/2,r),this.longitudeCenter=Math.min(t+(o-t)/2,i)}getLatitudeHeight(){return this.latitudeHi-this.latitudeLo}getLongitudeWidth(){return this.longitudeHi-this.longitudeLo}}t.CodeArea=n;class o{constructor(e){this.code=e}getCode(){return this.code}isPadded(){return this.code.indexOf(o.PADDING_CHARACTER_)>=0}static isValid(e){if(!e)return!1;if(-1===e.indexOf(o.SEPARATOR_))return!1;if(e.indexOf(o.SEPARATOR_)!==e.lastIndexOf(o.SEPARATOR_))return!1;if(1===e.length)return!1;if(e.indexOf(o.SEPARATOR_)>o.SEPARATOR_POSITION_||e.indexOf(o.SEPARATOR_)%2==1)return!1;if(e.indexOf(o.PADDING_CHARACTER_)>-1){if(0===e.indexOf(o.PADDING_CHARACTER_))return!1;const t=e.match(new RegExp("("+o.PADDING_CHARACTER_+"+)","g"));if(t.length>1||t[0].length%2==1||t[0].length>o.SEPARATOR_POSITION_-2)return!1;if(e.charAt(e.length-1)!==o.SEPARATOR_)return!1}if(e.length-e.indexOf(o.SEPARATOR_)-1==1)return!1;const t=e.replace(new RegExp("\\"+o.SEPARATOR_+"+"),"").replace(new RegExp(o.PADDING_CHARACTER_+"+"),"");for(let e=0,r=t.length;e<r;e++){const r=t.charAt(e).toUpperCase();if(r!==o.SEPARATOR_&&-1===o.CODE_ALPHABET_.indexOf(r))return!1}return!0}static isPadded(e){return new o(e).isPadded()}static isShort(e){return!!o.isValid(e)&&(e.indexOf(o.SEPARATOR_)>=0&&e.indexOf(o.SEPARATOR_)<o.SEPARATOR_POSITION_)}static isFull(e){if(!o.isValid(e))return!1;if(o.isShort(e))return!1;if(o.CODE_ALPHABET_.indexOf(e.charAt(0).toUpperCase())*o.ENCODING_BASE_>=2*o.LATITUDE_MAX_)return!1;if(e.length>1){if(o.CODE_ALPHABET_.indexOf(e.charAt(1).toUpperCase())*o.ENCODING_BASE_>=2*o.LONGITUDE_MAX_)return!1}return!0}contains(e,t){const r=o.decode(this.getCode());return r.latitudeLo<=e&&e<r.latitudeHi&&r.longitudeLo<=t&&t<r.longitudeHi}static encode(e,t,r=o.CODE_PRECISION_NORMAL){if(r<2||r<o.PAIR_CODE_LENGTH_&&r%2==1)throw new Error("IllegalArgumentException: Invalid Open Location Code length");r=Math.min(o.MAX_DIGIT_COUNT,r);let i=o.clipLatitude(e);const n=o.normalizeLongitude(t);90===i&&(i-=o.computeLatitudePrecision(r));let d=o.encodePairs(i,n,Math.min(r,o.PAIR_CODE_LENGTH_));return r>o.PAIR_CODE_LENGTH_&&(d+=o.encodeGrid(i,n,r-o.PAIR_CODE_LENGTH_)),d}static decode(e){if(!o.isFull(e))throw new Error("IllegalArgumentException: Passed Open Location Code is not a valid full code: "+e);let t=e.replace(o.SEPARATOR_,"");t=(t=t.replace(new RegExp(o.PADDING_CHARACTER_+"+"),"")).toUpperCase();const r=o.decodePairs(t.substring(0,o.PAIR_CODE_LENGTH_));if(t.length<=o.PAIR_CODE_LENGTH_)return r;const i=o.decodeGrid(t.substring(o.PAIR_CODE_LENGTH_,o.MAX_DIGIT_COUNT));return new n(r.latitudeLo+i.latitudeLo,r.longitudeLo+i.longitudeLo,r.latitudeLo+i.latitudeHi,r.longitudeLo+i.longitudeHi,r.codeLength+i.codeLength)}static recoverNearest(e,t,r){if(!o.isShort(e)){if(o.isFull(e))return e;throw new Error("ValueError: Passed short code is not valid: "+e)}const i=o.clipLatitude(t),n=o.normalizeLongitude(r),d=e.toUpperCase(),s=o.SEPARATOR_POSITION_-d.indexOf(o.SEPARATOR_),_=Math.pow(20,2-s/2),a=_/2,u=o.decode(o.encode(i,n).substr(0,s)+d);return i+a<u.latitudeCenter&&u.latitudeCenter-_>=-o.LATITUDE_MAX_?u.latitudeCenter-=_:i-a>u.latitudeCenter&&u.latitudeCenter+_<=o.LATITUDE_MAX_&&(u.latitudeCenter+=_),n+a<u.longitudeCenter?u.longitudeCenter-=_:n-a>u.longitudeCenter&&(u.longitudeCenter+=_),o.encode(u.latitudeCenter,u.longitudeCenter,u.codeLength)}static shorten(e,t,r){if(!o.isFull(e))throw new Error("ValueError: Passed code is not valid and full: "+e);if(-1!==e.indexOf(o.PADDING_CHARACTER_))throw new Error("ValueError: Cannot shorten padded codes: "+e);const i=e.toUpperCase(),n=o.decode(i);if(n.codeLength<o.MIN_TRIMMABLE_CODE_LEN_)throw new Error("ValueError: Code length must be at least "+o.MIN_TRIMMABLE_CODE_LEN_);const d=o.clipLatitude(t),s=o.normalizeLongitude(r),_=Math.max(Math.abs(n.latitudeCenter-d),Math.abs(n.longitudeCenter-s));for(let e=o.PAIR_RESOLUTIONS_.length-2;e>=1;e--)if(_<.3*o.PAIR_RESOLUTIONS_[e])return i.substring(2*(e+1));return i}static clipLatitude(e){return Math.min(90,Math.max(-90,e))}static computeLatitudePrecision(e){return e<=10?Math.pow(20,Math.floor(e/-2+2)):Math.pow(20,-3)/Math.pow(o.GRID_ROWS_,e-10)}static normalizeLongitude(e){let t=e;for(;t<-180;)t+=360;for(;t>=180;)t-=360;return t}static encodePairs(e,t,r){let i="",n=e+o.LATITUDE_MAX_,d=t+o.LONGITUDE_MAX_,s=0;for(;s<r;){const e=o.PAIR_RESOLUTIONS_[Math.floor(s/2)];let t=Math.floor(n/e);n-=t*e,i+=o.CODE_ALPHABET_.charAt(t),s+=1,d-=(t=Math.floor(d/e))*e,i+=o.CODE_ALPHABET_.charAt(t),(s+=1)===o.SEPARATOR_POSITION_&&s<r&&(i+=o.SEPARATOR_)}return i.length<o.SEPARATOR_POSITION_&&(i+=Array(o.SEPARATOR_POSITION_-i.length+1).join(o.PADDING_CHARACTER_)),i.length===o.SEPARATOR_POSITION_&&(i+=o.SEPARATOR_),i}static encodeGrid(e,t,r){let i="",n=o.GRID_SIZE_DEGREES_,d=o.GRID_SIZE_DEGREES_,s=e+o.LATITUDE_MAX_,_=t+o.LONGITUDE_MAX_,a=(s%=1)%n,u=(_%=1)%d;for(let e=0;e<r;e++){const e=Math.floor(a/(n/o.GRID_ROWS_)),t=Math.floor(u/(d/o.GRID_COLUMNS_));a-=e*(n/=o.GRID_ROWS_),u-=t*(d/=o.GRID_COLUMNS_),i+=o.CODE_ALPHABET_.charAt(e*o.GRID_COLUMNS_+t)}return i}static decodePairs(e){const t=o.decodePairsSequence(e,0),r=o.decodePairsSequence(e,1);return new n(t[0]-o.LATITUDE_MAX_,r[0]-o.LONGITUDE_MAX_,t[1]-o.LATITUDE_MAX_,r[1]-o.LONGITUDE_MAX_,e.length)}static decodePairsSequence(e,t){let r=0,i=0;for(;2*r+t<e.length;)i+=o.CODE_ALPHABET_.indexOf(e.charAt(2*r+t))*o.PAIR_RESOLUTIONS_[r],r+=1;return[i,i+o.PAIR_RESOLUTIONS_[r-1]]}static decodeGrid(e){let t=0,r=0,i=o.GRID_SIZE_DEGREES_,d=o.GRID_SIZE_DEGREES_,s=0;for(;s<e.length;){const n=o.CODE_ALPHABET_.indexOf(e.charAt(s)),_=Math.floor(n/o.GRID_COLUMNS_),a=n%o.GRID_COLUMNS_;t+=_*(i/=o.GRID_ROWS_),r+=a*(d/=o.GRID_COLUMNS_),s+=1}return new n(t,r,t+i,r+d,e.length)}}o.CODE_PRECISION_NORMAL=10,o.CODE_PRECISION_EXTRA=11,o.MAX_DIGIT_COUNT=15,o.SEPARATOR_="+",o.SEPARATOR_POSITION_=8,o.PADDING_CHARACTER_="0",o.CODE_ALPHABET_="23456789CFGHJMPQRVWX",o.ENCODING_BASE_=o.CODE_ALPHABET_.length,o.LATITUDE_MAX_=r,o.LONGITUDE_MAX_=i,o.PAIR_CODE_LENGTH_=10,o.PAIR_RESOLUTIONS_=[20,1,.05,.0025,125e-6],o.GRID_COLUMNS_=4,o.GRID_ROWS_=5,o.GRID_SIZE_DEGREES_=125e-6,o.MIN_TRIMMABLE_CODE_LEN_=6,t.default=o}),n=(r=i)&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r,o=(i.CodeArea,"23456789CFGHJMPQRVWX0+"),d="FGHJKLMNOPQRSTUVXWYZA9";function s(e){for(var t="",r=0;r<e.length;r++){var i=d.indexOf(e[r]);t+=o[i]}return t}function _(e){var t=new RegExp("^["+d+"]*$").test(e);return t&&(t=n.isFull(s(e))),t}function a(e,r,i){if(void 0===i&&(i=t.NORMAL),i<2)throw new Error("The precision is not valid");return c(n.encode(e,r,i))}function u(e){var t=n.decode(l(e));return{latitude:t.latitudeCenter,longitude:t.longitudeCenter,codePrecision:t.codeLength,latitudeLow:t.latitudeLo,latitudeHigh:t.latitudeHi,longitudeLow:t.longitudeLo,longitudeHigh:t.longitudeHi}}function c(e){if(!n.isValid(e))throw new Error("The openLocationCode is not valid");for(var t="",r=0;r<e.length;r++){var i=o.indexOf(e[r]);t+=d[i]}return t}function l(e){if(!_(e))throw new Error("The iotaAreaCode is not valid");return s(e)}var A=[2,4,6,8,10,11];function E(e,t,r){if(A.indexOf(t)<0)throw new Error("codePrecision must be one of "+A.join(", "));if(t===r.codePrecision)return e;if(t<r.codePrecision){var i=e.replace("9","").substr(0,t);return t<=8?""+i+"A".repeat(8-t)+"9":i.substr(0,8)+"9"+i.substr(8)}return a(r.latitude,r.longitude,t)}e.CodePrecision=t,e.PRECISIONS=A,e.decode=u,e.decreasePrecision=function(e){var t=u(e);if(t.codePrecision<=A[0])throw new Error("The precision can not be decreased any more");return E(e,A[A.indexOf(t.codePrecision)-1],t)},e.encode=a,e.extract=function(e){var t="(["+d.substr(0,9)+"]["+d.substr(0,18)+"]["+d.substr(0,21)+"]{6}9(?:["+d.substr(0,20)+"]{2,3})?)",r=new RegExp(t).exec(e);return r?r[1]:void 0},e.fromOpenLocationCode=c,e.getPrecisionDimensions=function(e){var t={2:{blocksSizeDegrees:20,blocksSizeDegreesFormatted:"20°",sizeMetres:22e5,sizeMetresFormatted:"2200km"},4:{blocksSizeDegrees:1,blocksSizeDegreesFormatted:"1°",sizeMetres:11e4,sizeMetresFormatted:"110km"},6:{blocksSizeDegrees:.05,blocksSizeDegreesFormatted:"0.05°",sizeMetres:5500,sizeMetresFormatted:"110km"},8:{blocksSizeDegrees:.0025,blocksSizeDegreesFormatted:"0.0025°",sizeMetres:275,sizeMetresFormatted:"275m"},10:{blocksSizeDegrees:125e-6,blocksSizeDegreesFormatted:"0.000125°",sizeMetres:14,sizeMetresFormatted:"14m"},11:{blocksSizeDegrees:void 0,blocksSizeDegreesFormatted:void 0,sizeMetres:3.5,sizeMetresFormatted:"3.5m"}};if(!t[e])throw new Error("codePrecision must be 2, 4, 6, 8, 10 or 11");return t[e]},e.increasePrecision=function(e){var t=u(e);if(t.codePrecision>=A[A.length-1])throw new Error("The precision can not be increased any more");return E(e,A[A.indexOf(t.codePrecision)+1],t)},e.internalSetPrecision=E,e.isValid=_,e.isValidPartial=function(e){if(null==e||"string"!=typeof e||e.length>9)return!1;if(e.endsWith("AA9")){var t=e.replace(/A*9$/,"");return!(t.length<2||t.length%2==1)&&new RegExp("^["+d.substr(0,20)+"]*$").test(t)}return!1},e.setPrecision=function(e,t){return E(e,t,u(e))},e.toOpenLocationCode=l,Object.defineProperty(e,"__esModule",{value:!0})});