semantic_range 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ec6ed6422a8b1100e9b8fa2f556a6353150b8c5
4
+ data.tar.gz: 4ce568684c4f9445a11b84da2ad46bf5641eae43
5
+ SHA512:
6
+ metadata.gz: 6d98c96a9411e9ee4887f16ae2d856668cc19f3c296a2316380e60c4e51236afdf3b20a4271a30cfdbd2184dce0f891ab3c4871d58a82a38b0a54d43a90bef7d
7
+ data.tar.gz: 9d0d40922cc914d970daa88f7e89e1977d0fe65e83f6872491b31ee80c21324b89856265078690ed195d7e35a895837103b96f73cbf7464ebcfcb736065b96a1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in semantic_range.gemspec
4
+ gemspec
5
+ gem 'guard-rspec'
data/Guardfile ADDED
@@ -0,0 +1,87 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :bundler do
19
+ require 'guard/bundler'
20
+ require 'guard/bundler/verify'
21
+ helper = Guard::Bundler::Verify.new
22
+
23
+ files = ['Gemfile']
24
+ files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
25
+
26
+ # Assume files are symlinked from somewhere
27
+ files.each { |file| watch(helper.real_path(file)) }
28
+ end
29
+
30
+ guard 'migrate' do
31
+ watch(%r{^db/migrate/(\d+).+\.rb})
32
+ watch('db/seeds.rb')
33
+ end
34
+
35
+ # Note: The cmd option is now required due to the increasing number of ways
36
+ # rspec may be run, below are examples of the most common uses.
37
+ # * bundler: 'bundle exec rspec'
38
+ # * bundler binstubs: 'bin/rspec'
39
+ # * spring: 'bin/rspec' (This will use spring if running and you have
40
+ # installed the spring binstubs per the docs)
41
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
42
+ # * 'just' rspec: 'rspec'
43
+
44
+ guard :rspec, cmd: "bundle exec rspec" do
45
+ require "guard/rspec/dsl"
46
+ dsl = Guard::RSpec::Dsl.new(self)
47
+
48
+ # Feel free to open issues for suggestions and improvements
49
+
50
+ # RSpec files
51
+ rspec = dsl.rspec
52
+ watch(rspec.spec_helper) { rspec.spec_dir }
53
+ watch(rspec.spec_support) { rspec.spec_dir }
54
+ watch(rspec.spec_files)
55
+
56
+ # Ruby files
57
+ ruby = dsl.ruby
58
+ dsl.watch_spec_files_for(ruby.lib_files)
59
+
60
+ # Rails files
61
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
62
+ dsl.watch_spec_files_for(rails.app_files)
63
+ dsl.watch_spec_files_for(rails.views)
64
+
65
+ watch(rails.controllers) do |m|
66
+ [
67
+ rspec.spec.("routing/#{m[1]}_routing"),
68
+ rspec.spec.("controllers/#{m[1]}_controller"),
69
+ rspec.spec.("acceptance/#{m[1]}")
70
+ ]
71
+ end
72
+
73
+ # Rails config changes
74
+ watch(rails.spec_helper) { rspec.spec_dir }
75
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
76
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
77
+
78
+ # Capybara features specs
79
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
80
+ watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
81
+
82
+ # Turnip features and steps
83
+ watch(%r{^spec/acceptance/(.+)\.feature$})
84
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
85
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
86
+ end
87
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Nesbitt
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # SemanticRange
2
+
3
+ [node-semver](https://github.com/npm/node-semver) written in Ruby for comparison and inclusion of semantic versions and ranges.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'semantic_range'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install semantic_range
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ SemanticRange.valid('1.2.3') # '1.2.3'
25
+ SemanticRange.valid('a.b.c') # nil
26
+ SemanticRange.clean(' =v1.2.3 ') # '1.2.3'
27
+ SemanticRange.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') # true
28
+ SemanticRange.gt('1.2.3', '9.8.7') # false
29
+ SemanticRange.lt('1.2.3', '9.8.7') # true
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/librariesio/semantic_range. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "semantic_range"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,233 @@
1
+ require "semantic_range/version"
2
+ require "semantic_range/range"
3
+ require "semantic_range/comparator"
4
+
5
+ module SemanticRange
6
+ BUILDIDENTIFIER = /[0-9A-Za-z-]+/
7
+ BUILD = /(?:\+(#{BUILDIDENTIFIER.source}(?:\.#{BUILDIDENTIFIER.source})*))/
8
+ NUMERICIDENTIFIER = /0|[1-9]\d*/
9
+ NUMERICIDENTIFIERLOOSE = /[0-9]+/
10
+ NONNUMERICIDENTIFIER = /\d*[a-zA-Z-][a-zA-Z0-9-]*/
11
+ XRANGEIDENTIFIERLOOSE = /#{NUMERICIDENTIFIERLOOSE.source}|x|X|\*/
12
+ PRERELEASEIDENTIFIERLOOSE = /(?:#{NUMERICIDENTIFIERLOOSE.source}|#{NONNUMERICIDENTIFIER.source})/
13
+ PRERELEASELOOSE = /(?:-?(#{PRERELEASEIDENTIFIERLOOSE.source}(?:\.#{PRERELEASEIDENTIFIERLOOSE.source})*))/
14
+ XRANGEPLAINLOOSE = /[v=\s]*(#{XRANGEIDENTIFIERLOOSE.source})(?:\.(#{XRANGEIDENTIFIERLOOSE.source})(?:\.(#{XRANGEIDENTIFIERLOOSE.source})(?:#{PRERELEASELOOSE.source})?#{BUILD.source}?)?)?/
15
+ HYPHENRANGELOOSE = /^\s*(#{XRANGEPLAINLOOSE.source})\s+-\s+(#{XRANGEPLAINLOOSE.source})\s*$/
16
+ PRERELEASEIDENTIFIER = /(?:#{NUMERICIDENTIFIER.source}|#{NONNUMERICIDENTIFIER.source})/
17
+ PRERELEASE = /(?:-(#{PRERELEASEIDENTIFIER.source}(?:\.#{PRERELEASEIDENTIFIER.source})*))/
18
+ XRANGEIDENTIFIER = /#{NUMERICIDENTIFIER.source}|x|X|\*/
19
+ XRANGEPLAIN = /[v=\s]*(#{XRANGEIDENTIFIER.source})(?:\.(#{XRANGEIDENTIFIER.source})(?:\.(#{XRANGEIDENTIFIER.source})(?:#{PRERELEASE.source})?#{BUILD.source}?)?)?/
20
+ HYPHENRANGE = /^\s*(#{XRANGEPLAIN.source})\s+-\s+(#{XRANGEPLAIN.source})\s*$/
21
+ MAINVERSIONLOOSE = /(#{NUMERICIDENTIFIERLOOSE.source})\.(#{NUMERICIDENTIFIERLOOSE.source})\.(#{NUMERICIDENTIFIERLOOSE.source})/
22
+ LOOSEPLAIN = /[v=\s]*#{MAINVERSIONLOOSE.source}#{PRERELEASELOOSE.source}?#{BUILD.source}?/
23
+ GTLT = /((?:<|>)?=?)/
24
+ COMPARATORTRIM = /(\s*)#{GTLT.source}\s*(#{LOOSEPLAIN.source}|#{XRANGEPLAIN.source})/
25
+ LONETILDE = /(?:~>?)/
26
+ TILDETRIM = /(\s*)#{LONETILDE.source}\s+/
27
+ LONECARET = /(?:\^)/
28
+ CARETTRIM = /(\s*)#{LONECARET.source}\s+/
29
+ STAR = /(<|>)?=?\s*\*/
30
+ CARET = /^#{LONECARET.source}#{XRANGEPLAIN.source}$/
31
+ CARETLOOSE = /^#{LONECARET.source}#{XRANGEPLAINLOOSE.source}$/
32
+ MAINVERSION = /(#{NUMERICIDENTIFIER.source})\.(#{NUMERICIDENTIFIER.source})\.(#{NUMERICIDENTIFIER.source})/
33
+ FULLPLAIN = /v?#{MAINVERSION.source}#{PRERELEASE.source}?#{BUILD.source}?/
34
+ FULL = /^#{FULLPLAIN.source}$/
35
+ LOOSE = /^#{LOOSEPLAIN.source}$/
36
+ TILDE = /^#{LONETILDE.source}#{XRANGEPLAIN.source}$/
37
+ TILDELOOSE = /^#{LONETILDE.source}#{XRANGEPLAINLOOSE.source}$/
38
+ XRANGE = /^#{GTLT.source}\s*#{XRANGEPLAIN.source}$/
39
+ XRANGELOOSE = /^#{GTLT.source}\s*#{XRANGEPLAINLOOSE.source}$/
40
+ COMPARATOR = /^#{GTLT.source}\s*(#{FULLPLAIN.source})$|^$/
41
+ COMPARATORLOOSE = /^#{GTLT.source}\s*(#{LOOSEPLAIN.source})$|^$/
42
+
43
+ ANY = {}
44
+
45
+ MAX_LENGTH = 256
46
+
47
+ def self.ltr(version, range, loose = false)
48
+ outside(version, range, '<', loose)
49
+ end
50
+
51
+ def self.gtr(version, range, loose = false)
52
+ outside(version, range, '>', loose)
53
+ end
54
+
55
+ def self.cmp(a, op, b, loose = false)
56
+ case op
57
+ when '==='
58
+ a = a.version if !a.is_a?(String)
59
+ b = b.version if !b.is_a?(String)
60
+ a == b
61
+ when '!=='
62
+ a = a.version if !a.is_a?(String)
63
+ b = b.version if !b.is_a?(String)
64
+ a != b
65
+ when '', '=', '=='
66
+ eq(a, b, loose)
67
+ when '!='
68
+ neq(a, b, loose)
69
+ when '>'
70
+ gt(a, b, loose)
71
+ when '>='
72
+ gte(a, b, loose)
73
+ when '<'
74
+ lt(a, b, loose)
75
+ when '<='
76
+ lte(a, b, loose)
77
+ else
78
+ raise 'Invalid operator: ' + op
79
+ end
80
+ end
81
+
82
+ def self.outside(version, range, hilo, loose = false)
83
+ version = Version.new(version, loose)
84
+ range = Range.new(range, loose)
85
+
86
+ return false if satisfies(version, range, loose)
87
+
88
+ case hilo
89
+ when '>'
90
+ comp = '>'
91
+ ecomp = '>='
92
+ when '<'
93
+ comp = '<'
94
+ ecomp = '<='
95
+ end
96
+
97
+ range.set.each do |comparators|
98
+ high = nil
99
+ low = nil
100
+
101
+ comparators.each do |comparator|
102
+ if comparator.semver == ANY
103
+ comparator = Comparator.new('>=0.0.0', loose)
104
+ end
105
+
106
+ high = high || comparator
107
+ low = low || comparator
108
+
109
+ case hilo
110
+ when '>'
111
+ if gt(comparator.semver, high.semver, loose)
112
+ high = comparator
113
+ elsif lt(comparator.semver, low.semver, loose)
114
+ low = comparator
115
+ end
116
+ when '<'
117
+ if lt(comparator.semver, high.semver, loose)
118
+ high = comparator
119
+ elsif gt(comparator.semver, low.semver, loose)
120
+ low = comparator
121
+ end
122
+ end
123
+ end
124
+
125
+ return false if (high.operator == comp || high.operator == ecomp)
126
+
127
+ case hilo
128
+ when '>'
129
+ if (low.operator.empty? || low.operator == comp) && lte(version, low.semver, loose)
130
+ return false;
131
+ elsif (low.operator == ecomp && lt(version, low.semver, loose))
132
+ return false;
133
+ end
134
+ when '<'
135
+ if (low.operator.empty? || low.operator == comp) && gte(version, low.semver, loose)
136
+ return false;
137
+ elsif low.operator == ecomp && gt(version, low.semver, loose)
138
+ return false;
139
+ end
140
+ end
141
+ end
142
+ true
143
+ end
144
+
145
+ def self.satisfies(version, range, loose = false)
146
+ Range.new(range, loose).test(version)
147
+ end
148
+
149
+ def self.max_satisfying(version, range, loose = false)
150
+ # TODO
151
+ end
152
+
153
+ def self.valid_range(range, loose = false)
154
+ begin
155
+ Range.new(range, loose).range || '*'
156
+ rescue
157
+ nil
158
+ end
159
+ end
160
+
161
+ def self.compare(a, b, loose = false)
162
+ Version.new(a, loose).compare(b)
163
+ end
164
+
165
+ def self.compare_loose(a, b)
166
+ compare(a, b, true)
167
+ end
168
+
169
+ def self.rcompare(a, b, loose = false)
170
+ compare(b, a, true)
171
+ end
172
+
173
+ def self.sort(list, loose = false)
174
+ # TODO
175
+ end
176
+
177
+ def self.rsort(list, loose = false)
178
+ # TODO
179
+ end
180
+
181
+ def self.lt(a, b, loose = false)
182
+ compare(a, b, loose) < 0
183
+ end
184
+
185
+ def self.gt(a, b, loose = false)
186
+ compare(a, b, loose) > 0
187
+ end
188
+
189
+ def self.eq(a, b, loose = false)
190
+ compare(a, b, loose) == 0
191
+ end
192
+
193
+ def self.neq(a, b, loose = false)
194
+ compare(a, b, loose) != 0
195
+ end
196
+
197
+ def self.gte(a, b, loose = false)
198
+ compare(a, b, loose) >= 0
199
+ end
200
+
201
+ def self.lte(a, b, loose = false)
202
+ compare(a, b, loose) <= 0
203
+ end
204
+
205
+ def self.valid(version, loose = false)
206
+ v = parse(version, loose)
207
+ return v ? v.version : nil
208
+ end
209
+
210
+ def self.clean(version, loose = false)
211
+ s = parse(version.strip.gsub(/^[=v]+/, ''), loose)
212
+ return s ? s.version : nil
213
+ end
214
+
215
+ def self.parse(version, loose = false)
216
+ return version if version.is_a?(Version)
217
+
218
+ return nil unless version.is_a?(String)
219
+
220
+ version.strip!
221
+
222
+ return nil if version.length > MAX_LENGTH
223
+
224
+ rxp = loose ? LOOSE : FULL
225
+ return nil if !rxp.match(version)
226
+
227
+ begin
228
+ Version.new(version, loose)
229
+ rescue
230
+ nil
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,59 @@
1
+ module SemanticRange
2
+ class Comparator
3
+ def initialize(comp, loose)
4
+ @loose = loose
5
+
6
+ if comp.is_a?(Comparator)
7
+ if comp.loose == loose
8
+ return comp
9
+ else
10
+ @comp = comp.value
11
+ end
12
+ end
13
+
14
+ parse(comp)
15
+
16
+ if @semver == ANY
17
+ @value = ''
18
+ else
19
+ @value = @operator + @semver.version
20
+ end
21
+ end
22
+
23
+ def semver
24
+ @semver
25
+ end
26
+
27
+ def operator
28
+ @operator
29
+ end
30
+
31
+ def to_s
32
+ @value
33
+ end
34
+
35
+ def value
36
+ @value
37
+ end
38
+
39
+ def test(version)
40
+ return true if @semver == ANY
41
+ version = Version.new(version, @loose) if version.is_a?(String)
42
+ SemanticRange.cmp(version, @operator, @semver, @loose)
43
+ end
44
+
45
+ def parse(comp)
46
+ m = comp.match(@loose ? COMPARATORLOOSE : COMPARATOR)
47
+ raise 'Invalid comparator: ' + comp unless m
48
+
49
+ @operator = m[1]
50
+ @operator = '' if @operator == '='
51
+
52
+ if !m[2]
53
+ @semver = ANY
54
+ else
55
+ @semver = Version.new(m[2], @loose)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,291 @@
1
+ module SemanticRange
2
+ class Range
3
+ def initialize(range, loose)
4
+ range = range.raw if range.is_a?(Range)
5
+
6
+ @raw = range
7
+ @loose = loose
8
+ split = range.split(/\s*\|\|\s*/)
9
+ split = [''] if split == []
10
+ @set = split.map do |range|
11
+ parse_range(range.strip)
12
+ end
13
+
14
+ raise 'Invalid SemVer Range: ' + range if @set.empty?
15
+
16
+ format
17
+ end
18
+
19
+ def loose
20
+ @loose
21
+ end
22
+
23
+ def raw
24
+ @raw
25
+ end
26
+
27
+ def format
28
+ @range = @set.map do |comps|
29
+ comps.join(' ').strip
30
+ end.join('||').strip
31
+ @range
32
+ end
33
+
34
+ def set
35
+ @set
36
+ end
37
+
38
+ def test(version)
39
+ return false if !version
40
+ version = Version.new(version, @loose) if version.is_a?(String)
41
+ @set.each do |s|
42
+ return true if test_set(s, version)
43
+ end
44
+ false
45
+ end
46
+
47
+ def test_set(set, version)
48
+ set.each do |comp|
49
+ return false if !comp.test(version)
50
+ end
51
+ if version.prerelease.length > 0
52
+ set.each do |comp|
53
+ next if comp.semver == ANY
54
+
55
+ if comp.semver.prerelease.length > 0
56
+ allowed = comp.semver
57
+ return true if allowed.major == version.major && allowed.minor == version.minor && allowed.patch == version.patch
58
+ end
59
+ end
60
+ return false
61
+ end
62
+ return true
63
+ end
64
+
65
+ def parse_range(range)
66
+ # expand hyphens
67
+ range = range.gsub(@loose ? HYPHENRANGELOOSE : HYPHENRANGE){ hyphen_replace(Regexp.last_match) }
68
+
69
+ # comparator trim
70
+ range = range.gsub(COMPARATORTRIM, '\1\2\3')
71
+
72
+ # tilde trim
73
+ range = range.gsub(TILDETRIM, '\1~')
74
+
75
+ # caret trim
76
+ range = range.gsub(CARETTRIM, '\1^')
77
+
78
+ # normalise spaces
79
+ range = range.split(/\s+/).join(' ')
80
+
81
+ set = range.split(' ').map do |comp|
82
+ parse_comparator(comp, @loose)
83
+ end.join(' ').split(/\s+/)
84
+ set = [''] if set == []
85
+
86
+ set = set.select{|comp| !!comp.match(COMPARATORLOOSE) } if @loose
87
+
88
+ set.map{|comp| Comparator.new(comp, @loose) }
89
+ end
90
+
91
+ def isX(id)
92
+ !id || id.downcase == 'x' || id == '*'
93
+ end
94
+
95
+ def parse_comparator(comp, loose)
96
+ comp = replace_carets(comp, loose)
97
+ comp = replace_tildes(comp, loose)
98
+ comp = replace_x_ranges(comp, loose)
99
+ replace_stars(comp, loose)
100
+ end
101
+
102
+ def replace_carets(comp, loose)
103
+ comp.strip.split(/\s+/).map do |comp|
104
+ replace_caret(comp, loose)
105
+ end.join(' ')
106
+ end
107
+
108
+ def replace_caret(comp, loose)
109
+ comp.gsub(loose ? CARETLOOSE : CARET) do
110
+ match = Regexp.last_match
111
+ mj = match[1]
112
+ m = match[2]
113
+ p = match[3]
114
+ pr = match[4]
115
+
116
+ if isX(mj)
117
+ ret = ''
118
+ elsif isX(m)
119
+ ret = ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0"
120
+ elsif isX(p)
121
+ if mj == '0'
122
+ ret = ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0"
123
+ else
124
+ ret = ">=#{mj}.#{m}.0 <#{(mj.to_i + 1)}.0.0"
125
+ end
126
+ elsif pr
127
+ if pr[0] != '-'
128
+ pr = "-#{pr}"
129
+ end
130
+ if mj == '0'
131
+ if m == '0'
132
+ ret = ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{m}.#{(p.to_i + 1)}"
133
+ else
134
+ ret = ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{(m.to_i + 1)}.0"
135
+ end
136
+ else
137
+ ret = ">=#{mj}.#{m}.#{p}#{pr} <#{(mj.to_i + 1)}.0.0"
138
+ end
139
+ else
140
+ if mj == '0'
141
+ if m == '0'
142
+ ret = ">=#{mj}.#{m}.#{p} <#{mj}.#{m}.#{(p.to_i + 1)}"
143
+ else
144
+ ret = ">=#{mj}.#{m}.#{p} <#{mj}.#{(m.to_i + 1)}.0"
145
+ end
146
+ else
147
+ ret = ">=#{mj}.#{m}.#{p} <#{(mj.to_i + 1)}.0.0"
148
+ end
149
+ end
150
+ ret
151
+ end
152
+ end
153
+
154
+ def replace_tildes(comp, loose)
155
+ comp.strip.split(/\s+/).map do |comp|
156
+ replace_tilde(comp, loose)
157
+ end.join(' ')
158
+ end
159
+
160
+ def replace_tilde(comp, loose)
161
+ comp.gsub(loose ? TILDELOOSE : TILDE) do
162
+ match = Regexp.last_match
163
+ mj = match[1]
164
+ m = match[2]
165
+ p = match[3]
166
+ pr = match[4]
167
+
168
+ if isX(mj)
169
+ ret = ''
170
+ elsif isX(m)
171
+ ret = ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0"
172
+ elsif isX(p)
173
+ ret = ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0"
174
+ elsif pr
175
+ pr = '-' + pr if (pr[0] != '-')
176
+ ret = ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{(m.to_i + 1)}.0"
177
+ else
178
+ ret = ">=#{mj}.#{m}.#{p} <#{mj}.#{(m.to_i + 1)}.0"
179
+ end
180
+ ret
181
+ end
182
+ end
183
+
184
+ def replace_x_ranges(comp, loose)
185
+ comp.strip.split(/\s+/).map do |comp|
186
+ replace_x_range(comp, loose)
187
+ end.join(' ')
188
+ end
189
+
190
+ def replace_x_range(comp, loose)
191
+ comp = comp.strip
192
+ comp.gsub(loose ? XRANGELOOSE : XRANGE) do
193
+ match = Regexp.last_match
194
+ ret = match[0]
195
+ gtlt = match[1]
196
+ mj = match[2]
197
+ m = match[3]
198
+ p = match[4]
199
+ pr = match[5]
200
+
201
+ xM = isX(mj)
202
+ xm = xM || isX(m)
203
+ xp = xm || isX(p)
204
+ anyX = xp
205
+
206
+ gtlt = '' if gtlt == '=' && anyX
207
+
208
+ if xM
209
+ if gtlt == '>' || gtlt == '<'
210
+ ret = '<0.0.0'
211
+ else
212
+ ret = '*'
213
+ end
214
+ elsif !gtlt.nil? && gtlt != '' && anyX
215
+ m = 0 if xm
216
+ p = 0 if xp
217
+
218
+ if gtlt == '>'
219
+ gtlt = '>='
220
+ if xm
221
+ mj = mj.to_i + 1
222
+ m = 0
223
+ p = 0
224
+ elsif xp
225
+ m = m.to_i + 1
226
+ p = 0
227
+ end
228
+ elsif gtlt == '<='
229
+ gtlt = '<'
230
+ if xm
231
+ mj = mj.to_i + 1
232
+ else
233
+ m = m.to_i + 1
234
+ end
235
+ end
236
+
237
+ ret = "#{gtlt}#{mj}.#{m}.#{p}"
238
+ elsif xm
239
+ ret = ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0"
240
+ elsif xp
241
+ ret = ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0"
242
+ end
243
+
244
+ ret
245
+ end
246
+ end
247
+
248
+ def replace_stars(comp, loose)
249
+ comp.strip.gsub(STAR, '')
250
+ end
251
+
252
+ def hyphen_replace(match)
253
+ from = match[1]
254
+ fM = match[2]
255
+ fm = match[3]
256
+ fp = match[4]
257
+ fpr = match[5]
258
+ fb = match[6]
259
+ to = match[7]
260
+ tM = match[8]
261
+ tm = match[9]
262
+ tp = match[10]
263
+ tpr = match[11]
264
+ tb = match[12]
265
+
266
+ if isX(fM)
267
+ from = ''
268
+ elsif isX(fm)
269
+ from = ">=#{fM}.0.0"
270
+ elsif isX(fp)
271
+ from = ">=#{fM}.#{fm}.0"
272
+ else
273
+ from = ">=#{from}"
274
+ end
275
+
276
+ if isX(tM)
277
+ to = ''
278
+ elsif isX(tm)
279
+ to = "<#{(tM.to_i + 1)}.0.0"
280
+ elsif isX(tp)
281
+ to = "<#{tm}.#{(tm.to_i + 1)}.0"
282
+ elsif tpr
283
+ to = "<=#{tM}.#{tm}.#{tp}-#{tpr}"
284
+ else
285
+ to = "<=#{to}"
286
+ end
287
+
288
+ "#{from} #{to}".strip
289
+ end
290
+ end
291
+ end
@@ -0,0 +1,136 @@
1
+ module SemanticRange
2
+ VERSION = "0.1.0"
3
+
4
+ class Version
5
+ def initialize(version, loose)
6
+ @raw = version
7
+ @loose = loose
8
+
9
+ if version.is_a?(Version)
10
+ @raw = version = version.raw
11
+ end
12
+
13
+ match = version.strip.match(loose ? LOOSE : FULL)
14
+ # TODO error handling
15
+
16
+ @major = match[1] ? match[1].to_i : 0
17
+ @minor = match[2] ? match[2].to_i : 0
18
+ @patch = match[3] ? match[3].to_i : 0
19
+
20
+ # TODO error handling
21
+
22
+ if !match[4]
23
+ @prerelease = []
24
+ else
25
+ @prerelease = match[4].split('.').map do |id|
26
+ if /^[0-9]+$/.match(id)
27
+ num = id.to_i
28
+ # TODO error handling
29
+ else
30
+ id
31
+ end
32
+ end
33
+ end
34
+
35
+ @build = match[5] ? match[5].split('.') : []
36
+ @version = format
37
+ end
38
+
39
+ def version
40
+ @version
41
+ end
42
+
43
+ def raw
44
+ @raw
45
+ end
46
+
47
+ def major
48
+ @major
49
+ end
50
+
51
+ def minor
52
+ @minor
53
+ end
54
+
55
+ def patch
56
+ @patch
57
+ end
58
+
59
+ def prerelease
60
+ @prerelease
61
+ end
62
+
63
+ def format
64
+ v = "#{@major}.#{@minor}.#{@patch}"
65
+ if @prerelease.length > 0
66
+ v += '-' + @prerelease.join('.')
67
+ end
68
+ v
69
+ end
70
+
71
+ def to_s
72
+ @version
73
+ end
74
+
75
+ def compare(other)
76
+ other = Version.new(other, @loose) unless other.is_a?(Version)
77
+ res = truthy(compare_main(other)) || truthy(compare_pre(other))
78
+ res.is_a?(FalseClass) ? 0 : res
79
+ end
80
+
81
+ def compare_main(other)
82
+ other = Version.new(other, @loose) unless other.is_a?(Version)
83
+ truthy(compare_identifiers(@major, other.major)) || truthy(compare_identifiers(@minor, other.minor)) || truthy(compare_identifiers(@patch, other.patch))
84
+ end
85
+
86
+ def truthy(val)
87
+ return val unless val.is_a?(Integer)
88
+ val.zero? ? false : val
89
+ end
90
+
91
+ def compare_pre(other)
92
+ other = Version.new(other, @loose) unless other.is_a?(Version)
93
+
94
+ return -1 if truthy(@prerelease.length) && !truthy(other.prerelease.length)
95
+
96
+ return 1 if !truthy(@prerelease.length) && truthy(other.prerelease.length)
97
+
98
+ return 0 if !truthy(@prerelease.length) && !truthy(other.prerelease.length)
99
+
100
+ i = 0
101
+ while true
102
+ a = @prerelease[i]
103
+ b = other.prerelease[i]
104
+
105
+ if a.nil? && b.nil?
106
+ return 0
107
+ elsif b.nil?
108
+ return 1
109
+ elsif a.nil?
110
+ return -1
111
+ elsif a == b
112
+
113
+ else
114
+ return compare_identifiers(a, b)
115
+ end
116
+ i += 1
117
+ end
118
+ end
119
+
120
+ def compare_identifiers(a,b)
121
+ anum = /^[0-9]+$/.match(a.to_s)
122
+ bnum = /^[0-9]+$/.match(b.to_s)
123
+
124
+ if anum && bnum
125
+ a = a.to_i
126
+ b = b.to_i
127
+ end
128
+
129
+ return (anum && !bnum) ? -1 :
130
+ (bnum && !anum) ? 1 :
131
+ a < b ? -1 :
132
+ a > b ? 1 :
133
+ 0;
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'semantic_range/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "semantic_range"
8
+ spec.version = SemanticRange::VERSION
9
+ spec.authors = ["Andrew Nesbitt"]
10
+ spec.email = ["andrewnez@gmail.com"]
11
+
12
+ spec.summary = %q{node-semver rewritten in ruby, for comparison and inclusion of semantic versions and rangee}
13
+ spec.homepage = "https://libraries.io/github/librariesio/semantic_range"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Nesbitt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-13 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - andrewnez@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Guardfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/semantic_range.rb
74
+ - lib/semantic_range/comparator.rb
75
+ - lib/semantic_range/range.rb
76
+ - lib/semantic_range/version.rb
77
+ - semantic_range.gemspec
78
+ homepage: https://libraries.io/github/librariesio/semantic_range
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: node-semver rewritten in ruby, for comparison and inclusion of semantic versions
102
+ and rangee
103
+ test_files: []
104
+ has_rdoc: