• Stars
    star
    308
  • Rank 135,672 (Top 3 %)
  • Language
    JavaScript
  • License
    Creative Commons ...
  • Created almost 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fractional Indexing in JavaScript

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, // start
  b: string | null | undefined, // end
  digits?: string | undefined = BASE_62_DIGITS, // optional character encoding
): string;
import { generateKeyBetween } from 'fractional-indexing';

const first = generateKeyBetween(null, null); // "a0"

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

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

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

// Insert in between 2nd and 3rd (midpoint)
const secondAndHalf = 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.

generateNKeysBetween(
  a: string | null | undefined, // start
  b: string | null | undefined, // end
  n: number // number of keys to generate evenly between start and end
  digits?: string | undefined = BASE_62_DIGITS, // optional character encoding
): string[];
import { generateNKeysBetween } from 'fractional-indexing';

const first = generateNKeysBetween(null, null, 2); // ['a0', 'a1']

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

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

// Insert two keys in between 1st and 2nd (midpoints)
generateNKeysBetween(second, third, 2); // ['a0G', 'a0V']

Other Languages

These should be byte-for-byte compatible.

Language Repo
Go https://github.com/rocicorp/fracdex
Python https://github.com/httpie/fractional-indexing-python
Kotlin https://github.com/darvelo/fractional-indexing-kotlin

More Repositories

1

replicache

Realtime Sync for Any Backend Stack
1,029
star
2

repliear

TypeScript
151
star
3

replidraw

TypeScript
87
star
4

replicache-old

Realtime Sync for Any Backend Stack
71
star
5

todo-nextjs

Starter Todo App
TypeScript
66
star
6

undo

Undo Redo Manager
TypeScript
37
star
7

rails

Replicache on Rails
TypeScript
34
star
8

fracdex

Fractional indexing in Golang
Go
31
star
9

repc

The canonical Replicache client, implemented in Rust.
Rust
31
star
10

replicache-react

Miscellaneous utilities for using Replicache with React
TypeScript
23
star
11

replicache-nextjs

Generic Replicache backend for Next.js
TypeScript
20
star
12

reflect-draw

TypeScript
17
star
13

diff-server

Replicache Diff Server
Go
14
star
14

replicache-quickstarts

Replicache examples integrating with various frontend frameworks
TypeScript
13
star
15

lock

Provides Lock and RwLock synchronization primitives.
TypeScript
13
star
16

logger

Context-carrying logger with conditional methods.
TypeScript
11
star
17

replicache-client

Replicache client library and CLI
Go
9
star
18

replicache-express

Generic Repliache Express Backend Server
TypeScript
7
star
19

compare-utf8

Compares JS strings using UTF-8 bitwise semantics
JavaScript
7
star
20

replicache-sample-chat

JavaScript
6
star
21

replicache-sdk-flutter

Replicant SDK for Flutter
Dart
6
star
22

resolver

A simple promise resolver
TypeScript
6
star
23

reflect-starter-vanilla

JavaScript
5
star
24

byob-starter

Bring Your Own Backend Starter
TypeScript
4
star
25

todo-wc

Todo Web Component Example for Replicache
TypeScript
4
star
26

replicache-sample-todo

Sample Replicache integration that implements a Todo service
Go
4
star
27

replicache-simple-backend

Simple Replicache backend on Supabase and TypeScript
TypeScript
3
star
28

replicache-firestore

Replicache POC Backend on Firestore
TypeScript
2
star
29

replicache-sample-websql

TypeScript
2
star
30

todo-react

TypeScript
2
star
31

replicache-yjs

Experiments with YJS
TypeScript
2
star
32

reflect-starter-ts-vanilla

TypeScript
2
star
33

reflect-orchestrator

Library for orchestrating the assignment of Reflect clients to rooms with a max client capacity.
TypeScript
2
star
34

replicache-sample-datalayer-dotnet-cosmosdb

A sample Replicache data layer for .Net using C# and CosmosDB
C#
2
star
35

reflect-loop

Demo for collaborative audio/visual experience
TypeScript
2
star
36

datadog-util

TypeScript
1
star
37

replicache-worker

Experimental Cloudflare Worker backend for Replicache
TypeScript
1
star
38

cf-global-state

TypeScript
1
star
39

reflect-docs

Docs for Reflect.net
MDX
1
star