@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
116 lines • 3.93 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
/**
* Model to represent an Entity in Ditto.
*/
export class EntityModel {
/**
* Turns an object into an array of the type A. It contains one Entity for every key of the original object
*
* @typeParam A - The type of array to return
* @param o - The object to parse.
* @param parser - A method to parse instances of the desired Entity.
* @returns The array of Entities.
*/
static objectToArray(o, parser) {
return Object.keys(o).map((key) => {
// @ts-expect-error legacy ignored
return parser(o[key], key);
});
}
/**
* Builds an object representation out of the provided content.
*
* @param content - The content of the object representation.
* @returns The object containing all elements of the content that aren't undefined.
*/
static buildObject(content) {
const res = {};
content.forEach((v, k) => {
if (v !== undefined && v !== '') {
res[k] = v;
}
});
return res;
}
toJson() {
return JSON.stringify(this.toObject());
}
equals(toCompare) {
return toCompare !== undefined && (this === toCompare || this.toJson() === toCompare.toJson());
}
}
/**
* Entity with an 'id' field
*/
export class EntityWithId extends EntityModel {
equals(toCompare) {
return super.equals(toCompare) && this.id === toCompare.id;
}
}
/**
* Abstract entity model class that consists of entities of the same type but with unknown property keys.
*/
export class IndexedEntityModel {
static toJson(entity) {
return JSON.stringify(this.toObject(entity));
}
static equals(a, b) {
return a !== undefined && b !== undefined && (a === b || this.toJson(a) === this.toJson(b));
}
/**
* Map the object to an indexed object with types. Iterates all keys and maps all values with {@code mapValue}.
*
* @param objectToMap - the object to map.
* @param mapValue - how to get the value from an objects value.
* @param mapKey - how to get the key from an objects key.
*/
static fromPlainObject(objectToMap, mapValue, mapKey = key => key) {
if (objectToMap) {
const entries = Object.keys(objectToMap)
.map(k => {
// @ts-expect-error legacy ignored
const theKey = mapKey(k, objectToMap[k]);
// @ts-expect-error legacy ignored
const theVal = mapValue(objectToMap[k], k);
return { [theKey]: theVal };
});
return Object.assign({}, ...entries);
}
return {};
}
/**
* Removes all type information from the indexed type.
* @param objectToMap - the object to map.
* @param removeType - function that is called to remove the type of the objects values.
*/
static toPlainObject(objectToMap, removeType) {
const entries = Object.keys(objectToMap)
.map(k => {
return { [k]: removeType(objectToMap[k]) };
});
return Object.assign({}, ...entries);
}
/**
* Turns the Entity into an object.
*
* @returns The object representation of the Entity.
*/
static toObject(entityModel) {
if (entityModel != null) {
return IndexedEntityModel.toPlainObject(entityModel, element => element.toObject());
}
return undefined;
}
}
//# sourceMappingURL=model.js.map