range_compressor 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83e62be00199f227f3dae78b086d5f63759205cfa19c5eeb255bf5787b33450b
4
- data.tar.gz: f62233009ec12d166fb2072897e8ecc95336ec6df6745facaa7b7cb530d9c2f7
3
+ metadata.gz: 745781dd4c3d89b99798e84fc67df0836fef43b16681fc9230e22da77663837d
4
+ data.tar.gz: ae73d9c9041ee39ea49a54a96be950ce6e95b9f3c9dad0b3872b7dd7bd2587d9
5
5
  SHA512:
6
- metadata.gz: 4ca724e589d5827ce79c89556acf4b97d8984f65d8c417f66b227a64b82d5e8b1aa3e20d372818214146ef750d8f445ac731136d1cbc1862de7c3152b29af1a7
7
- data.tar.gz: 178feded786fb56dbcd52574a4dca4cae421ef96f37318d3c778206d83849aa9b1d221fb91750c466945eb05fe21342ad381322e3656a8b427a8831f7c11998e
6
+ metadata.gz: 4d57300e18de3c4deb59a9607ece9f44cb5dfdd4beab2fb9048e032c46a37b17c0af090c5fd104b5500e3618d94e956b53ae8ec5ac13b2d4baff1203cc8457dd
7
+ data.tar.gz: 479f08611f4e1e2edff798e961ef9ad038209cff75855e7de117fd6453945d6d1b349a9413d7764f184f0ffd1e29311f4c38b56454fc9ea51b6fd41c1ca5cd00
@@ -0,0 +1,22 @@
1
+ name: tests
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+
9
+ strategy:
10
+ matrix:
11
+ ruby: [ '2.4', '3.0', 'ruby-head' ]
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby ${{ matrix.ruby }}
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby }}
19
+ - name: Install dependencies
20
+ run: bundle install --jobs 4
21
+ - name: Test with Rake
22
+ run: bundle exec rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [1.1.0]
8
+
9
+ ### Added
10
+
11
+ - support for compressing lists of Ranges as well as mixed and nested inputs
12
+
13
+ ### Fixed
14
+
15
+ - fixed implicit dependency on `sorted_set` in Ruby >= 3.0 by no longer requiring `sorted_set`
16
+
17
+ ## [1.0.0]
18
+
19
+ Initial release.
data/README.md CHANGED
@@ -1,19 +1,16 @@
1
1
  # RangeCompressor
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/range_compressor.svg)](http://badge.fury.io/rb/range_compressor)
4
- [![Build Status](https://travis-ci.org/janosch-x/range_compressor.svg?branch=master)](https://travis-ci.org/janosch-x/range_compressor)
4
+ [![Build Status](https://github.com/jaynetics/range_compressor/workflows/tests/badge.svg)](https://github.com/jaynetics/range_compressor/actions)
5
5
 
6
- A micro-gem that turns this:
6
+ A micro-gem that compresses enumerables into Ranges.
7
7
 
8
- ```ruby
9
- [1, 2, 3, 4, 6, 8, 9, 10]
10
- ```
11
-
12
- into this:
13
-
14
- ```ruby
15
- [1..4, 6..6, 8..10]
16
- ```
8
+ | Input | Output |
9
+ |----------------------------|----------------------|
10
+ | [1, 2, 3, 4, 6, 8, 9, 10] | [1..4, 6..6, 8..10] |
11
+ | %w[a b c e g h] | ['a'..'c', 'e'..'h'] |
12
+ | [1..5, 3..8] | [1..8] |
13
+ | [Set[1, 2], 3..6, [9, 10]] | [1..6, 9..10] |
17
14
 
18
15
  ## Installation
19
16
 
@@ -1,3 +1,3 @@
1
1
  module RangeCompressor
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -3,27 +3,15 @@ require 'range_compressor/version'
3
3
  module RangeCompressor
4
4
  module_function
5
5
 
6
- SORTED_SET_CLASSES = %w[
7
- CharacterSet
8
- CharacterSet::Pure
9
- ImmutableSet
10
- SortedSet
11
- ].freeze
12
-
13
- # Set#divide is very slow unfortunately, else it would be nice for this:
14
- # divide { |i, j| (i - j).abs == 1 }
15
- def compress(enum)
16
- if enum.class.ancestors.none? { |anc| SORTED_SET_CLASSES.include? anc.to_s }
17
- require 'set'
18
- enum = SortedSet.new(enum)
19
- end
6
+ def compress(arg)
7
+ sorted_set = to_sorted_set(arg)
20
8
 
21
9
  ranges = []
22
10
  previous = nil
23
11
  current_start = nil
24
12
  current_end = nil
25
13
 
26
- enum.each do |object|
14
+ sorted_set.each do |object|
27
15
  if previous.nil?
28
16
  current_start = object
29
17
  elsif previous.next != object
@@ -40,4 +28,35 @@ module RangeCompressor
40
28
 
41
29
  ranges
42
30
  end
31
+
32
+ SORTED_SET_CLASSES = %w[
33
+ CharacterSet
34
+ CharacterSet::Pure
35
+ ImmutableSet
36
+ SortedSet
37
+ ].freeze
38
+
39
+ def to_sorted_set(arg)
40
+ if arg.nil?
41
+ []
42
+ elsif arg.class.ancestors.any? { |anc| SORTED_SET_CLASSES.include? anc.to_s }
43
+ arg
44
+ elsif arg.respond_to?(:each)
45
+ hash = {}
46
+ each_flattened(arg) { |el| hash[el] = true }
47
+ hash.keys.sort
48
+ else
49
+ raise(ArgumentError, 'value must be enumerable')
50
+ end
51
+ end
52
+
53
+ def each_flattened(arg, &block)
54
+ if arg.class == Range && (arg.begin.nil? || arg.end.nil?)
55
+ raise ArgumentError, 'beginless and endless Ranges are not supported'
56
+ elsif arg.respond_to?(:each)
57
+ arg.each { |el| each_flattened(el, &block) }
58
+ else
59
+ block.call(arg)
60
+ end
61
+ end
43
62
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ['janosch84@gmail.com']
10
10
 
11
11
  s.summary = 'Compresses Arrays of Objects to Arrays of Ranges.'
12
- s.homepage = 'https://github.com/janosch-x/range_compressor'
12
+ s.homepage = 'https://github.com/jaynetics/range_compressor'
13
13
  s.license = 'MIT'
14
14
 
15
15
  s.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -19,7 +19,10 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.required_ruby_version = '>= 1.9.3'
21
21
 
22
- s.add_development_dependency 'bundler', '~> 1.16'
23
- s.add_development_dependency 'rake', '~> 10.0'
24
- s.add_development_dependency 'rspec', '~> 3.0'
22
+ s.add_development_dependency 'rake', '~> 13.0'
23
+ s.add_development_dependency 'rspec', '~> 3.8'
24
+ # SortedSet was moved to a gem in Ruby 3.
25
+ if RUBY_VERSION.to_f >= 3.0 && !RUBY_PLATFORM[/java/i]
26
+ s.add_development_dependency 'sorted_set', '~> 1.0'
27
+ end
25
28
  end
metadata CHANGED
@@ -1,67 +1,68 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: range_compressor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janosch Müller
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-06 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '13.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '13.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '3.8'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '3.8'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: sorted_set
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '1.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
55
- description:
54
+ version: '1.0'
55
+ description:
56
56
  email:
57
57
  - janosch84@gmail.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".github/workflows/tests.yml"
62
63
  - ".gitignore"
63
64
  - ".rspec"
64
- - ".travis.yml"
65
+ - CHANGELOG.md
65
66
  - Gemfile
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -71,11 +72,11 @@ files:
71
72
  - lib/range_compressor.rb
72
73
  - lib/range_compressor/version.rb
73
74
  - range_compressor.gemspec
74
- homepage: https://github.com/janosch-x/range_compressor
75
+ homepage: https://github.com/jaynetics/range_compressor
75
76
  licenses:
76
77
  - MIT
77
78
  metadata: {}
78
- post_install_message:
79
+ post_install_message:
79
80
  rdoc_options: []
80
81
  require_paths:
81
82
  - lib
@@ -90,9 +91,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  requirements: []
93
- rubyforge_project:
94
- rubygems_version: 2.7.6
95
- signing_key:
94
+ rubygems_version: 3.2.32
95
+ signing_key:
96
96
  specification_version: 4
97
97
  summary: Compresses Arrays of Objects to Arrays of Ranges.
98
98
  test_files: []
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.2
5
- - 2.6
6
- before_install:
7
- - gem update --system
8
- - gem install bundler