@bizone-ai/json-transform-utils
Version:
Utilities for handling JSON transformers
69 lines • 2.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParseContext = void 0;
const ContextVariablesSchemas_1 = require("./functions/ContextVariablesSchemas");
class ParseContext {
constructor(paths, additionalContext, previousPaths) {
this.paths = paths;
this.additionalContext = additionalContext;
this.knownVariables = new Set();
if (paths) {
for (const path in paths) {
const v = path.split(/[.[]/, 1)[0];
this.knownVariables.add(v);
}
}
if (additionalContext) {
for (const path in additionalContext) {
const v = path.split(/[.[]/, 1)[0];
this.knownVariables.add(v);
}
}
if (previousPaths) {
for (const path of previousPaths) {
const v = path.split(/[.[]/, 1)[0];
this.knownVariables.add(v);
}
}
}
hasPaths() {
return Boolean(this.paths);
}
hasPath(path) {
return typeof this.paths?.[path] !== "undefined";
}
/**
* If you are about to change the result, use this and not 'resolve()'
* @param path
*/
getPath(path) {
return this.paths?.[path];
}
setPath(path, type) {
if (!this.paths)
return;
this.paths[path] = type;
const v = path.split(/[.[]/, 1)[0];
this.knownVariables.add(v);
}
removePaths(paths, variableToRemove) {
for (const path of paths) {
delete this.paths?.[path];
}
if (variableToRemove) {
this.knownVariables.delete(variableToRemove);
}
}
resolve(key) {
return ContextVariablesSchemas_1.ContextVariablesSchemas[key] ?? this.additionalContext?.[key] ?? this.paths?.[key];
}
isReferencingKnownVariable(path) {
if (typeof path !== "string" || path.startsWith("$$"))
return false;
const v = path.split(/[.[]/, 1)[0];
return this.knownVariables.has(v) && (path === v || path.startsWith(v + ".") || path.startsWith(v + "["));
}
}
exports.ParseContext = ParseContext;
exports.default = ParseContext;
//# sourceMappingURL=ParseContext.js.map
;