fractional_indexer 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +87 -8
- data/lib/fractional_indexer/version.rb +1 -1
- data/lib/fractional_indexer.rb +36 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6ee81d4e4b26c262bb862ec3b52558808d483bfb67fdb4c32889a7b3cc6c822
|
4
|
+
data.tar.gz: 2bd077740a8272885e6703f04abdc1361f3171c43156a59794b40bada2c608fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cc2e6cc619a29bcb7cfae8c2fb6e2fbbda62ee17823b217717ac357e93de4bf24830fe3ffd98ce48ba5d19f293fb68d10df4bf4926b8619a61bcfd67be0f826
|
7
|
+
data.tar.gz: ef547fa499d8bf94c57db52da6fb1b146fe6ce4ba4f14b24123c5e30e125da4e904605918dc314ec76dfebf60deff5af406ab50dbe096b1b24f288e39b4093c0
|
data/README.md
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# Fractional Indexer
|
2
2
|
|
3
3
|
[](https://codecov.io/gh/kazu-2020/fractional_indexer)
|
4
|
+
[](https://github.com/kazu-2020/fractional_indexer/actions/workflows/ruby.yml)
|
4
5
|
|
5
|
-
|
6
|
+
> efficient data insertion and sorting through fractional indexing
|
6
7
|
|
7
|
-
|
8
|
+
This gem is designed to achieve "[Realtime editing of ordered sequences](https://www.figma.com/blog/realtime-editing-of-ordered-sequences/#fractional-indexing)" as featured in a Figma blog post.
|
9
|
+
|
10
|
+
Specifically, it implements Fractional Indexing as a means of managing order, realized through strings. By managing indexes as strings, it significantly delays the occurrence of index duplication that can arise when implementing Fractional Indexing with floating-point numbers. Additionally, using strings allows for the representation of a much larger range of numbers in a single digit compared to decimal numbers (by default, a single digit is represented in base-62).
|
11
|
+
|
12
|
+
This gem was implemented based on the excellent article "[Implementing Fractional Indexing.](https://observablehq.com/@dgreensp/implementing-fractional-indexing)" I would like to express my gratitude to David Greenspan, the author of the article.
|
8
13
|
|
9
14
|
## Installation
|
10
15
|
|
@@ -24,17 +29,91 @@ Or install it yourself as:
|
|
24
29
|
|
25
30
|
## Usage
|
26
31
|
|
27
|
-
|
32
|
+
### Generate Order key
|
33
|
+
|
34
|
+
To generate an order key, use the `FractionalIndexer.generate_key` method. This method can take two arguments: prev_key and next_key, both of which can be either nil or a string (excluding empty strings).
|
35
|
+
|
36
|
+
The characters that can be used in the strings are available for review in `FractionalIndexer.configuration.digits`.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'fractional_indexer'
|
40
|
+
|
41
|
+
# create first order key
|
42
|
+
FractionalIndexer.generate_key
|
43
|
+
# => "a0"
|
44
|
+
|
45
|
+
# increment
|
46
|
+
FractionalIndexer.generate_key(prev_key: 'a0')
|
47
|
+
# => "a1"
|
48
|
+
|
49
|
+
# decrement
|
50
|
+
FractionalIndexer.generate_key(next_key: 'a0')
|
51
|
+
# => "Zz"
|
52
|
+
|
53
|
+
# between prev_key and next_key
|
54
|
+
FractionalIndexer.generate_key(prev_key: 'a0', next_key: 'a2')
|
55
|
+
# => "a1"
|
56
|
+
|
57
|
+
# prev_key should be less than next_key
|
58
|
+
FractionalIndexer.generate_key(prev_key: 'a2', next_key: 'a1')
|
59
|
+
# => error
|
60
|
+
FractionalIndexer.generate_key(prev_key: 'a1', next_key: 'a1')
|
61
|
+
# => error
|
62
|
+
```
|
63
|
+
|
64
|
+
### Generate Multiple Order keys
|
65
|
+
|
66
|
+
To generate multiple order keys, use the `FractionalIndexer.generate_keys` method.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'fractional_indexer'
|
70
|
+
|
71
|
+
# generate n order keys that sequentially follow a given prev_key.
|
72
|
+
FractionalIndexer.generate_keys(prev_key: "b11", count: 5)
|
73
|
+
# => ["b12", "b13", "b14", "b15", "b16"]
|
74
|
+
|
75
|
+
# generate n order keys that sequentially precede a given next_key.
|
76
|
+
FractionalIndexer.generate_keys(next_key: "b11", count: 5)
|
77
|
+
# => ["b0w", "b0x", "b0y", "b0z", "b10"]
|
28
78
|
|
29
|
-
|
79
|
+
# generate n order keys between the given prev_key and next_key.
|
80
|
+
FractionalIndexer.generate_keys(prev_key: "b10", next_key: "b11", count: 5)
|
81
|
+
# => ["b108", "b10G", "b10V", "b10d", "b10l"]
|
82
|
+
```
|
83
|
+
|
84
|
+
## Configure
|
85
|
+
|
86
|
+
### base
|
30
87
|
|
31
|
-
|
88
|
+
You can set the base (number system) used to represent each digit. The possible values are :base_10, :base_62, and :base_94. (The default is :base_62.)
|
32
89
|
|
33
|
-
|
90
|
+
Note: base_10 is primarily intended for operational verification and debugging purposes.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'fractional_indexer'
|
94
|
+
|
95
|
+
FractionalIndexer.configure do |config|
|
96
|
+
config.base = :base_10
|
97
|
+
end
|
98
|
+
FractionalIndexer.configuration.digits.join
|
99
|
+
# => "0123456789"
|
100
|
+
|
101
|
+
FractionalIndexer.configure do |config|
|
102
|
+
config.base = :base_62
|
103
|
+
end
|
104
|
+
FractionalIndexer.configuration.digits.join
|
105
|
+
# => "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
106
|
+
|
107
|
+
FractionalIndexer.configure do |config|
|
108
|
+
config.base = :base_94
|
109
|
+
end
|
110
|
+
FractionalIndexer.configuration.digits.join
|
111
|
+
# => "!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
112
|
+
```
|
34
113
|
|
35
114
|
## Contributing
|
36
115
|
|
37
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
116
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kazu-2020/fractional_indexer.
|
38
117
|
|
39
118
|
## License
|
40
119
|
|
data/lib/fractional_indexer.rb
CHANGED
@@ -50,6 +50,42 @@ module FractionalIndexer
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
+
def self.generate_keys(prev_key: nil, next_key: nil, count: 1)
|
54
|
+
return [] if count <= 0
|
55
|
+
return [generate_key(prev_key: prev_key, next_key: next_key)] if count == 1
|
56
|
+
|
57
|
+
if next_key.nil?
|
58
|
+
base_order_key = generate_key(prev_key: prev_key)
|
59
|
+
result = [base_order_key]
|
60
|
+
|
61
|
+
(count - 1).times do
|
62
|
+
result << generate_key(prev_key: result.last)
|
63
|
+
end
|
64
|
+
|
65
|
+
return result
|
66
|
+
end
|
67
|
+
|
68
|
+
if prev_key.nil?
|
69
|
+
base_order_key = generate_key(next_key: next_key)
|
70
|
+
result = [base_order_key]
|
71
|
+
|
72
|
+
(count - 1).times do
|
73
|
+
result << generate_key(next_key: result.last)
|
74
|
+
end
|
75
|
+
|
76
|
+
return result.reverse
|
77
|
+
end
|
78
|
+
|
79
|
+
mid = count / 2
|
80
|
+
base_order_key = generate_key(prev_key: prev_key, next_key: next_key)
|
81
|
+
|
82
|
+
[
|
83
|
+
*generate_keys(prev_key: prev_key, next_key: base_order_key, count: mid),
|
84
|
+
base_order_key,
|
85
|
+
*generate_keys(prev_key: base_order_key, next_key: next_key, count: count - mid - 1),
|
86
|
+
]
|
87
|
+
end
|
88
|
+
|
53
89
|
def self.decrement(order_key)
|
54
90
|
return order_key.integer + Midpointer.execute("", order_key.fractional) if order_key.minimum_integer?
|
55
91
|
|