@allgemein/schema-api
Version:
Library for schema api
108 lines • 3.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LookupRegistry = void 0;
const lodash_1 = require("lodash");
const Constants_1 = require("./Constants");
class LookupRegistry {
constructor(namespace) {
this.namespace = Constants_1.DEFAULT_NAMESPACE;
this._entries = {};
this.namespace = namespace;
}
getNamespace() {
return this.namespace;
}
static reset(namespace = Constants_1.DEFAULT_NAMESPACE) {
if (this.$self[namespace]) {
delete this.$self[namespace];
}
}
static $(namespace = Constants_1.DEFAULT_NAMESPACE) {
if (!this.$self[namespace]) {
this.$self[namespace] = new LookupRegistry(namespace);
}
return this.$self[namespace];
}
list(context) {
if (!(0, lodash_1.has)(this._entries, context)) {
this._entries[context] = [];
}
return this._entries[context];
}
add(context, entry) {
if (!(0, lodash_1.has)(this._entries, context)) {
this._entries[context] = [];
}
this._entries[context].push(entry);
return entry;
}
remove(context, search) {
if (!(0, lodash_1.has)(this._entries, context)) {
this._entries[context] = [];
}
return (0, lodash_1.remove)(this._entries[context], search);
}
filter(context, search) {
if (!(0, lodash_1.has)(this._entries, context)) {
this._entries[context] = [];
}
if (search) {
return this._entries[context].filter(search);
}
return this._entries[context];
}
find(context, search) {
if (!(0, lodash_1.has)(this._entries, context)) {
this._entries[context] = [];
}
return this._entries[context].find(search);
}
/**
* return lookup registry namespaces
*/
static getRegistryNamespaces() {
return Object.keys(this.$self);
}
/**
* return lookup registries
*/
static getLookupRegistries() {
return Object.keys(this.$self).map(x => this.$self[x]);
}
/**
* search in all registries
*
* @param context
* @param search
*/
static find(context, search) {
const registryNames = this.getRegistryNamespaces();
for (const registryName of registryNames) {
const found = this.$self[registryName].find(context, search);
if (found) {
return found;
}
}
return null;
}
/**
* filter over all registries
*
* @param context
* @param search
*/
static filter(context, search) {
const results = [];
const namespaces = Object.keys(this.$self);
for (const namespace of namespaces) {
const found = this.$self[namespace].filter(context, search);
if (!(0, lodash_1.isEmpty)(found)) {
results.push(...found);
}
}
return results;
}
}
exports.LookupRegistry = LookupRegistry;
LookupRegistry.$self = {};
//# sourceMappingURL=LookupRegistry.js.map