turbocommons-ts
Version:
General purpose library that implements frequently used and generic software development tasks
1 lines • 3.73 kB
JavaScript
"use strict";var __extends=this&&this.__extends||function(){var t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)r.hasOwnProperty(e)&&(t[e]=r[e])})(r,e)};return function(r,e){function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}();Object.defineProperty(exports,"__esModule",{value:!0}),exports.CSVObject=void 0;var TableObject_1=require("./TableObject"),StringUtils_1=require("../utils/StringUtils"),ArrayUtils_1=require("../utils/ArrayUtils"),CSVObject=function(t){function r(r,e,n,i){void 0===r&&(r=""),void 0===e&&(e=!1),void 0===n&&(n=","),void 0===i&&(i='"');var s=t.call(this)||this;if(s._hasHeaders=!1,!StringUtils_1.StringUtils.isString(r))throw new Error("constructor expects a string value");if(StringUtils_1.StringUtils.isEmpty(r))return s;for(var o=0,l=0,u=!1,a="",c=r.length,h=0;h<c;h++){var f=r.charAt(h);f!==n||u?f!==i?"\r"!==f&&"\n"!==f?a+=f:(u?a+=f:l>0&&(s._insertField(o,l,a),o++,a="",l=0),"\r"===f&&"\n"===r.substr(h+1,1)&&(u&&(a+="\n"),h++)):u&&r.substr(h+1,1)===i?(a+=f,h++):(u=!u)?a="":h=s._findNextDelimiterIndex(r,h,n,c)-1:(s._insertField(o,l,a),a="",l++)}return(""!=a||l>=s._columnsCount)&&s._insertField(o,l,a),e&&s._defineHeaders(),s}return __extends(r,t),r.prototype.getCell=function(r,e){var n=t.prototype.getCell.call(this,r,e);return null===n?"":n},r.prototype.setCell=function(r,e,n){if(!StringUtils_1.StringUtils.isString(n))throw new Error("value must be a string");return t.prototype.setCell.call(this,r,e,n)},r.isCSV=function(t){try{return new r(t).countCells()>=0}catch(e){try{return null!==t&&t instanceof r}catch(t){return!1}}},r.prototype.isEqualTo=function(t){var e=null;try{e=new r(t,this._hasHeaders)}catch(n){try{t instanceof r&&(e=t)}catch(t){}}if(null==e)throw new Error("csv does not contain valid csv data");var n=this.countRows(),i=this.countColumns();if(0===this.countCells()&&0===e.countCells())return!0;if(this._hasHeaders&&!ArrayUtils_1.ArrayUtils.isEqualTo(this.getColumnNames(),e.getColumnNames()))return!1;if(n!==e.countRows()||i!==e.countColumns())return!1;for(var s=0;s<n;s++)for(var o=0;o<i;o++){var l=this.getCell(s,o);null===l&&(l="");var u=e.getCell(s,o);if(null===u&&(u=""),l!==u)return!1}return!0},r.prototype.toString=function(t,r){void 0===t&&(t=","),void 0===r&&(r='"');var e="";if(this._hasHeaders){for(var n=[],i=0,s=this.getColumnNames();i<s.length;i++){var o=s[i];n.push(this._escapeField(o,t,r))}e+=n.join(t)+"\r\n"}for(var l=this.countRows(),u=this.countColumns(),a=0;a<l;a++){n=[];for(var c=0;c<u;c++){var h="";try{h=this._escapeField(this._cells.get(a+"-"+c),t,r)}catch(t){}n.push(h)}e+=n.join(t)+"\r\n"}return l>0?e.substring(0,e.length-2):e},r.prototype._insertField=function(t,r,e){t>=this._rowsCount&&this._rowsCount++,r>=this._columnsCount&&this._columnsCount++,this._cells.set(t+"-"+r,e)},r.prototype._escapeField=function(t,r,e){return(t.indexOf("\r")>=0||t.indexOf("\n")>=0||t.indexOf(e)>=0||t.indexOf(r)>=0)&&(t=e+(t=StringUtils_1.StringUtils.replace(t,[e],[e+e]))+e),t},r.prototype._findNextDelimiterIndex=function(t,r,e,n){for(var i=r+1;i<n;i++){var s=t.charAt(i);if(s===e||"\r"===s||"\n"===s)return i}return n},r.prototype._defineHeaders=function(){var t=this.getRow(0);if(ArrayUtils_1.ArrayUtils.hasDuplicateElements(t)){for(var r=0,e=[],n=ArrayUtils_1.ArrayUtils.getDuplicateElements(t),i=0,s=t;i<s.length;i++){var o=s[i];if(null===o||""===o)o="("+ ++r+")";else for(var l=0,u=n;l<u.length;l++){if(o===u[l]){o=o+"("+ ++r+")";break}}e.push(o)}this.setColumnNames(e)}else this.setColumnNames(t);this.removeRow(0),this._hasHeaders=!0},r}(TableObject_1.TableObject);exports.CSVObject=CSVObject;