Gets the item at an index of the list order, walking from the first item.
The 0-based index.
A result with the item, or with undefined if the index is out of bounds or not a number.
Removes every item and the selection. Your objects are not changed.
Removes an item. The other positions don't change, so nothing else needs to be saved.
The id of the item to remove.
An empty success, or a "not-found" error.
Releases the internal structures. The list can't be used after it.
Gets an item by its id. The item exposes its id, its position and the ids of its previous and next items.
The id of the item.
The item, or undefined if there is no item with that id.
Gets the first item of the list, or undefined if it is empty.
Gets the position an item would take at the start of the list: the one before the first item, or the start position if the list is empty.
The position, or a "position" error if there is no room before the first item.
Gets the last item of the list, or undefined if it is empty.
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.
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.
The object to add.
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.
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.
The id of the item to select.
Gets the selected item, see select. The selection is cleared when the item is deleted,
or when the list is cleared or replaced.
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:
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.
The values to be stored in the list.
Optionaloptions: { untrusted?: boolean }Optionaluntrusted?: booleanIf set, sorts and repairs the positions instead of failing.
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.
Compares two items, like Array.prototype.sort: negative if a goes first.
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 the position an item would take at the end of the list: the one after the last item, or the start position if the list is empty.
The position, or a "position" error if there is no room after the last item.
Gets every item in the list order.
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.
The object to add.
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"
A list that keeps the order of your objects and their positions in sync, as a doubly linked list.
Any object with an
id(nevernullorundefined) and apositionstring can be stored. Every operation updates thepositionfield 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 aResult, and when one fails neither the list nor your objects are changed.Example