@bizone-ai/json-transform-utils
Version:
Utilities for handling JSON transformers
89 lines • 4.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformUtils = void 0;
const ContextVariablesSchemas_1 = require("./functions/ContextVariablesSchemas");
class TransformUtils {
constructor(additionalContext, specialKeys, contextVariablesSchemas, scopedContextVariablesSchema) {
this.additionalContext = additionalContext || new Set();
this.variableDetectionRegExp = this.variableDetectionRegExpFactory();
this.specialKeys = specialKeys || new Set();
this.contextVariablesSchemas = contextVariablesSchemas
? Object.assign({}, ContextVariablesSchemas_1.ContextVariablesSchemas, contextVariablesSchemas)
: ContextVariablesSchemas_1.ContextVariablesSchemas;
this.scopedContextVariablesSchema = scopedContextVariablesSchema || {};
this.allContextVariables = Object.keys(this.contextVariablesSchemas).concat(Object.keys(this.scopedContextVariablesSchema));
}
/**
* This affects detection and syntax highlighting of additional context variables (e.g. $varname)
* @param additionalContext
*/
setAdditionalContext(additionalContext) {
this.additionalContext = additionalContext;
this.variableDetectionRegExp = this.variableDetectionRegExpFactory();
}
getAdditionalContext() {
return this.additionalContext;
}
/**
* This mainly affects syntax highlighting. Making certain keys being highlighted differently.
*/
setSpecialKeys(specialKeys) {
this.specialKeys = specialKeys;
}
getSpecialKeys() {
return this.specialKeys;
}
/**
* This affects detection and syntax highlighting of additional context variables (e.g. #varname)
* In this case, a schema is also required
* @param contextVariablesSchemas
*/
setContextVariablesSchemas(contextVariablesSchemas) {
this.contextVariablesSchemas = Object.assign({}, ContextVariablesSchemas_1.ContextVariablesSchemas, contextVariablesSchemas);
this.allContextVariables = Object.keys(this.contextVariablesSchemas).concat(Object.keys(this.scopedContextVariablesSchema));
}
getContextVariablesSchemas() {
return this.contextVariablesSchemas;
}
setScopedContextVariablesSchema(scopedContextVariablesSchema) {
this.scopedContextVariablesSchema = scopedContextVariablesSchema;
this.allContextVariables = Object.keys(this.contextVariablesSchemas).concat(Object.keys(this.scopedContextVariablesSchema));
}
getScopedContextVariablesSchema() {
return this.scopedContextVariablesSchema;
}
matchesAnyOfContextVariables(variableName) {
for (const key of this.allContextVariables) {
if (variableName === key || variableName.startsWith(key + ".") || variableName.startsWith(key + "[")) {
return true;
}
}
return false;
}
matchesAnyOfAdditionalContext(variableName) {
for (const key of this.additionalContext) {
if (variableName === key || variableName.startsWith(key + ".") || variableName.startsWith(key + "[")) {
return true;
}
}
return false;
}
matchesAnyOfSpecialKeys(variableName) {
for (const key of this.specialKeys) {
if (variableName === key || variableName.startsWith(key + ".") || variableName.startsWith(key + "[")) {
return true;
}
}
return false;
}
variableDetectionRegExpFactory(flags = "g") {
const altNames = [];
for (const key of this.additionalContext) {
altNames.push(key.substring(1));
}
const altPrefixes = altNames.length ? `(${altNames.join("|")})?` : "";
return new RegExp(`(?<![\\])}?!@#$%^&*+\\\\\\w])(#[a-z_]+[a-z_\\d]*|\\$(?!\\$)${altPrefixes})(((\\.(?![-\\w$]+\\()[-\\w$]+)|(\\[[^\\]\\n]+]))+|(?=[^\\w.]|$))`, flags);
}
}
exports.transformUtils = new TransformUtils();
//# sourceMappingURL=transformUtils.js.map