-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.global.iife.js
More file actions
1 lines (1 loc) · 4.95 KB
/
Copy pathindex.global.iife.js
File metadata and controls
1 lines (1 loc) · 4.95 KB
1
var A1=(function(){let e=e=>{let t=e.length,n=0;for(let r=0;r<t;r++)n+=(e.charCodeAt(r)-64)*26**(t-r-1);return n},t=e=>{let t,n,r,i=0;for(t=0,n=e.length;t<n;t++)r=e.charCodeAt(t),i=i*26+r-65+1;return i},n=e=>{let t=``,n;for(;e>0;)n=(e-1)%26,t=String.fromCharCode(n+65)+t,e=(e-n-1)/26;return t},r=e=>parseInt(e,10),i=e=>String(e),a=e=>typeof e,o=e=>a(e)===`string`,s=e=>a(e)===`number`&&Number.isInteger(e),c=e=>s(e)&&e>0,l=e=>o(e)&&/^[0-9]+$/.test(e)&&c(+e),u=e=>o(e)&&/^[a-z]+$/i.test(e),d=e=>o(e)&&/^[A-Z]+\d+(:[A-Z]+\d+)?$/i.test(e);var f=class extends Error{constructor(e){let t=JSON.stringify(e);super(t),this.name=`A1Error`,this.message=t}s(){return this.message=`Invalid A1 notation: ${this.message}`,this}n(){return this.message=`Invalid A1 number(s): ${this.message}`,this}u(){return this.message=`Invalid A1 argument(s): ${this.message}`,this}};return class p{static _reg=/^([A-Z]+)(\d+)(?::([A-Z]+)(\d+))?$/;_colStart=0;_rowStart=0;_colEnd=0;_rowEnd=0;_converter=1;static _parse(e,t){let[,n,i,a,o]=e.toUpperCase().match(this._reg)??[],s=a||n,c=o||i,l=this._A1Col(n,t),u=this._A1Col(s,t),d=r(i),f=r(c);return{cs:u>l?l:u,rs:f>d?d:f,ce:u>l?u:l,re:f>d?f:d}}static _A1Col(n,r){return r===1?e(n):t(n)}static isValid(e){return d(e)}static getCol(e,t=1){if(!d(e))throw new f(e).s();return this._parse(e,t).cs}static getLastCol(e,t=1){if(!d(e))throw new f(e).s();return this._parse(e,t).ce}static toCol(e){if(!c(e))throw new f(e).n();return n(e)}static getRow(e){if(!d(e))throw new f(e).s();return this._parse(e,1).rs}static getLastRow(e){if(!d(e))throw new f(e).s();return this._parse(e,1).re}static toRow(e){if(!c(e))throw new f(e).n();return i(e)}static getWidth(e,t=1){if(!d(e))throw new f(e).s();let{ce:n,cs:r}=this._parse(e,t);return n-r+1}static getHeight(e){if(!d(e))throw new f(e).s();let{re:t,rs:n}=this._parse(e,1);return t-n+1}_initObject(e){let{a1Start:t,a1End:n,colStart:r,colEnd:i,rowStart:a,rowEnd:o,nCols:s,nRows:m,converter:h}=e;this._converter=h===2?2:1;let g=0,_=0,v=0,y=0,b=(e,t=!0)=>c(e)||l(e)?+e:t&&u(e)?p._A1Col(e,this._converter):0;if(d(t)){let e=p._parse(t,this._converter);g=e.cs,v=e.rs;let n=e.cs===e.ce,r=e.rs===e.re,i=n&&r;(!i||i&&t.includes(`:`))&&(_=e.ce,y=e.re)}if(!g&&r&&(g=b(r)),!v&&a&&(v=b(a,!1)),!_&&!y&&d(n)){let e=p._parse(n,this._converter);_=e.ce,y=e.re}if(!_&&i&&(_=b(i)),!y&&o&&(y=b(o,!1)),!_&&g&&c(s)&&(_=g+s-1),!y&&v&&c(m)&&(y=v+m-1),g&&!_&&(_=g),!g&&_&&(g=_),v&&!y&&(y=v),!v&&y&&(v=y),!g||!v||!_||!y)throw new f(e).u();this._colStart=g,this._rowStart=v,this._colEnd=_,this._rowEnd=y}_initNumber(...e){let[t,n,r,i]=e,a=r||1,o=i||1,s=[t,n,a,o];if(!s.every(e=>c(e)))throw new f(s.join(`, `)).n();this._colStart=t,this._rowStart=n,this._colEnd=t+o-1,this._rowEnd=n+a-1}_initString(...e){let[t,n]=e,r=n?`${t}:${n}`:t;if(!d(r))throw new f(r).s();let{cs:i,rs:a,ce:o,re:s}=p._parse(r,this._converter);this._colStart=i,this._rowStart=a,this._colEnd=o,this._rowEnd=s}constructor(...e){if(!e.length)throw new f().u();let[t]=e;if(t&&a(t)===`object`)this._initObject(t);else if(s(t))this._initNumber(...e);else if(o(t))this._initString(...e);else throw new f(t).u()}get(){let e=n(this._colStart)+i(this._rowStart),t=n(this._colEnd)+i(this._rowEnd);return e===t?e:`${e}:${t}`}toString(){return this.get()}toJSON(){return{colStart:this._colStart,rowStart:this._rowStart,colEnd:this._colEnd,rowEnd:this._rowEnd,a1:this.get(),rowsCount:this._rowEnd-this._rowStart+1,colsCount:this._colEnd-this._colStart+1}}getCol(){return this._colStart}getLastCol(){return this._colEnd}getRow(){return this._rowStart}getLastRow(){return this._rowEnd}getWidth(){return this._colEnd-this._colStart+1}getHeight(){return this._rowEnd-this._rowStart+1}copy(){return new p(this.get())}setCol(e){return this._setFields(e,`_colStart`,`col`)}setLastCol(e){return this._setFields(e,`_colEnd`,`col`)}setRow(e){return this._setFields(e,`_rowStart`,`row`,!1)}setLastRow(e){return this._setFields(e,`_rowEnd`,`row`,!1)}addX(e){return this._addFields(e,`col`)}addY(e){return this._addFields(e,`row`)}add(e,t){return this.addX(e).addY(t)}removeX(e){return this._removeFields(e,`col`)}removeY(e){return this._removeFields(e,`row`)}remove(e,t){return this.removeX(e).removeY(t)}shiftX(e){return this._shiftFields(e,`col`)}shiftY(e){return this._shiftFields(e,`row`)}shift(e,t){return this.shiftX(e).shiftY(t)}_setFields(e,t,n,r=!0){if(c(e)||l(e))this[t]=+e;else if(r&&u(e))this[t]=p._A1Col(e,this._converter);else throw new f(e).u();if(this[`_${n}Start`]>this[`_${n}End`])throw new f(`The first column or row can't be bigger than the last, got: ${e}`);return this}_addFields(e,t){if(!s(e))throw new f(e).u();let n=`_${t}Start`,r=`_${t}End`;return e>=0?this[r]+=e:this[n]+=e,this[n]<=0&&(this[n]=1),this}_removeFields(e,t){if(!s(e))throw new f(e).u();let n=`_${t}Start`,r=`_${t}End`;return e>=0?(this[r]-=e,this[r]<this[n]&&(this[r]=this[n])):(this[n]-=e,this[n]>this[r]&&(this[n]=this[r])),this}_shiftFields(e,t){if(!s(e))throw new f(e).u();let n=`_${t}Start`,r=`_${t}End`,i=this[r]-this[n],a=this[n]+e,o=this[r]+e;return this[n]=a>0?a:1,this[r]=a>0?o:i+1,this}}})();