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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb26a758da0bb2de8108cd4a77fc48779488abaa647e908a02bb3d8476a29b5e
4
- data.tar.gz: 1ea8bf3b0c21a1d70daba1843bc15ff6a50943b1b1510a91631260c37ff53def
3
+ metadata.gz: c6ee81d4e4b26c262bb862ec3b52558808d483bfb67fdb4c32889a7b3cc6c822
4
+ data.tar.gz: 2bd077740a8272885e6703f04abdc1361f3171c43156a59794b40bada2c608fd
5
5
  SHA512:
6
- metadata.gz: dffb6fcbf7e72af343944c33d35cd287a0e7fb7310b42064b6e567de50f6cbf551a5f05132102484080f6ce0062a762effd1b5eac6bc177c26f838ab6c35bfbc
7
- data.tar.gz: d0316fe102d0e6259a76f699affc2016c1b6ac891d8e22740ef3c0b316124490475c406632f6bcbd75f21715da13e0991a0589ca57d76b023094823d28378b68
6
+ metadata.gz: 7cc2e6cc619a29bcb7cfae8c2fb6e2fbbda62ee17823b217717ac357e93de4bf24830fe3ffd98ce48ba5d19f293fb68d10df4bf4926b8619a61bcfd67be0f826
7
+ data.tar.gz: ef547fa499d8bf94c57db52da6fb1b146fe6ce4ba4f14b24123c5e30e125da4e904605918dc314ec76dfebf60deff5af406ab50dbe096b1b24f288e39b4093c0
data/README.md CHANGED
@@ -1,10 +1,15 @@
1
- # FractionalIndexer
1
+ # Fractional Indexer
2
2
 
3
3
  [![codecov](https://codecov.io/gh/kazu-2020/fractional_indexer/graph/badge.svg?token=OCCYE4EKT1)](https://codecov.io/gh/kazu-2020/fractional_indexer)
4
+ [![test](https://github.com/kazu-2020/fractional_indexer/actions/workflows/ruby.yml/badge.svg?branch=main&event=push)](https://github.com/kazu-2020/fractional_indexer/actions/workflows/ruby.yml)
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fractional_indexer`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+ > efficient data insertion and sorting through fractional indexing
6
7
 
7
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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/[USERNAME]/fractional_indexer.
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FractionalIndexer
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fractional_indexer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazu