Staquia API
    Preparing search index...

    Class LexicalList<K, T>

    A list that keeps the order of your objects and their positions in sync, as a doubly linked list.

    Any object with an id (never null or undefined) and a position string can be stored. Every operation updates the position field of your objects and tells you which items changed: in the usual case only one, so only one row needs to be saved. Nothing is thrown: the operations return a Result, and when one fails neither the list nor your objects are changed.

    import Staquia, { LexicalList } from "staquia";

    interface Task {
    id: number;
    title: string;
    position: string;
    }

    const list = new LexicalList<number, Task>(Staquia.create("!", "a", "z", 5, 15, 0.15));

    const write: Task = { id: 1, title: "Write", position: "" };
    const review: Task = { id: 2, title: "Review", position: "" };
    const publish: Task = { id: 3, title: "Publish", position: "" };
    list.push(write); // "mmmmm!"
    list.push(review); // "mmmmn!"
    list.push(publish); // "mmmmo!"

    // Moves "Publish" between "Write" and "Review": only its position changes
    const moved = list.move(3, 1, 2);
    if (moved.ok) console.log(publish.position); // "mmmmmn", save only this task

    Type Parameters

    • K

      The type of the id of the items.

    • T extends Item<K>

      The type of the stored objects.

    Index

    Constructors

    Accessors

    Methods

    • Gets an item by its id. The item exposes its id, its position and the ids of its previous and next items.

      Parameters

      • id: K | null | undefined

        The id of the item.

      Returns LexicalItem<K, T> | undefined

      The item, or undefined if there is no item with that id.

    • Moves an item between two neighbors, for example when a drag and drop ends. Only the moved item gets a new position: the one between its new neighbors (or the one before the first item, or after the last one).

      The neighbors are verified in constant time: the move fails with a "not-adjacent" error when previousId is not immediately followed by nextId (ignoring the moved item itself), when only previousId is passed but it is not the last item, or when only nextId is passed but it is not the first item. Moving an item to the place it already occupies does nothing and keeps its position. null and undefined mean "no neighbor".

      When there is no room left between the neighbors (the threshold or the overflow), it fails with a "position" error: space the positions again with sort and repeat the move.

      Parameters

      • id: K | null | undefined

        The id of the item to move.

      • OptionalpreviousId: K | null

        The id of its new previous item, or undefined to move it to the start.

      • OptionalnextId: K | null

        The id of its new next item, or undefined to move it to the end.

      Returns Result<LexicalItem<K, T>, LexicalError>

      The moved item (the only one to save), or the error: "not-found", "not-adjacent" or "position".

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));
      const milk = { id: 1, position: "" };
      const meat = { id: 2, position: "" };
      const bread = { id: 3, position: "" };
      list.push(milk); // "mmmmm!"
      list.push(meat); // "mmmmn!"
      list.push(bread); // "mmmmo!"

      // Moves the bread between the milk and the meat
      const moved = list.move(3, 1, 2);
      if (moved.ok) console.log(bread.position); // "mmmmmn", the only position that changed

      // Moves the meat to the start
      list.move(2, undefined, 1);

      // The neighbors must be next to each other: the order is 2, 1, 3 and "3" is the last item
      const wrong = list.move(1, 3, 2);
      if (!wrong.ok) console.log(wrong.error.type); // "not-adjacent"
    • Adds an object at the end of the list, with the position after the last item (or the start position if the list is empty). Its position field is set, so the object itself can be saved.

      Parameters

      • value: T

        The object to add.

      Returns Result<LexicalItem<K, T>, LexicalError>

      The added item (the only one to save), or the error: "invalid-id", "duplicate-id", or "position" if there is no room left after the last item.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));

      const milk = { id: 1, text: "Milk", position: "" };
      const added = list.push(milk);
      if (added.ok) console.log(milk.position); // "mmmmm!"

      // Ids must be unique, and never null or undefined
      const duplicated = list.push({ id: 1, text: "Milk again", position: "" });
      if (!duplicated.ok) console.log(duplicated.error.type); // "duplicate-id"
    • Spaces every position again from the start position, keeping the current order: the positions become as short as possible with the most room between them. Use it after a "position" error (the threshold or the overflow was reached), then try the operation again.

      Like sort, it is all or nothing: when the system has no room for every item the error is returned and nothing changes.

      Returns Result<[LexicalItem<K, T>[], LexicalItem<K, T>[]], LexicalError>

      The items whose position changed (only these need to be saved) and every item in order, or a "position" error if the system has no room for all of them.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));
      list.set([{ id: 1, position: "aaaaa" }, { id: 2, position: "bbbbb" }]);

      const rebalanced = list.rebalance();
      if (rebalanced.ok) {
      const [affected] = rebalanced.value;
      console.log(affected.map((item) => item.position.toString())); // ["mmmmm!", "mmmmn!"], save these
      }
    • Selects the item with the id, for example the one being dragged or edited. An id that is not in the list clears the selection.

      Parameters

      • id: K | null | undefined

        The id of the item to select.

      Returns void

    • Replaces the content of the list with the passed values.

      By default the values are trusted: they are expected to be already sorted by their position (e.g. loaded with an ORDER BY position query), so they are linked in the same order and no position is changed. The order is still verified (one comparison per item), failing with a "not-ordered" error if a position is not strictly greater than the previous one, and with a "position" error if a position is empty or can't be forged.

      With untrusted the values are sorted by their position and repaired instead of rejected:

      • Equal positions keep the first one and give new positions to the others, right after it.
      • Empty or invalid positions are given new positions at the end of the list (in the incoming order).
      • If there is no room to repair them, every position is spaced again from the start.

      Nothing is changed (neither the list nor the passed values) when an error is returned. In both modes null, undefined or repeated ids fail with an "invalid-id" or "duplicate-id" error.

      Parameters

      • list: T[]

        The values to be stored in the list.

      • Optionaloptions: { untrusted?: boolean }
        • Optionaluntrusted?: boolean

          If set, sorts and repairs the positions instead of failing.

      Returns Result<[affected: LexicalItem<K, T>[], list: LexicalList<K, T>], LexicalError>

      The items whose position changed (only these need to be saved) and this list.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));

      // Trusted (default): the rows come sorted from the database, e.g. ORDER BY position
      const loaded = list.set([
      { id: 1, position: "mmmmm!" },
      { id: 2, position: "mmmmn!" },
      ]);
      if (loaded.ok) console.log(loaded.value[0].length); // 0, nothing to save

      // Untrusted: sorted, and repeated or empty positions repaired
      const repaired = list.set(
      [
      { id: 1, position: "ccccc" },
      { id: 2, position: "aaaaa" },
      { id: 3, position: "aaaaa" }, // repeated
      { id: 4, position: "" }, // missing
      ],
      { untrusted: true },
      );
      if (repaired.ok) {
      const [affected] = repaired.value;
      // Order: 2, 3, 1, 4. Save only the repaired rows:
      console.log(affected.map((item) => `${item.id}: ${item.position}`)); // ["3: bbbbb!", "4: ccccd!"]
      }
    • Sorts the items with a compare function and spaces every position again from the start position, going next one by one.

      Use it to sort by any criteria. To space the positions again keeping the current order (after a "position" error, when the threshold or the overflow was reached) use rebalance.

      It is all or nothing: the room for every position is verified before anything changes, so when the system has no room for every item the error is returned and neither the list nor your objects are changed.

      Parameters

      • compareFn: (a: LexicalItem<K, T>, b: LexicalItem<K, T>) => number

        Compares two items, like Array.prototype.sort: negative if a goes first.

      Returns Result<[LexicalItem<K, T>[], LexicalItem<K, T>[]], LexicalError>

      The items whose position changed (only these need to be saved) and every item in the new order, or a "position" error if the system has no room for all of them.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));
      list.set([{ id: 1, position: "aaaaa" }, { id: 2, position: "bbbbb" }]);

      // Highest id first
      const sorted = list.sort((a, b) => b.id - a.id);
      if (sorted.ok) {
      const [affected] = sorted.value;
      console.log(affected.map((item) => item.position.toString())); // ["mmmmm!", "mmmmn!"], save these
      }
    • Gets every item in the list order.

      Returns Result<LexicalItem<K, T>[], LexicalError>

      A result with the items, from the first to the last.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));
      list.push({ id: 1, position: "" });
      list.unshift({ id: 2, position: "" });

      const items = list.toArray();
      if (items.ok) console.log(items.value.map((item) => item.id)); // [2, 1]
    • Adds an object at the start of the list, with the position before the first item (or the start position if the list is empty). Its position field is set, so the object itself can be saved.

      Parameters

      • value: T

        The object to add.

      Returns Result<LexicalItem<K, T>, LexicalError>

      The added item (the only one to save), or the error: "invalid-id", "duplicate-id", or "position" if there is no room left before the first item.

      import Staquia, { LexicalList } from "staquia";

      const list = new LexicalList(Staquia.create("!", "a", "z", 5, 15, 0.15));
      list.push({ id: 1, text: "Milk", position: "" }); // "mmmmm!"

      const bread = { id: 2, text: "Bread", position: "" };
      const added = list.unshift(bread);
      if (added.ok) console.log(bread.position); // "mmmml!", before "Milk"