friendlyrange 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE.md +20 -0
- data/README.md +85 -0
- data/lib/friendlyrange.rb +69 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDAzYzIxMzU0Njg2MjgzNjg2ZGQxNWNlNzBjY2I1MWIxZWZlNDU3Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDNiMGUxMGQyODc1N2I4MTY0Yjk1N2E4MWMxOGE3OWE4NzdlMmYyZg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NmMwY2ViOGU5MWNhMDFhNjJhNzA3ZTJmNWVjNGQ5ZjRjYjc2OTI4YjllNGFm
|
10
|
+
ODgwNDE5MzU1MWQ5NDhiYTg4MzU1ZjQ1ZmZkMjk4Yjc3YmZiZmI5OWVkMmM1
|
11
|
+
MmMxY2VlNDAwY2Q4NjAwYjRkNWVlZThiYjc5ZTVmZTI4ZGE2NzM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWMyMTU2YjY3NTg1MzY0NmM4ZDVhZWQxMzczZTA0MDk3YmFlMWNjNzg5MWI3
|
14
|
+
ZWFlNmYzNGU3Y2E1ZjA0NTQ1YzQxYmFhYzI4NjNiOWMyNjQ5MDA1NTAyYjMz
|
15
|
+
MjE1MDU0NjU3Mzg5Y2YzNDQ1Y2IzMDkyYmNjYTBmY2MzYzgwYTQ=
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Tim Cheadle
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included
|
12
|
+
in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# FriendlyRange
|
2
|
+
|
3
|
+
FriendlyRange makes it easy to parse and compose ranges of numbers in a human friendly way.
|
4
|
+
|
5
|
+
It allows you to easily accept multiple ranges as input, like `1-4, 9,
|
6
|
+
15-19`. Just make sure ranges use hyphens and commas.
|
7
|
+
|
8
|
+
### Installation
|
9
|
+
|
10
|
+
Add to your `Gemfile`:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'friendlyrange'
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install manually:
|
17
|
+
|
18
|
+
```
|
19
|
+
$ gem install friendlyrange
|
20
|
+
```
|
21
|
+
|
22
|
+
### Usage
|
23
|
+
|
24
|
+
To parse a human-input range of numbers:
|
25
|
+
|
26
|
+
```
|
27
|
+
range = FriendlyRange.new('1-4, 9, 11-13')
|
28
|
+
```
|
29
|
+
|
30
|
+
Now just use the range as an array:
|
31
|
+
|
32
|
+
```
|
33
|
+
range.to_a
|
34
|
+
# => [1, 2, 3, 4, 9, 11, 12, 13]
|
35
|
+
```
|
36
|
+
|
37
|
+
Or, you can transform it back to a string:
|
38
|
+
|
39
|
+
```
|
40
|
+
range.to_s
|
41
|
+
# => "1-4, 9, 11-13"
|
42
|
+
```
|
43
|
+
|
44
|
+
### Fun Stuff
|
45
|
+
|
46
|
+
You can even take ranges in weird orders:
|
47
|
+
|
48
|
+
```
|
49
|
+
range = FriendlyRange.new('5-9, 1, 3')
|
50
|
+
|
51
|
+
range.to_a
|
52
|
+
# => [1, 3, 5, 6, 7, 8, 9]
|
53
|
+
|
54
|
+
range.to_s
|
55
|
+
# => "1, 3, 5-9"
|
56
|
+
```
|
57
|
+
|
58
|
+
It will even reconcile "bad" input, with overlapping numbers. It will
|
59
|
+
deduplicate any numbers automatically.
|
60
|
+
|
61
|
+
```
|
62
|
+
range = FriendlyRange.new('1, 1-4, 4')
|
63
|
+
|
64
|
+
range.to_a
|
65
|
+
# => [1, 2, 3, 4]
|
66
|
+
|
67
|
+
range.to_s
|
68
|
+
#=> "1-4"
|
69
|
+
```
|
70
|
+
|
71
|
+
It will also compensate for crap input. Human friendly!
|
72
|
+
|
73
|
+
```
|
74
|
+
range = FriendlyRange.new('1 - 4 , 8- 9 , 10')
|
75
|
+
|
76
|
+
range.to_a
|
77
|
+
# => [1, 2, 3, 4, 8, 9, 10]
|
78
|
+
|
79
|
+
range.to_s
|
80
|
+
# => "1-4, 8-10"
|
81
|
+
```
|
82
|
+
|
83
|
+
### Feedback
|
84
|
+
|
85
|
+
I'd love any feedback. Just file an issue or pull request. Please make sure any pull requests have tests for whatever you've changed.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
class FriendlyRange
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
def initialize(ranges_string)
|
5
|
+
@ranges_string = ranges_string
|
6
|
+
@ranges_array = parse(@ranges_string)
|
7
|
+
end
|
8
|
+
|
9
|
+
def each(&block)
|
10
|
+
@ranges_array.each(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def ==(other)
|
14
|
+
@ranges_array == other
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
array_to_string_of_ranges(@ranges_array)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def parse(ranges_string)
|
24
|
+
tmp_array = []
|
25
|
+
|
26
|
+
ranges_string.split(/\s*,\s*/).each do |item|
|
27
|
+
if item =~ /(\d+)\s*-\s*(\d+)/
|
28
|
+
first, last = $1.to_i, $2.to_i
|
29
|
+
first, last = last, first if first > last
|
30
|
+
first.upto(last) { |rangeitem| tmp_array << rangeitem }
|
31
|
+
elsif item =~ /^\s*(\d+)\s*$/
|
32
|
+
tmp_array << item.to_i
|
33
|
+
else
|
34
|
+
raise ArgumentError, "Cannot parse '#{ranges_string}'"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
tmp_array.sort.uniq
|
39
|
+
end
|
40
|
+
|
41
|
+
def array_to_string_of_ranges(ranges_array)
|
42
|
+
range_strings = []
|
43
|
+
range_start = range_end = ranges_array.sort[0]
|
44
|
+
|
45
|
+
ranges_array.drop(1).sort.each do |n|
|
46
|
+
if n == range_end + 1
|
47
|
+
# Number is sequential, extend the range
|
48
|
+
range_end = n
|
49
|
+
else
|
50
|
+
# Number is non-sequential, add the range
|
51
|
+
range_strings << range_string(range_start, range_end)
|
52
|
+
range_start = range_end = n
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Add the final range we found
|
57
|
+
range_strings << range_string(range_start, range_end)
|
58
|
+
|
59
|
+
range_strings.join(', ')
|
60
|
+
end
|
61
|
+
|
62
|
+
def range_string(range_start, range_end)
|
63
|
+
if range_start == range_end
|
64
|
+
range_start.to_s
|
65
|
+
else
|
66
|
+
"#{range_start}-#{range_end}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendlyrange
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Cheadle
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.13.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.13.0
|
27
|
+
description: FriendlyRange makes it easy to parse and compose ranges of numbers in
|
28
|
+
a human friendly way.
|
29
|
+
email:
|
30
|
+
- tim@rationalmeans.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/friendlyrange.rb
|
36
|
+
- LICENSE.md
|
37
|
+
- README.md
|
38
|
+
homepage: http://github.com/timcheadle/friendlyrange
|
39
|
+
licenses: []
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.6
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project: friendlyrange
|
57
|
+
rubygems_version: 2.0.3
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: An easy way to parse and compose human friendly number ranges
|
61
|
+
test_files: []
|
62
|
+
has_rdoc:
|