arvo-event-handler
Version:
Type-safe event handler system with versioning, telemetry, and contract validation for distributed Arvo event-driven architectures, featuring routing and multi-handler support.
64 lines (63 loc) • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathValueToString = void 0;
exports.getAllPaths = getAllPaths;
/**
* Recursively retrieves all paths and their corresponding values from an object.
*
* @param {Record<string, any>} obj - The object from which paths are extracted.
* @returns {PathValue[]} An array of PathValue, each containing a path and its corresponding value.
* @throws {Error} Will throw an error if `obj` is not a string or an object.
*
* @example
* const obj = { a: { b: 1 }, c: 2 };
* const paths = getAllPaths(obj);
* // paths = [
* // { path: ['a', 'b'], value: 1 },
* // { path: ['c'], value: 2 }
* // ]
*/
function getAllPaths(obj) {
if (!obj || !(typeof obj === 'string' || typeof obj === 'object')) {
throw new Error("[getAllPaths] the 'obj' type must be 'string' or 'object'. The given obj is '".concat(obj, "' of type '").concat(typeof obj, "'"));
}
var result = [];
var stack = [{ obj: obj, path: [] }];
while (stack.length > 0) {
var stack_item = stack.pop();
if (!stack_item)
continue;
var currentObj = stack_item.obj, currentPath = stack_item.path;
if (typeof currentObj !== 'object' || currentObj === null) {
result.push({ path: currentPath, value: currentObj });
}
else {
for (var key in currentObj) {
// biome-ignore lint/suspicious/noPrototypeBuiltins: no reason to entertain this lint issue
if (currentObj.hasOwnProperty(key)) {
stack.push({ obj: currentObj[key], path: currentPath.concat(key) });
}
}
}
}
return result;
}
/**
* Converts a PathValue object to a string representation.
*
* @param {PathValue} item - The PathValue object to convert.
* @returns {string} A string representation of the PathValue.
*
* @example
* const pathValue = { path: ['a', 'b'], value: 1 };
* const result = pathValueToString(pathValue);
* // result = "#a.#b.1"
*/
var pathValueToString = function (item) {
if (!(item.path || []).length) {
return item.value.toString();
}
var pathString = item.path.map(function (i) { return "#".concat(i); }).join('.');
return "".concat(pathString, ".").concat(item.value);
};
exports.pathValueToString = pathValueToString;