@genexus/web-standard-functions
Version:
GeneXus JavaScript standard functions library for web generators
94 lines • 2.71 kB
JavaScript
import { GxCollectionData } from "./gxcollection";
import { GxDate } from "./gxdate";
import { GxDatetime } from "./gxdatetime";
import { classToObject } from "./type-conversions/classToObject";
import { objectToClass } from "./type-conversions/objectToClass";
export class GxDictionaryData {
constructor() {
this.dictionary = {};
}
toKey(k) {
let key = "";
if (k instanceof GxDate) {
key = k.serialize();
}
else if (k instanceof 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 GxCollectionData(...ks);
}
get Values() {
const vs = Object.values(this.dictionary);
return new 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] = 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), objectToClass(obj[k], this.__valueType));
}
}
return dictionary;
}
}
//# sourceMappingURL=gxdictionary.js.map