Staquia API
    Preparing search index...

    Class Staquia

    A lexicographic number system that creates positions: short strings that keep the order of a list. Sort the items by their position string and you get the order back; moving an item only changes its own position.

    Create one with Staquia.create (or Staquia.default), then use it to create positions, or give it to a LexicalList to keep the positions of your objects in sync. Nothing is thrown: every function that can fail returns a Result.

    import Staquia from "staquia";

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

    const start = system.start(); // "mmmmm!"
    const next = system.next(start); // "mmmmn!", stacked after start
    const core = system.core(start, next); // "mmmmmn", between both

    if (core.ok) console.log(core.value.toString()); // "mmmmmn"

    Read the guide at the Staquia documentation page.

    Implements

    Index

    Constructors

    Methods

    • Creates a position between two others, to move or insert an item between its neighbors.

      The room left in the N section is used first. When the positions are adjacent, the Z section takes the middle value, making the position one digit longer. Moving items again and again into the same gap makes positions grow until the threshold (ThresholdReachedError) and the overflow (OverflowError).

      Parameters

      Returns IPositionResult

      The position between previous and next, or the error (including a SystemMismatchError if a position was created by a system with another configuration).

    • Creates a position from its string, for example one stored in a database.

      When the string is shorter than the N section (ln characters), N is completed with zeroes to the right, which keeps the string order ("xy" becomes "xy!!!!").

      Parameters

      • string: string

        The string representation of the position.

      Returns IPositionResult

      The position, or the error: InvalidCharacterError for a character that is not a digit of the system, OverflowError if it is too long, ThresholdReachedError if it reached the threshold.

    • Creates a position from its digits, the inverse of position.digits().

      The digits are split in the N and Z sections of the system: N takes the ln most significant digits.

      Parameters

      • number: IDigits

        The digits of the position.

      Returns IPositionResult

      The position, or the error if it is too long (OverflowError) or reached the threshold (ThresholdReachedError).

    • The alphabet: ! as the zero and the lowercase letters from a to z (base 27). Readable positions, safe in URLs, the system of every example of the documentation.

      Parameters

      • Optionaloptions: IPresetOptions

        Changes the lengths, the threshold or the classes, see IPresetOptions.

      Returns Staquia

      import Staquia from "staquia";

      const start = Staquia.alphabet().start();
      if (start.ok) console.log(start.value.toString()); // "mmmmm!"
    • Plain ASCII: ! as the zero and every printable character from # to ~ except ', \ and ` (base 90). The most compact choice per byte, and nothing to escape in JSON, SQL literals or logs.

      Parameters

      • Optionaloptions: IPresetOptions

        Changes the lengths, the threshold or the classes, see IPresetOptions.

      Returns Staquia

      import Staquia from "staquia";

      const start = Staquia.ascii().start();
      if (start.ok) console.log(start.value.toString()); // "PPPPP!"
    • Base 36: 0 as the zero, the digits 1-9 and the lowercase letters (base 36). Safe with case insensitive collations (the MySQL default) and where keys must be alphanumeric.

      Parameters

      • Optionaloptions: IPresetOptions

        Changes the lengths, the threshold or the classes, see IPresetOptions.

      Returns Staquia

      import Staquia from "staquia";

      const start = Staquia.base36().start();
      if (start.ok) console.log(start.value.toString()); // "iiiii0"
    • Creates a number system. Its digits are the zero and every character from first to last, so its base is b = (last - first) + 2 (by their unicode values).

      Use characters of the Basic Multilingual Plane (up to U+FFFF), so JavaScript, UTF-8 and UTF-16 compare them in the same order. The characters must be zero < first < last.

      Parameters

      • zero: string

        The character of the zero digit.

      • first: string

        The character of the first digit (1).

      • last: string

        The character of the last digit (b - 1).

      • ln: number

        The fixed length of the N section, used to stack items without making positions longer.

      • lz: number

        The max length of the Z section, used when items are moved between two others.

      • fϰ: number

        A factor from 0 to 1: the threshold is reached at the length lϰ = ceil((1 - fϰ) * (ln + lz)).

      • Optionaloptions: IStaquiaOptions

        Replaces the classes used to create positions and numbers, see IStaquiaOptions.

      Returns Staquia

      The Staquia system.

      import Staquia from "staquia";

      // The alphabet: "!" is 0, "a" is 1 and "z" is 26, a base 27 system
      const alphabet = Staquia.create("!", "a", "z", 5, 15, 0.15);

      const start = alphabet.start();
      if (start.ok) console.log(start.value.toString()); // "mmmmm!"
    • Creates the default number system, ready for production: the zero !, the digits from \u00B0 (°) to \u04B1 (ұ), a base 1027 system, with ln = 5, lz = 15 and fϰ = 0.15.

      Parameters

      Returns Staquia

      The default Staquia system.

      import Staquia from "staquia";

      const system = Staquia.default();
      // The same as
      const same = Staquia.create("!", "\u00B0", "\u04B1", 5, 15, 0.15);

      const start = system.start();
      if (start.ok) console.log(start.value.toString()); // "ʰʰʰʰʰ!"

      Try other settings for your project in the Setup App of the documentation.

    • Creates a number system from a set of characters: the zero followed by every digit, sorted by their code, so its base is the number of characters. Use it when the digits are not a range, for example to leave some characters out; a set whose digits are consecutive is the same system as create with that range, and their positions are compatible.

      The characters must be sorted by their code without repeating any, and be in the Basic Multilingual Plane (up to U+FFFF), so JavaScript, UTF-8 and UTF-16 compare them in the same order.

      Parameters

      • characters: string

        The zero followed by every digit, sorted by their code.

      • ln: number

        The fixed length of the N section.

      • lz: number

        The max length of the Z section.

      • fϰ: number

        A factor from 0 to 1: the threshold is reached at the length lϰ = ceil((1 - fϰ) * (ln + lz)).

      • Optionaloptions: IStaquiaOptions

        Replaces the classes used to create positions and numbers, see IStaquiaOptions.

      Returns Staquia

      import Staquia from "staquia";

      // The digits without the vowels: "0" is the zero
      const system = Staquia.fromCharacters("0bcdfghjklmnpqrstvwxyz", 5, 15, 0.15);
      const start = system.start();
      if (start.ok) console.log(start.value.toString()); // "nnnnn0"
    • URL safe: the characters of URL safe base64 (-, 0-9, A-Z, _, a-z) sorted by their code, - as the zero (base 64). Positions go in URLs, file names and HTML ids without any encoding.

      The characters are not in the base64 order: positions are sort keys, not encoded data, so they keep the order a database compares them in.

      Parameters

      • Optionaloptions: IPresetOptions

        Changes the lengths, the threshold or the classes, see IPresetOptions.

      Returns Staquia

      import Staquia from "staquia";

      const start = Staquia.urlSafe().start();
      if (start.ok) console.log(start.value.toString()); // "VVVVV-"