mcp-jarvis-config
Version:
MCP JARVIS configuration tool for Claude Desktop
97 lines (93 loc) • 3.2 kB
JavaScript
/**
* Placeholder Processing Module
* This module adds dynamic placeholder detection and replacement to MCP JARVIS
*/
/**
* Finds all placeholder values in a configuration object
* @param {Object} obj - Configuration object to search
* @param {Set} placeholders - Optional Set to accumulate placeholders
* @returns {Set} - Set of placeholders found in the format {{PLACEHOLDER}}
*/
function findPlaceholders(obj, placeholders = new Set()) {
if (Array.isArray(obj)) {
for (const item of obj) {
if (typeof item === 'object' && item !== null) {
findPlaceholders(item, placeholders);
} else if (typeof item === 'string') {
const matches = item.match(/\{\{([A-Z0-9_]+)\}\}/g);
if (matches) {
matches.forEach(match => placeholders.add(match));
}
}
}
} else if (typeof obj === 'object' && obj !== null) {
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
findPlaceholders(obj[key], placeholders);
} else if (typeof obj[key] === 'string') {
const matches = obj[key].match(/\{\{([A-Z0-9_]+)\}\}/g);
if (matches) {
matches.forEach(match => placeholders.add(match));
}
}
}
}
return placeholders;
}
/**
* Replaces placeholders in a configuration object with provided values
* @param {Object} obj - Configuration object with placeholders
* @param {Object} replacements - Object mapping placeholders to their values
* @returns {Object} - Configuration with placeholders replaced
*/
function replacePlaceholders(obj, replacements) {
if (Array.isArray(obj)) {
return obj.map(item => {
if (typeof item === 'object' && item !== null) {
return replacePlaceholders(item, replacements);
} else if (typeof item === 'string') {
let result = item;
for (const [placeholder, value] of Object.entries(replacements)) {
result = result.replace(placeholder, value);
}
return result;
}
return item;
});
} else if (typeof obj === 'object' && obj !== null) {
const result = {};
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
result[key] = replacePlaceholders(obj[key], replacements);
} else if (typeof obj[key] === 'string') {
let value = obj[key];
for (const [placeholder, replacement] of Object.entries(replacements)) {
value = value.replace(placeholder, replacement);
}
result[key] = value;
} else {
result[key] = obj[key];
}
}
return result;
}
return obj;
}
/**
* Gets a friendly display name from a placeholder
* @param {string} placeholder - The placeholder to format (e.g. {{API_KEY}})
* @returns {string} - User-friendly display name (e.g. "API Key")
*/
function getDisplayName(placeholder) {
// Remove {{ and }} and convert to title case with spaces
return placeholder
.replace(/^\{\{|\}\}$/g, '') // Remove {{ and }}
.split('_')
.map(word => word.charAt(0) + word.slice(1).toLowerCase())
.join(' ');
}
module.exports = {
findPlaceholders,
replacePlaceholders,
getDisplayName
};