ranger
ranger
ranger
is a Crystal library to operate on Range
objects in Crystal programming language. Current version can include, exclude and two types of split Range
object.
First split you choose how many smaller ranges would you like, the second type allow you to specify range length. It is very useful for Time
related operations.
Installation
Add this to your application's shard.yml
:
dependencies:
ranger:
github: akwiatkowski/ranger
Usage
Include simple ranges
require "ranger"
result = (1..2) + (2..3)
puts result.inspect
# [1..3]
Keep in mind that you can only include two Range
objects. This is because result is Array(Range)
. If you include two exclusive Range
the result are two Range
objects. That means result of inclusion cannot be one Range
.
Include more than 2 ranges
In case you want to include more objects please use example below.
require "ranger"
r = Ranger.new(1..2)
r.include(2..3)
r.include(3..4)
result = r.render
puts result.inspect
# [1..4]
Exclude ranges
Exclusion is opposite operation to inclusion. This library allow you to do it.
require "ranger"
result = (1..10) - (5..6)
puts result.inspect
# [1..5, 6..10]
Exclude more ranges
require "ranger"
r = Ranger.new(10..20)
r.exclude(15..16)
r.exclude(18..19)
result = r.render
puts result.inspect
# [10..15, 16..18, 19..20]
Divide/split Range into more objects
You can use ranger
to split one Range
object into more smaller objects.
result = (0..10) / 2
puts result.inspect
# [0..5, 5..10]
Divide/split Range into specific ranges
If you have 1 month Time
Range
and would like to divide into 1-week Range
objects you can use modulo operator - %
.
ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra % j
Keep in mind last Range
object will be not full 1-week range. If you would like only full 1-week ranges you could change operator to bitwise xor - ^
. Sorry, there is lack of proper operators.
ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra ^ j
Alternatively there is method div
.
ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra.div(j, allow_partial: false)
Development
TODO: Write development instructions here
Contributing
- Fork it ( https://github.com/akwiatkowski/ranger/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- akwiatkowski Aleksander Kwiatkowski - creator, maintainer
ranger
- 2
- 1
- 0
- 0
- 0
- over 5 years ago
- June 10, 2016
MIT License
Sat, 21 Dec 2024 11:38:19 GMT