fractional_indexing_dart 1.0.1 copy "fractional_indexing_dart: ^1.0.1" to clipboard
fractional_indexing_dart: ^1.0.1 copied to clipboard

A Dart implementation of fractional indexing for efficient list ordering and reordering without large-scale updates. Ideal for CRDTs and real-time apps.

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.

String generateKeyBetween(
  String? a, // start
  String? b, // end
  [String digits = base62Digits], // optional character encoding
);
import 'package:fractional_indexing_dart/fractional_indexing_dart.dart' as findex;

final first = findex.generateKeyBetween(null, null); // "a0"

// Insert after 1st
final second = findex.generateKeyBetween(first, null); // "a1"

// Insert after 2nd
final third = findex.generateKeyBetween(second, null); // "a2"

// Insert before 1st
final zeroth = findex.generateKeyBetween(null, first); // "Zz"

// Insert in between 2nd and 3rd (midpoint)
final secondAndHalf = findex.generateKeyBetween(second, third); // "a1V"

generateNKeysBetween #

Use this when generating multiple keys at some known position, as it spaces out indexes more evenly and leads to shorter keys.

List<String> generateNKeysBetween(
  String? a, // start
  String? b, // end
  int n, // number of keys to generate evenly between start and end
  [String digits = base62Digits], // optional character encoding
);
import 'package:fractional_indexing_dart/fractional_indexing_dart.dart' as findex;

final first = findex.generateNKeysBetween(null, null, 2); // ['a0', 'a1']

// Insert two keys after 2nd
findex.generateNKeysBetween(first[1], null, 2); // ['a2', 'a3']

// Insert two keys before 1st
findex.generateNKeysBetween(null, first[0], 2); // ['Zy', 'Zz']

// Insert two keys in between 1st and 2nd (midpoints)
// Assuming second and third are defined as above
findex.generateNKeysBetween(second, third, 2); // ['a0G', 'a0V']

Sorting #

The indexes generated by this library are case-sensitive. Dart's default String comparison is case-sensitive, so standard sorting works out of the box.

class Todo {
  final String id;
  final String content;
  final String fractionalIndex;

  Todo(this.id, this.content, this.fractionalIndex);
}

void main() {
  final todos = [
    Todo("todo_1253241", "Read the docs", "YzZ"),
    Todo("todo_8973942", "Open a PR", "Yza"),
  ];

  todos.sort((a, b) => a.fractionalIndex.compareTo(b.fractionalIndex));
}

Other Implementations #

Languages #

This is a Dart port of the original JavaScript implementation by @rocicorp. That means that this implementation is byte-for-byte compatible with:

Language Repo
JavaScript https://github.com/rocicorp/fractional-indexing
Go https://github.com/rocicorp/fracdex
Python https://github.com/httpie/fractional-indexing-python
Kotlin https://github.com/darvelo/fractional-indexing-kotlin
Ruby https://github.com/kazu-2020/fractional_indexer
3
likes
0
points
357
downloads

Publisher

verified publisherblubox.dev

Weekly Downloads

A Dart implementation of fractional indexing for efficient list ordering and reordering without large-scale updates. Ideal for CRDTs and real-time apps.

Repository (GitHub)
View/report issues

Topics

#algorithms #sorting #crdt #indexing #list-ordering

License

unknown (license)

More

Packages that depend on fractional_indexing_dart