Module: SemanticRange

Defined in:
lib/semantic_range.rb,
lib/semantic_range/range.rb,
lib/semantic_range/version.rb,
lib/semantic_range/comparator.rb,
lib/semantic_range/pre_release.rb

Defined Under Namespace

Classes: Comparator, InvalidIncrement, NoMatchFound, PreRelease, Range, Version

Constant Summary collapse

BUILDIDENTIFIER =
/[0-9A-Za-z-]+/
BUILD =
/(?:\+(#{BUILDIDENTIFIER.source}(?:\.#{BUILDIDENTIFIER.source})*))/
NUMERICIDENTIFIER =
/0|[1-9]\d*/
NUMERICIDENTIFIERLOOSE =
/[0-9]+/
NONNUMERICIDENTIFIER =
/\d*[a-zA-Z-][a-zA-Z0-9-]*/
XRANGEIDENTIFIERLOOSE =
/#{NUMERICIDENTIFIERLOOSE.source}|x|X|\*/
PRERELEASEIDENTIFIERLOOSE =
/(?:#{NUMERICIDENTIFIERLOOSE.source}|#{NONNUMERICIDENTIFIER.source})/
PRERELEASELOOSE =
/(?:-?(#{PRERELEASEIDENTIFIERLOOSE.source}(?:\.#{PRERELEASEIDENTIFIERLOOSE.source})*))/
XRANGEPLAINLOOSE =
/[v=\s]*(#{XRANGEIDENTIFIERLOOSE.source})(?:\.(#{XRANGEIDENTIFIERLOOSE.source})(?:\.(#{XRANGEIDENTIFIERLOOSE.source})(?:#{PRERELEASELOOSE.source})?#{BUILD.source}?)?)?/
HYPHENRANGELOOSE =
/^\s*(#{XRANGEPLAINLOOSE.source})\s+-\s+(#{XRANGEPLAINLOOSE.source})\s*$/
PRERELEASEIDENTIFIER =
/(?:#{NUMERICIDENTIFIER.source}|#{NONNUMERICIDENTIFIER.source})/
PRERELEASE =
/(?:-(#{PRERELEASEIDENTIFIER.source}(?:\.#{PRERELEASEIDENTIFIER.source})*))/
XRANGEIDENTIFIER =
/#{NUMERICIDENTIFIER.source}|x|X|\*/
XRANGEPLAIN =
/[v=\s]*(#{XRANGEIDENTIFIER.source})(?:\.(#{XRANGEIDENTIFIER.source})(?:\.(#{XRANGEIDENTIFIER.source})(?:#{PRERELEASE.source})?#{BUILD.source}?)?)?/
HYPHENRANGE =
/^\s*(#{XRANGEPLAIN.source})\s+-\s+(#{XRANGEPLAIN.source})\s*$/
MAINVERSIONLOOSE =
/(#{NUMERICIDENTIFIERLOOSE.source})\.(#{NUMERICIDENTIFIERLOOSE.source})\.(#{NUMERICIDENTIFIERLOOSE.source})/
LOOSEPLAIN =
/[v=\s]*#{MAINVERSIONLOOSE.source}#{PRERELEASELOOSE.source}?#{BUILD.source}?/
GTLT =
/((?:<|>)?=?)/
COMPARATORTRIM =
/(\s*)#{GTLT.source}\s*(#{LOOSEPLAIN.source}|#{XRANGEPLAIN.source})/
LONETILDE =
/(?:~>?)/
TILDETRIM =
/(\s*)#{LONETILDE.source}\s+/
LONECARET =
/(?:\^)/
CARETTRIM =
/(\s*)#{LONECARET.source}\s+/
STAR =
/(<|>)?=?\s*\*/
CARET =
/^#{LONECARET.source}#{XRANGEPLAIN.source}$/
CARETLOOSE =
/^#{LONECARET.source}#{XRANGEPLAINLOOSE.source}$/
MAINVERSION =
/(#{NUMERICIDENTIFIER.source})\.(#{NUMERICIDENTIFIER.source})\.(#{NUMERICIDENTIFIER.source})/
FULLPLAIN =
/v?#{MAINVERSION.source}#{PRERELEASE.source}?#{BUILD.source}?/
FULL =
/^#{FULLPLAIN.source}$/
LOOSE =
/^#{LOOSEPLAIN.source}$/
TILDE =
/^#{LONETILDE.source}#{XRANGEPLAIN.source}$/
TILDELOOSE =
/^#{LONETILDE.source}#{XRANGEPLAINLOOSE.source}$/
XRANGE =
/^#{GTLT.source}\s*#{XRANGEPLAIN.source}$/
XRANGELOOSE =
/^#{GTLT.source}\s*#{XRANGEPLAINLOOSE.source}$/
COMPARATOR =
/^#{GTLT.source}\s*(#{FULLPLAIN.source})$|^$/
COMPARATORLOOSE =
/^#{GTLT.source}\s*(#{LOOSEPLAIN.source})$|^$/
ANY =
{}
MAX_LENGTH =
256
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.clean(version, loose = false) ⇒ Object



217
218
219
220
# File 'lib/semantic_range.rb', line 217

def self.clean(version, loose = false)
  s = parse(version.strip.gsub(/^[=v]+/, ''), loose)
  return s ? s.version : nil
end

.cmp(a, op, b, loose = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/semantic_range.rb', line 59

def self.cmp(a, op, b, loose = false)
  case op
  when '==='
    a = a.version if !a.is_a?(String)
    b = b.version if !b.is_a?(String)
    a == b
  when '!=='
    a = a.version if !a.is_a?(String)
    b = b.version if !b.is_a?(String)
    a != b
  when '', '=', '=='
    eq(a, b, loose)
  when '!='
    neq(a, b, loose)
  when '>'
    gt(a, b, loose)
  when '>='
    gte(a, b, loose)
  when '<'
    lt(a, b, loose)
  when '<='
    lte(a, b, loose)
  else
    raise 'Invalid operator: ' + op
  end
end

.compare(a, b, loose = false) ⇒ Object



168
169
170
# File 'lib/semantic_range.rb', line 168

def self.compare(a, b, loose = false)
  Version.new(a, loose).compare(b)
end

.compare_loose(a, b) ⇒ Object



172
173
174
# File 'lib/semantic_range.rb', line 172

def self.compare_loose(a, b)
  compare(a, b, true)
end

.diff(a, b) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/semantic_range.rb', line 252

def self.diff(a, b)
  a = Version.new(a, false) unless a.kind_of?(Version)
  b = Version.new(b, false) unless b.kind_of?(Version)
  pre_diff = a.prerelease.to_s != b.prerelease.to_s
  pre = pre_diff ? 'pre' : ''
  return "#{pre}major" if a.major != b.major
  return "#{pre}minor" if a.minor != b.minor
  return "#{pre}patch" if a.patch != b.patch
  return "prerelease"  if pre_diff
end

.eq(a, b, loose = false) ⇒ Object



196
197
198
# File 'lib/semantic_range.rb', line 196

def self.eq(a, b, loose = false)
  compare(a, b, loose) == 0
end

.gt(a, b, loose = false) ⇒ Object



192
193
194
# File 'lib/semantic_range.rb', line 192

def self.gt(a, b, loose = false)
  compare(a, b, loose) > 0
end

.gte(a, b, loose = false) ⇒ Object



204
205
206
# File 'lib/semantic_range.rb', line 204

def self.gte(a, b, loose = false)
  compare(a, b, loose) >= 0
end

.gtr(version, range, loose = false) ⇒ Object



55
56
57
# File 'lib/semantic_range.rb', line 55

def self.gtr(version, range, loose = false)
  outside(version, range, '>', loose)
end

.increment!(version, release, loose, identifier) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/semantic_range.rb', line 241

def self.increment!(version, release, loose, identifier)
  if loose.is_a? String
    identifier = loose
    loose = false
  end

  Version.new(version, loose).increment!(release, identifier).version
rescue InvalidIncrement, NoMatchFound
  nil
end

.lt(a, b, loose = false) ⇒ Object



188
189
190
# File 'lib/semantic_range.rb', line 188

def self.lt(a, b, loose = false)
  compare(a, b, loose) < 0
end

.lte(a, b, loose = false) ⇒ Object



208
209
210
# File 'lib/semantic_range.rb', line 208

def self.lte(a, b, loose = false)
  compare(a, b, loose) <= 0
end

.ltr(version, range, loose = false) ⇒ Object



51
52
53
# File 'lib/semantic_range.rb', line 51

def self.ltr(version, range, loose = false)
  outside(version, range, '<', loose)
end

.max_satisfying(version, range, loose = false) ⇒ Object



154
155
156
# File 'lib/semantic_range.rb', line 154

def self.max_satisfying(version, range, loose = false)
  # TODO
end

.neq(a, b, loose = false) ⇒ Object



200
201
202
# File 'lib/semantic_range.rb', line 200

def self.neq(a, b, loose = false)
  compare(a, b, loose) != 0
end

.outside(version, range, hilo, loose = false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/semantic_range.rb', line 86

def self.outside(version, range, hilo, loose = false)
  version = Version.new(version, loose)
  range = Range.new(range, loose)

  return false if satisfies(version, range, loose)

  case hilo
  when '>'
    comp = '>'
    ecomp = '>='
  when '<'
    comp = '<'
    ecomp = '<='
  end

  range.set.each do |comparators|
    high = nil
    low = nil

    comparators.each do |comparator|
      if comparator.semver == ANY
        comparator = Comparator.new('>=0.0.0', loose)
      end

      high = high || comparator
      low = low || comparator

      case hilo
      when '>'
        if gt(comparator.semver, high.semver, loose)
          high = comparator
        elsif lt(comparator.semver, low.semver, loose)
          low = comparator
        end
      when '<'
        if lt(comparator.semver, high.semver, loose)
          high = comparator
        elsif gt(comparator.semver, low.semver, loose)
          low = comparator
        end
      end
    end

    return false if (high.operator == comp || high.operator == ecomp)

    case hilo
    when '>'
      if (low.operator.empty? || low.operator == comp) && lte(version, low.semver, loose)
        return false;
      elsif (low.operator == ecomp && lt(version, low.semver, loose))
        return false;
      end
    when '<'
      if (low.operator.empty? || low.operator == comp) && gte(version, low.semver, loose)
        return false;
      elsif low.operator == ecomp && gt(version, low.semver, loose)
        return false;
      end
    end
  end
  true
end

.parse(version, loose = false) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/semantic_range.rb', line 222

def self.parse(version, loose = false)
  return version if version.is_a?(Version)

  return nil unless version.is_a?(String)

  version.strip!

  return nil if version.length > MAX_LENGTH

  rxp = loose ? LOOSE : FULL
  return nil if !rxp.match(version)

  begin
    Version.new(version, loose)
  rescue
    nil
  end
end

.rcompare(a, b, loose = false) ⇒ Object



176
177
178
# File 'lib/semantic_range.rb', line 176

def self.rcompare(a, b, loose = false)
  compare(b, a, true)
end

.rsort(list, loose = false) ⇒ Object



184
185
186
# File 'lib/semantic_range.rb', line 184

def self.rsort(list, loose = false)
  # TODO
end

.satisfies(version, range, loose = false) ⇒ Object



149
150
151
152
# File 'lib/semantic_range.rb', line 149

def self.satisfies(version, range, loose = false)
  return false if !valid_range(range, loose)
  Range.new(range, loose).test(version)
end

.sort(list, loose = false) ⇒ Object



180
181
182
# File 'lib/semantic_range.rb', line 180

def self.sort(list, loose = false)
  # TODO
end

.valid(version, loose = false) ⇒ Object



212
213
214
215
# File 'lib/semantic_range.rb', line 212

def self.valid(version, loose = false)
  v = parse(version, loose)
  return v ? v.version : nil
end

.valid_range(range, loose = false) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/semantic_range.rb', line 158

def self.valid_range(range, loose = false)
  begin
    r = Range.new(range, loose).range
    r = '*' if r.nil? || r.empty?
    r
  rescue
    nil
  end
end