Creates a wrapper for working with 'pathed' trees. An example is a filesystem.

const t = create();
// Store a value. Path implies a structure of
// c -> users -> admin
// ...which is autoatically created
t.add({x:10}, `c.users.admin`);

t.add({x:20}, `c.users.guest`);
// Tree will now be:
// c-> users -> admin
// -> guest

t.getValue(`c.users.guest`); // { x:20 }

By default only a single value can be stored at a path. Set options to allow this:

const t = create({ duplicates: `allow` });
t.add({x:10}, `c.users.admin`);
t.add({x:20}, `c.users.admin`);
t.getValue(`c.users.admin`); // Throws an error because there are multiple values
t.getValues(`c.users.admin`); // [ {x:10}, {x:20 } ]
  • Type Parameters

    • T

    Parameters

    • pathOpts: Partial<Readonly<{
          duplicates: "ignore" | "allow" | "overwrite";
          separator: string;
      }>> = {}

    Returns {
        add: ((value: T, path: string) => void);
        childrenLength: ((path: string) => number);
        clearValues: ((path: string) => boolean);
        getNode: ((path: string) => undefined | LabelledNode<T>);
        getValue: ((path: string) => undefined | T);
        getValues: ((path: string) => T[]);
        hasPath: ((path: string) => boolean);
        prettyPrint: (() => string);
        remove: ((path: string) => boolean);
    }

    • add: ((value: T, path: string) => void)
        • (value, path): void
        • Parameters

          • value: T
          • path: string

          Returns void

    • childrenLength: ((path: string) => number)
        • (path): number
        • Parameters

          • path: string

          Returns number

    • clearValues: ((path: string) => boolean)
        • (path): boolean
        • Parameters

          • path: string

          Returns boolean

    • getNode: ((path: string) => undefined | LabelledNode<T>)
    • getValue: ((path: string) => undefined | T)
        • (path): undefined | T
        • Parameters

          • path: string

          Returns undefined | T

    • getValues: ((path: string) => T[])
        • (path): T[]
        • Parameters

          • path: string

          Returns T[]

    • hasPath: ((path: string) => boolean)
        • (path): boolean
        • Parameters

          • path: string

          Returns boolean

    • prettyPrint: (() => string)
        • (): string
        • Returns string

    • remove: ((path: string) => boolean)
        • (path): boolean
        • Parameters

          • path: string

          Returns boolean