@genexus/web-standard-functions
Version:
GeneXus JavaScript standard functions library for web generators
122 lines • 3.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GxCollectionData = exports.gxRowNumberId = void 0;
const classToObject_1 = require("./type-conversions/classToObject");
const objectToClass_1 = require("./type-conversions/objectToClass");
exports.gxRowNumberId = "_gxIndex";
class GxCollectionData extends Array {
setType(itemType, serializationType) {
this.__itemType = itemType;
this.__serializationType = serializationType !== null && serializationType !== void 0 ? serializationType : itemType;
return this;
}
get CurrentItem() {
if (!this.__currentItem && this.__itemType) {
this.__currentItem = new this.__itemType();
}
return this.__currentItem;
}
set CurrentItem(value) {
this.__currentItem = value;
}
get Count() {
return this.length;
}
get itemClass() {
return this.__itemType;
}
add(element, position) {
if (position) {
this.splice(position - 1, 0, element);
}
else {
this.push(element);
}
}
addRange(collection, position) {
if (position === undefined) {
this.splice(this.length, 0, ...collection);
return true;
}
else if (position === 0) {
this.splice(position, 0, ...collection);
return true;
}
else if (position > 0 && position <= collection.length + 1) {
this.splice(position - 1, 0, ...collection);
return true;
}
return false;
}
removeRange(position, count) {
if (position > 0 && position <= this.length) {
if (count === undefined) {
this.splice(-position - 1);
return true;
}
else if (count < this.length - position) {
this.splice(position - 1, count);
return true;
}
}
return false;
}
set(position, element) {
if (position > 0 && position < this.length) {
this[position - 1] = element;
return true;
}
return false;
}
clear() {
this.splice(0, this.length);
}
clone() {
return this.slice(0);
}
indexOf(element) {
return super.indexOf(element) + 1;
}
item(ix) {
return this[ix - 1];
}
remove(ix) {
this.splice(ix - 1, 1);
}
toJson() {
return JSON.stringify(this.serialize());
}
fromJson(json) {
const deserialized = this.deserialize(JSON.parse(json));
for (const item of deserialized) {
this.add(item);
}
}
static fromArray(array) {
return GxCollectionData.from(array);
}
serialize() {
const array = new Array();
this.forEach(element => {
const item = (0, classToObject_1.classToObject)(element, this.__itemType);
array.push(item);
});
return array;
}
deserialize(items) {
const collection = new GxCollectionData().setType(this.__itemType, this.__serializationType);
if (items) {
let idx = 0;
items.forEach(element => {
const item = (0, objectToClass_1.objectToClass)(element, this.__itemType);
if (typeof item === "object") {
item[exports.gxRowNumberId] = idx++;
}
collection.push(item);
});
}
return collection;
}
}
exports.GxCollectionData = GxCollectionData;
//# sourceMappingURL=gxcollection.js.map