range_compressor 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 83e62be00199f227f3dae78b086d5f63759205cfa19c5eeb255bf5787b33450b
4
+ data.tar.gz: f62233009ec12d166fb2072897e8ecc95336ec6df6745facaa7b7cb530d9c2f7
5
+ SHA512:
6
+ metadata.gz: 4ca724e589d5827ce79c89556acf4b97d8984f65d8c417f66b227a64b82d5e8b1aa3e20d372818214146ef750d8f445ac731136d1cbc1862de7c3152b29af1a7
7
+ data.tar.gz: 178feded786fb56dbcd52574a4dca4cae421ef96f37318d3c778206d83849aa9b1d221fb91750c466945eb05fe21342ad381322e3656a8b427a8831f7c11998e
@@ -0,0 +1,31 @@
1
+ *.bundle
2
+ *.gem
3
+ *.iml
4
+ *.stTheme.cache
5
+ *.sublime-project
6
+ *.sublime-workspace
7
+ *.swp
8
+ *.tmlanguage.cache
9
+ *.tmPreferences.cache
10
+ *~
11
+ .byebug_history
12
+ .DS_Store
13
+ .idea/
14
+ .ruby-gemset
15
+ .ruby-version
16
+ .tags
17
+ .tags1
18
+ bbin/
19
+ binstubs/*
20
+ bundler_stubs/*/.yardoc
21
+ Gemfile.lock
22
+ /.bundle/
23
+ /_yardoc/
24
+ /coverage/
25
+ /doc/
26
+ /pkg/
27
+ /spec/reports/
28
+ /tmp/
29
+
30
+ # rspec failure tracking
31
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,8 @@
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
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in range_compressor.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jannosch Müller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ # RangeCompressor
2
+
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)
5
+
6
+ A micro-gem that turns this:
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
+ ```
17
+
18
+ ## Installation
19
+
20
+ `gem install range_compressor`
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ ranges = RangeCompressor.compress(some_enumerable)
26
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'range_compressor'
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ require 'range_compressor/version'
2
+
3
+ module RangeCompressor
4
+ module_function
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
20
+
21
+ ranges = []
22
+ previous = nil
23
+ current_start = nil
24
+ current_end = nil
25
+
26
+ enum.each do |object|
27
+ if previous.nil?
28
+ current_start = object
29
+ elsif previous.next != object
30
+ # gap found, finalize previous range
31
+ ranges << (current_start..current_end)
32
+ current_start = object
33
+ end
34
+ current_end = object
35
+ previous = object
36
+ end
37
+
38
+ # add final range
39
+ ranges << (current_start..current_end) if current_start
40
+
41
+ ranges
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module RangeCompressor
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'range_compressor/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'range_compressor'
7
+ s.version = RangeCompressor::VERSION
8
+ s.authors = ['Janosch Müller']
9
+ s.email = ['janosch84@gmail.com']
10
+
11
+ s.summary = 'Compresses Arrays of Objects to Arrays of Ranges.'
12
+ s.homepage = 'https://github.com/janosch-x/range_compressor'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ s.require_paths = ['lib']
19
+
20
+ s.required_ruby_version = '>= 1.9.3'
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'
25
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range_compressor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Janosch Müller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - janosch84@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/range_compressor.rb
72
+ - lib/range_compressor/version.rb
73
+ - range_compressor.gemspec
74
+ homepage: https://github.com/janosch-x/range_compressor
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.9.3
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Compresses Arrays of Objects to Arrays of Ranges.
98
+ test_files: []