turbocommons-ts
Version:
General purpose library that implements frequently used and generic software development tasks
1 lines • 6.57 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TableObject=void 0;var StringUtils_1=require("../utils/StringUtils"),ArrayUtils_1=require("../utils/ArrayUtils"),NumericUtils_1=require("../utils/NumericUtils"),HashMapObject_1=require("./HashMapObject"),TableObject=function(){function t(t,r){if(void 0===t&&(t=0),void 0===r&&(r=0),this._columnsCount=0,this._rowsCount=0,!(NumericUtils_1.NumericUtils.isInteger(t)&&t>=0))throw new Error("constructor rows must be a positive integer");if(this._rowsCount=t,NumericUtils_1.NumericUtils.isInteger(r)&&r>=0)this._columnsCount=Number(r);else{if(!ArrayUtils_1.ArrayUtils.isArray(r))throw new Error("constructor columns must be an integer or an array of strings");this._columnsCount=r.length,this.setColumnNames(r)}if(this._columnsCount+this._rowsCount>0&&(0==this._columnsCount||0==this._rowsCount))throw new Error("constructor columns cannot be empty if rows are positive and vice versa");this._columnNames=new HashMapObject_1.HashMapObject,this._cells=new HashMapObject_1.HashMapObject}return t.prototype.setColumnName=function(t,r){var e=this._validateColumnIndex(t);if(!StringUtils_1.StringUtils.isString(r))throw new Error("name must be a string");return this._columnNames.set(String(e),r),!0},t.prototype.setColumnNames=function(t){if(this._columnsCount==t.length){if(ArrayUtils_1.ArrayUtils.hasDuplicateElements(t))throw new Error("array must not contain duplicate elements");var r=t.length;this._columnNames=new HashMapObject_1.HashMapObject;for(var e=0;e<r;e++){if(!StringUtils_1.StringUtils.isString(t[e]))throw new Error("List of names must be an array of strings");this._columnNames.set(String(e),t[e])}return t}throw new Error("List of names must match number of columns")},t.prototype.getColumnNames=function(){for(var t=[],r=0;r<this._columnsCount;r++){var e=String(r);t.push(this._columnNames.isKey(e)?this._columnNames.get(e):"")}return t},t.prototype.getColumnName=function(t){var r=String(this._validateColumnIndex(t));return this._columnNames.isKey(r)?this._columnNames.get(r):""},t.prototype.getColumnIndex=function(t){if(!StringUtils_1.StringUtils.isString(t)||""===t)throw new Error("value must be a non empty string");for(var r=0,e=this._columnNames.getKeys();r<e.length;r++){var n=e[r];if(this._columnNames.get(n)===t)return Number(n)}throw new Error("provided column name does not exist")},t.prototype.getColumn=function(t){for(var r=[],e=this._validateColumnIndex(t),n=0;n<this._rowsCount;n++)r.push(this.getCell(n,e));return r},t.prototype.addColumns=function(t,r,e){if(void 0===r&&(r=[]),void 0===e&&(e=-1),!ArrayUtils_1.ArrayUtils.isArray(r))throw new Error("names must be an array");if(!NumericUtils_1.NumericUtils.isInteger(t)||t<=0)throw new Error("number must be a positive integer");if(!NumericUtils_1.NumericUtils.isInteger(e)||e<-1||e>=this._columnsCount)throw new Error("at must be a valid column index");if(e>=0)for(var n=this._columnsCount-1;n>=e;n--){this._columnNames.isKey(String(n))&&this._columnNames.rename(String(n),String(n+t));for(var s=0;s<this._rowsCount;s++){var i=String(s)+"-"+String(n);this._cells.isKey(i)&&this._cells.rename(i,String(s)+"-"+String(n+t))}}var o=r.length;if(o>0){if(o!=t)throw new Error("names length must be the same as number");var u=e<0?this._columnsCount:e;for(n=0;n<o;n++)this._columnNames.set(String(u+n),r[n])}return this._columnsCount+=t,!0},t.prototype.setColumn=function(t,r){var e=r.length;if(e<=0)throw new Error("data must not be empty");if(this._rowsCount!=e)throw new Error("data length and number of rows must match");for(var n=this._validateColumnIndex(t),s=0;s<this._rowsCount;s++)this.setCell(s,n,r[s])},t.prototype.removeColumn=function(t){var r=this._validateColumnIndex(t);this._columnNames.isKey(String(r))&&this._columnNames.remove(String(r));for(var e=0;e<this._rowsCount;e++){var n=String(e)+"-"+String(r);this._cells.isKey(n)&&this._cells.remove(n)}for(e=r+1;e<this._columnsCount;e++){this._columnNames.isKey(String(e))&&this._columnNames.rename(String(e),String(e-1));for(var s=0;s<this._rowsCount;s++){n=String(s)+"-"+String(e);this._cells.isKey(n)&&this._cells.rename(n,String(s)+"-"+String(e-1))}}this._columnsCount--,this._columnsCount<=0&&(this._rowsCount=0)},t.prototype.getCell=function(t,r){var e=this._validateRowIndex(t)+"-"+this._validateColumnIndex(r);return this._cells.isKey(e)?this._cells.get(e):null},t.prototype.setCell=function(t,r,e){var n=this._validateRowIndex(t),s=this._validateColumnIndex(r);return this._cells.set(n+"-"+s,e)},t.prototype.getRow=function(t){for(var r=[],e=this._validateRowIndex(t),n=0;n<this._columnsCount;n++)r.push(this.getCell(e,n));return r},t.prototype.addRows=function(t,r){if(void 0===r&&(r=-1),!NumericUtils_1.NumericUtils.isInteger(t)||t<=0)throw new Error("number must be a positive integer");if(!NumericUtils_1.NumericUtils.isInteger(r)||r<-1||r>=this._rowsCount)throw new Error("at must be a valid row index");if(r>=0)for(var e=this._rowsCount-1;e>=r;e--)for(var n=0;n<this._columnsCount;n++){var s=String(e)+"-"+String(n);this._cells.isKey(s)&&this._cells.rename(s,e+t+"-"+String(n))}return this._rowsCount+=t,!0},t.prototype.setRow=function(t,r){var e=r.length;if(e<=0)throw new Error("data must not be empty");if(this._columnsCount!=e)throw new Error("data length and number of columns must match");for(var n=this._validateRowIndex(t),s=0;s<this._columnsCount;s++)this.setCell(n,s,r[s])},t.prototype.removeRow=function(t){for(var r=this._validateRowIndex(t),e=0;e<this._columnsCount;e++){var n=String(r)+"-"+String(e);this._cells.isKey(n)&&this._cells.remove(n)}for(e=r+1;e<this._rowsCount;e++)for(var s=0;s<this._columnsCount;s++){n=String(e)+"-"+String(s);this._cells.isKey(n)&&this._cells.rename(n,String(e-1)+"-"+String(s))}this._rowsCount--,this._rowsCount<=0&&(this._columnsCount=0)},t.prototype.countRows=function(){return this._rowsCount},t.prototype.countColumns=function(){return this._columnsCount},t.prototype.countCells=function(){return this._rowsCount*this._columnsCount},t.prototype._validateColumnIndex=function(t){var r=NumericUtils_1.NumericUtils.isInteger(t)?Number(t):-1,e=this._columnNames.getValues(),n=this._columnNames.getKeys(),s=e.length;if(StringUtils_1.StringUtils.isString(t))for(var i=0;i<s;i++)if(t===e[i]){r=Number(n[i]);break}if(r<0||r>=this._columnsCount)throw new Error("Invalid column value");return r},t.prototype._validateRowIndex=function(t){var r=NumericUtils_1.NumericUtils.isInteger(t)?t:-1;if(r<0||r>=this._rowsCount)throw new Error("Invalid row value");return r},t}();exports.TableObject=TableObject;