UNPKG

@modelmap/a1-notation

Version:

Work with A1 notation like "A1" or "A1:B2"

3 lines (2 loc) 6.69 kB
var A1=function(t){"use strict";function r(t){return parseInt(t,10)}function i(t){let r,i="";for(;t>0;)r=(t-1)%26,i=String.fromCharCode(r+65)+i,t=(t-r-1)/26;return i}function s(t){return t.toString()}function n(t){return/^[A-Z]+\d+(:[A-Z]+\d+)?$/i.test(t)}function e(t,r=!0){let i="number"==typeof t&&t%1==0;return r?i&&+t>0:i}class a extends Error{constructor(t){let r=JSON.stringify(t);super(r),this.name="A1Error",this.message=r}wasString(){return this.message="Invalid A1 notation: "+this.message,this}wasNumber(){return this.message="Invalid A1 number(s): "+this.message,this}wasUnknown(){return this.message="Invalid A1 argument(s): "+this.message,this}}class o{constructor(t,r,i,s){if(this._colStart=0,this._rowStart=0,this._colEnd=0,this._rowEnd=0,this._converter=1,!arguments.length)throw(new a).wasUnknown();let n=typeof t;if(t&&"object"===n)this._initObject.apply(this,arguments);else if("number"===n)this._initNumber.apply(this,arguments);else{if("string"!==n)throw new a(t).wasUnknown();this._initString.apply(this,arguments)}}static _parse(t,i){let[,s,n,e,a]=t.toUpperCase().match(this._reg);e=e||s,a=a||n;let o=this._A1Col(s,i),h=this._A1Col(e,i),l=r(n),w=r(a);return{cs:h>o?o:h,rs:w>l?l:w,ce:h>o?h:o,re:w>l?w:l}}static _A1Col(t,r){return 1===r?function(t){let r=0,i=t.length;for(let s=0;s<i;s++)r+=(t.charCodeAt(s)-64)*Math.pow(26,i-s-1);return r}(t):function(t){let r,i,s,n=0,e="A".charCodeAt(0),a="Z".charCodeAt(0)-e+1;for(r=0,i=t.length;r<i;r++)s=t.charCodeAt(r),n=n*a+s-e+1;return n}(t)}static isValid(t){return n(t)}static getCol(t,r=1){if(!n(t))throw new a(t).wasString();return this._parse(t,r).cs}static getLastCol(t,r=1){if(!n(t))throw new a(t).wasString();return this._parse(t,r).ce}static toCol(t){if(!e(t))throw new a(t).wasNumber();return i(t)}static getRow(t){if(!n(t))throw new a(t).wasString();return this._parse(t,1).rs}static getLastRow(t){if(!n(t))throw new a(t).wasString();return this._parse(t,1).re}static toRow(t){if(!e(t))throw new a(t).wasNumber();return s(t)}static getWidth(t,r=1){if(!n(t))throw new a(t).wasString();let{ce:i,cs:s}=this._parse(t,r);return i-s+1}static getHeight(t){if(!n(t))throw new a(t).wasString();let{re:r,rs:i}=this._parse(t,1);return r-i+1}_initObject(...t){const r=(t,r)=>{!Array.isArray(r)&&(r=[r]);for(let i in t)if(-1===r.indexOf(i)&&t[i].val)return!1;return!0};let i=t[0],{converter:s}=i;if(s&&-1===[1,2].indexOf(s))throw new a({converter:s}).wasUnknown();this._converter=s||1;let n={};["colStart","rowStart","colEnd","rowEnd","a1Start","a1End","nCols","nRows"].forEach(t=>{let r=i[t];var s;n[t]={isString:(s=r,"string"==typeof s),isNumber:e(r),val:r}});let{colStart:o,rowStart:h,colEnd:l,rowEnd:w,a1Start:c,a1End:u,nCols:S,nRows:_}=n;switch(!0){case c.isString&&r(n,"a1Start"):return this._initString(c.val);case c.isString&&u.isString&&r(n,["a1Start","a1End"]):return this._initString(c.val,u.val);case o.isString&&h.isString&&r(n,["colStart","rowStart"]):return this._initString(o.val+h.val);case o.isNumber&&h.isNumber&&r(n,["colStart","rowStart"]):return this._initNumber(o.val,h.val);case o.isNumber&&h.isNumber&&_.isNumber&&r(n,["colStart","rowStart","nRows"]):return this._initNumber(o.val,h.val,_.val);case o.isNumber&&h.isNumber&&S.isNumber&&r(n,["colStart","rowStart","nCols"]):return this._initNumber(o.val,h.val,1,S.val);case o.isNumber&&h.isNumber&&_.isNumber&&S.isNumber&&r(n,["colStart","rowStart","nRows","nCols"]):return this._initNumber(o.val,h.val,_.val,S.val);case o.isString&&h.isString&&l.isString&&r(n,["colStart","rowStart","colEnd"]):return this._initString(`${o.val}${h.val}:${l.val}${h.val}`);case o.isNumber&&h.isNumber&&l.isNumber&&r(n,["colStart","rowStart","colEnd"]):return this._initNumber(o.val,h.val,1,l.val-o.val+1);case o.isString&&h.isString&&w.isString&&r(n,["colStart","rowStart","rowEnd"]):return this._initString(`${o.val}${h.val}:${o.val}${w.val}`);case o.isNumber&&h.isNumber&&w.isNumber&&r(n,["colStart","rowStart","rowEnd"]):return this._initNumber(o.val,h.val,w.val-h.val+1);case o.isString&&h.isString&&l.isString&&w.isString&&r(n,["colStart","rowStart","colEnd","rowEnd"]):return this._initString(`${o.val}${h.val}:${l.val}${w.val}`);case o.isNumber&&h.isNumber&&l.isNumber&&w.isNumber&&r(n,["colStart","rowStart","colEnd","rowEnd"]):return this._initNumber(o.val,h.val,w.val-h.val+1,l.val-o.val+1);default:throw new a(i).wasUnknown()}}_initNumber(...t){let[r,i,s,n]=t;s=s||1,n=n||1;let o=[r,i,s,n];if(!o.every(t=>e(t)))throw new a(o.join(", ")).wasNumber();this._colStart=r,this._rowStart=i,this._colEnd=r+n-1,this._rowEnd=i+s-1}_initString(...t){let[r,i]=t,s=i?`${r}:${i}`:r;if(!n(s))throw new a(s).wasString();let{cs:e,rs:h,ce:l,re:w}=o._parse(s,this._converter);this._colStart=e,this._rowStart=h,this._colEnd=l,this._rowEnd=w}get(){let t=i(this._colStart)+s(this._rowStart),r=i(this._colEnd)+s(this._rowEnd);return t===r?t:`${t}:${r}`}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 o(this.get())}addX(t){if(!e(t,!1))throw new a(t).wasUnknown();return t>=0?this._colEnd+=t:this._colStart+=t,this._colStart<=0&&(this._colStart=1),this}addY(t){if(!e(t,!1))throw new a(t).wasUnknown();return t>=0?this._rowEnd+=t:this._rowStart+=t,this._rowStart<=0&&(this._rowStart=1),this}add(t,r){return this.addX(t),this.addY(r),this}removeX(t){if(!e(t,!1))throw new a(t).wasUnknown();return t>=0?(this._colEnd-=t,this._colEnd<this._colStart&&(this._colEnd=this._colStart)):(this._colStart-=t,this._colStart>this._colEnd&&(this._colStart=this._colEnd)),this}removeY(t){if(!e(t,!1))throw new a(t).wasUnknown();return t>=0?(this._rowEnd-=t,this._rowEnd<this._rowStart&&(this._rowEnd=this._rowStart)):(this._rowStart-=t,this._rowStart>this._rowEnd&&(this._rowStart=this._rowEnd)),this}remove(t,r){return this.removeX(t),this.removeY(r),this}shiftX(t){if(!e(t,!1))throw new a(t).wasUnknown();let r=this._colEnd-this._colStart,i=this._colStart+t,s=this._colEnd+t;return this._colStart=i>0?i:1,this._colEnd=i>0?s:r+1,this}shiftY(t){if(!e(t,!1))throw new a(t).wasUnknown();let r=this._rowEnd-this._rowStart,i=this._rowStart+t,s=this._rowEnd+t;return this._rowStart=i>0?i:1,this._rowEnd=i>0?s:r+1,this}shift(t,r){return this.shiftX(t),this.shiftY(r),this}}return o._reg=/^([A-Z]+)(\d+)(?::([A-Z]+)(\d+))?$/,t.A1=o,t}({}); //# sourceMappingURL=index.next.min.js.map