trclib
Version:
Client and utility wrappers for TRC rest APIs
144 lines (143 loc) • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var SheetContentsIndex = (function () {
function SheetContentsIndex(source) {
this._map = {};
this._source = source;
var cRecId = source["RecId"];
for (var i = 0; i < cRecId.length; i++) {
var recId = cRecId[i];
this._map[recId] = i;
}
}
SheetContentsIndex.prototype.lookupRecId = function (recId) {
var idx = this._map[recId];
if (idx == undefined) {
return -1;
}
return idx;
};
SheetContentsIndex.prototype.set = function (recId, columnName, newValue) {
var idx = this.lookupRecId(recId);
if (idx != -1) {
this._source[columnName][idx] = newValue;
}
};
SheetContentsIndex.prototype.getContents = function () {
return this._source;
};
return SheetContentsIndex;
}());
exports.SheetContentsIndex = SheetContentsIndex;
var SheetContents = (function () {
function SheetContents() {
}
SheetContents.getSheetContentsIndex = function (source) {
return new SheetContentsIndex(source);
};
SheetContents.toCsv = function (data) {
var colKeys = Object.keys(data);
var grid = [];
var rowCount = data[colKeys[0]].length;
var index = 0;
grid.push(colKeys);
while (index < rowCount) {
var row = [];
for (var _i = 0, colKeys_1 = colKeys; _i < colKeys_1.length; _i++) {
var colKey = colKeys_1[_i];
var direct = data[colKey][index];
var val;
if (direct == null || direct == undefined) {
val = "";
}
else {
val = direct.toString();
try {
val = val.replace("\"", "'");
if (val.indexOf(",") >= 0) {
val = "\"" + val + "\"";
}
}
catch (e) {
val = "???";
}
}
row.push(val);
}
grid.push(row);
index++;
}
var content = "";
grid.forEach(function (arr, index) {
var row = arr.join(",");
content += index < grid.length ? row + "\r\n" : row;
});
return content;
};
SheetContents.ForEach = function (source, callback) {
var colRecId = source["RecId"];
for (var columnName in source) {
var column = source[columnName];
if (column == colRecId) {
continue;
}
for (var i = 0; i < column.length; i++) {
var recId = colRecId[i];
var newValue = column[i];
callback(recId, columnName, newValue);
}
}
};
SheetContents.FromSingleCell = function (recId, columnName, newValue) {
var body = {};
body["RecId"] = [recId];
body[columnName] = [newValue];
return body;
};
SheetContents.FromRow = function (recId, columnNames, newValues) {
if (columnNames.length != newValues.length) {
throw "length mismatch";
}
var body = {};
body["RecId"] = [recId];
for (var i = 0; i < columnNames.length; i++) {
var columnName = columnNames[i];
var newValue = newValues[i];
body[columnName] = [newValue];
}
return body;
};
SheetContents.KeepRows = function (source, fpInclude) {
var columnNames = [];
var results = {};
var len = -1;
for (var columnName in source) {
if (len == -1) {
len = source[columnName].length;
}
columnNames.push(columnName);
results[columnName] = [];
}
for (var iRow = 0; iRow < len; iRow++) {
var keepRow = fpInclude(iRow);
if (keepRow) {
for (var x in columnNames) {
var columnName = columnNames[x];
var val = source[columnName][iRow];
results[columnName].push(val);
}
}
}
return results;
};
SheetContents.TakeN = function (sheet, topN) {
var sheet2 = {};
for (var key in sheet) {
var value = sheet[key];
sheet2[key] = value.slice(0, topN);
}
return sheet2;
};
return SheetContents;
}());
exports.SheetContents = SheetContents;