@toolbox-ts/dsa
Version:
Data Structures and Algorithms implemented
35 lines • 1.36 kB
JavaScript
import { Anchors } from "./anchors/index.js";
import { Base } from "./base/index.js";
import { Pointers } from "./pointers/index.js";
/**
* Creates a node structure module with anchor and pointer management.
* Returns an object with anchor manager, pointer key info,
* type, and node factory.
*
* @template TK - Node type key
* @template AK - Anchor key string type
* @template PK - Pointer key string type
* @template D - Data type stored in the node
* @param config - Configuration object for anchors, pointers, and type
* @returns A module for managing nodes of the specified type
*/
export const create = ({ primaryAnchorKey, anchorKeys, pointerKeys, type, }) => {
// Create anchor manager with provided keys and optional primary
const anchors = Anchors.create({
anchorKeys,
primary: primaryAnchorKey,
});
// Copy pointer keys as a tuple
const pks = [...pointerKeys];
// Node factory: creates a base node and augments it with pointer properties
const createNode = ({ data, id }) => Pointers.assign({
base: Base.create({ data, id, type }),
pointerKeys: pks,
});
const setPointer = (node, map) => {
for (const key in map)
node[key] = map[key];
};
return { type, anchors, pointerKeys: pks, createNode, setPointer };
};
//# sourceMappingURL=Manager.js.map