Add function to write extension fields to character cards.

This commit is contained in:
Cohee
2024-03-12 01:49:05 +02:00
parent c9c6d798d9
commit 6b2374c405
5 changed files with 81 additions and 20 deletions

View File

@ -1226,3 +1226,27 @@ export async function extractTextFromMarkdown(blob) {
const text = postProcessText(document.body.textContent, false);
return text;
}
/**
* Sets a value in an object by a path.
* @param {object} obj Object to set value in
* @param {string} path Key path
* @param {any} value Value to set
* @returns {void}
*/
export function setValueByPath(obj, path, value) {
const keyParts = path.split('.');
let currentObject = obj;
for (let i = 0; i < keyParts.length - 1; i++) {
const part = keyParts[i];
if (!Object.hasOwn(currentObject, part)) {
currentObject[part] = {};
}
currentObject = currentObject[part];
}
currentObject[keyParts[keyParts.length - 1]] = value;
}