Fractional Indexing
This is based on Implementing Fractional Indexing
by David Greenspan
.
Fractional indexing is a technique to create an ordering that can be used for Realtime Editing of Ordered Sequences.
This implementation includes variable-length integers, and the prepend/append optimization described in David's article.
API
generateKeyBetween
Generate a single key in between two points.
generateKeyBetween(
a: string | null | undefined,
b: string | null | undefined,
digits?: string | undefined = BASE_62_DIGITS,
): string;
import { generateKeyBetween } from 'fractional-indexing';
const first = generateKeyBetween(null, null);
const second = generateKeyBetween(first, null);
const third = generateKeyBetween(second, null);
const zeroth = generateKeyBetween(null, first);
const secondAndHalf = generateKeyBetween(second, third);
generateNKeysBetween
Use this when generating multiple keys at some known position, as it spaces out indexes more evenly and leads to shorter keys.
generateNKeysBetween(
a: string | null | undefined,
b: string | null | undefined,
n: number
digits?: string | undefined = BASE_62_DIGITS,
): string[];
import { generateNKeysBetween } from 'fractional-indexing';
const first = generateNKeysBetween(null, null, 2);
generateNKeysBetween(first[1], null, 2);
generateNKeysBetween(null, first[0], 2);
generateNKeysBetween(second, third, 2);
Other Languages
These should be byte-for-byte compatible.