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 +4 -4
- data/.github/workflows/tests.yml +22 -0
- data/CHANGELOG.md +19 -0
- data/README.md +8 -11
- data/lib/range_compressor/version.rb +1 -1
- data/lib/range_compressor.rb +34 -15
- data/range_compressor.gemspec +7 -4
- metadata +19 -19
- data/.travis.yml +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 745781dd4c3d89b99798e84fc67df0836fef43b16681fc9230e22da77663837d
|
4
|
+
data.tar.gz: ae73d9c9041ee39ea49a54a96be950ce6e95b9f3c9dad0b3872b7dd7bd2587d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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://
|
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
|
6
|
+
A micro-gem that compresses enumerables into Ranges.
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
|
data/lib/range_compressor.rb
CHANGED
@@ -3,27 +3,15 @@ require 'range_compressor/version'
|
|
3
3
|
module RangeCompressor
|
4
4
|
module_function
|
5
5
|
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
data/range_compressor.gemspec
CHANGED
@@ -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/
|
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 '
|
23
|
-
s.add_development_dependency '
|
24
|
-
|
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.
|
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:
|
11
|
+
date: 2021-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
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: '
|
26
|
+
version: '13.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
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: '
|
40
|
+
version: '3.8'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: sorted_set
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
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: '
|
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
|
-
-
|
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/
|
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
|
-
|
94
|
-
|
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: []
|