https://github.com/rocicorp/fracdex
Fractional indexing in Golang
https://github.com/rocicorp/fracdex
Last synced: over 1 year ago
JSON representation
Fractional indexing in Golang
- Host: GitHub
- URL: https://github.com/rocicorp/fracdex
- Owner: rocicorp
- License: cc0-1.0
- Created: 2020-11-15T04:10:42.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-12-11T17:55:10.000Z (over 1 year ago)
- Last Synced: 2025-03-24T08:47:56.796Z (over 1 year ago)
- Language: Go
- Size: 21.5 KB
- Stars: 33
- Watchers: 4
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fractional Indexing
This is based on [Implementing Fractional Indexing
](https://observablehq.com/@dgreensp/implementing-fractional-indexing) by [David Greenspan
](https://github.com/dgreensp).
Fractional indexing is a technique to create an ordering that can be used for [Realtime Editing of Ordered Sequences](https://www.figma.com/blog/realtime-editing-of-ordered-sequences/).
This implementation includes variable-length integers, and the prepend/append optimization described in David's article.
This should be byte-for-byte compatible with https://github.com/rocicorp/fractional-indexing.
## Example
```go
package main
import (
"fmt"
"roci.dev/fracdex"
)
func main() {
first, _ := fracdex.KeyBetween("", "") // a0
fmt.Println(first)
// Insert after 1st
second, _ := fracdex.KeyBetween(first, "") // "a1"
fmt.Println(second)
// Insert after 2nd
third, _ := fracdex.KeyBetween(second, "") // "a2"
fmt.Println(third)
// Insert before 1st
zeroth, _ := fracdex.KeyBetween("", first) // "Zz"
fmt.Println(zeroth)
// Insert in between 2nd and 3rd
secondAndHalf, _ := fracdex.KeyBetween(second, third) // "a1V"
fmt.Println(secondAndHalf)
}
```