@genexus/web-standard-functions
Version:
GeneXus JavaScript standard functions library for web generators
98 lines • 2.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GxDictionaryData = void 0;
const gxcollection_1 = require("./gxcollection");
const gxdate_1 = require("./gxdate");
const gxdatetime_1 = require("./gxdatetime");
const classToObject_1 = require("./type-conversions/classToObject");
const objectToClass_1 = require("./type-conversions/objectToClass");
class GxDictionaryData {
constructor() {
this.dictionary = {};
}
toKey(k) {
let key = "";
if (k instanceof gxdate_1.GxDate) {
key = k.serialize();
}
else if (k instanceof gxdatetime_1.GxDatetime) {
key = k.serialize();
}
else {
key = k.toString();
}
return key;
}
fromKey(k) {
return k;
}
get Keys() {
const ks = Object.keys(this.dictionary).map(x => this.fromKey(x));
return new gxcollection_1.GxCollectionData(...ks);
}
get Values() {
const vs = Object.values(this.dictionary);
return new gxcollection_1.GxCollectionData(...vs);
}
get Count() {
return Object.values(this.dictionary).length;
}
setType(keyType, valueType, serializationType) {
this.__keyType = keyType;
this.__valueType = valueType;
this.__serializationType = serializationType !== null && serializationType !== void 0 ? serializationType : valueType;
return this;
}
set(key, value) {
return (this.dictionary[this.toKey(key)] = value);
}
setDictionary(d) {
Object.assign(this.dictionary, d.dictionary);
}
remove(key) {
delete this.dictionary[this.toKey(key)];
}
removeKeys(keys) {
for (const k of keys) {
delete this.dictionary[this.toKey(k)];
}
}
removeAll(d) {
for (const k of d.Keys) {
delete this.dictionary[this.toKey(k)];
}
}
clear() {
this.dictionary = {};
}
get(key) {
return this.dictionary[this.toKey(key)];
}
contains(key) {
return this.dictionary[this.toKey(key)] !== undefined;
}
toJson() {
return JSON.stringify(this.serialize());
}
fromJson(json) {
this.dictionary = JSON.parse(json);
}
serialize() {
const obj = {};
for (const k in this.dictionary) {
obj[k] = (0, classToObject_1.classToObject)(this.dictionary[k], this.__valueType);
}
return obj;
}
deserialize(obj) {
const dictionary = new GxDictionaryData().setType(this.__keyType, this.__valueType, this.__serializationType);
if (obj) {
for (const k in obj) {
dictionary.set(this.fromKey(k), (0, objectToClass_1.objectToClass)(obj[k], this.__valueType));
}
}
return dictionary;
}
}
exports.GxDictionaryData = GxDictionaryData;
//# sourceMappingURL=gxdictionary.js.map