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, InvalidComparator, InvalidIncrement, InvalidRange, InvalidVersion, 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 =
"3.1.0"
Class Method Summary collapse
- .clean(version, loose: false) ⇒ Object
- .cmp(a, op, b, loose: false) ⇒ Object
- .compare(a, b, loose: false) ⇒ Object
- .compare_loose(a, b) ⇒ Object
- .diff(a, b) ⇒ Object
- .eq?(a, b, loose: false) ⇒ Boolean (also: eq)
- .filter(versions, range, loose: false, platform: nil) ⇒ Object
- .gt?(a, b, loose: false) ⇒ Boolean (also: gt)
- .gte?(a, b, loose: false) ⇒ Boolean (also: gte)
- .gtr?(version, range, loose: false, platform: nil) ⇒ Boolean (also: gtr)
- .increment!(version, release, identifier, loose: false) ⇒ Object
- .lt?(a, b, loose: false) ⇒ Boolean (also: lt)
- .lte?(a, b, loose: false) ⇒ Boolean (also: lte)
- .ltr?(version, range, loose: false, platform: nil) ⇒ Boolean (also: ltr)
- .max_satisfying(versions, range, loose: false, platform: nil) ⇒ Object
- .neq?(a, b, loose: false) ⇒ Boolean (also: neq)
- .outside?(version, range, hilo, loose: false, platform: nil) ⇒ Boolean (also: outside)
- .parse(version, loose: false) ⇒ Object
- .rcompare(a, b, loose: false) ⇒ Object
- .rsort(list, loose: false) ⇒ Object
- .satisfies?(version, range, loose: false, platform: nil) ⇒ Boolean (also: satisfies)
- .sort(list, loose: false) ⇒ Object
- .to_comparators(range, loose: false, platform: nil) ⇒ Object
- .valid(version, loose: false) ⇒ Object
- .valid?(version, loose: false) ⇒ Boolean
- .valid_range(range, loose: false, platform: nil) ⇒ Object
Class Method Details
.clean(version, loose: false) ⇒ Object
242 243 244 245 |
# File 'lib/semantic_range.rb', line 242 def self.clean(version, loose: false) s = parse(version.strip.gsub(/^[=v]+/, ''), loose: loose) return s ? s.version : nil end |
.cmp(a, op, b, loose: false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/semantic_range.rb', line 61 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: loose) when '!=' neq?(a, b, loose: loose) when '>' gt?(a, b, loose: loose) when '>=' gte?(a, b, loose: loose) when '<' lt?(a, b, loose: loose) when '<=' lte?(a, b, loose: loose) else raise 'Invalid operator: ' + op end end |
.compare(a, b, loose: false) ⇒ Object
183 184 185 |
# File 'lib/semantic_range.rb', line 183 def self.compare(a, b, loose: false) Version.new(a, loose: loose).compare(b) end |
.compare_loose(a, b) ⇒ Object
187 188 189 |
# File 'lib/semantic_range.rb', line 187 def self.compare_loose(a, b) compare(a, b, loose: true) end |
.diff(a, b) ⇒ Object
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/semantic_range.rb', line 260 def self.diff(a, b) a = Version.new(a, loose: false) unless a.kind_of?(Version) b = Version.new(b, loose: 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) ⇒ Boolean Also known as: eq
211 212 213 |
# File 'lib/semantic_range.rb', line 211 def self.eq?(a, b, loose: false) compare(a, b, loose: loose) == 0 end |
.filter(versions, range, loose: false, platform: nil) ⇒ Object
159 160 161 162 163 |
# File 'lib/semantic_range.rb', line 159 def self.filter(versions, range, loose: false, platform: nil) return [] if !valid_range(range, loose: loose, platform: platform) versions.filter { |v| SemanticRange.satisfies?(v, range, loose: loose, platform: platform) } end |
.gt?(a, b, loose: false) ⇒ Boolean Also known as: gt
207 208 209 |
# File 'lib/semantic_range.rb', line 207 def self.gt?(a, b, loose: false) compare(a, b, loose: loose) > 0 end |
.gte?(a, b, loose: false) ⇒ Boolean Also known as: gte
219 220 221 |
# File 'lib/semantic_range.rb', line 219 def self.gte?(a, b, loose: false) compare(a, b, loose: loose) >= 0 end |
.gtr?(version, range, loose: false, platform: nil) ⇒ Boolean Also known as: gtr
57 58 59 |
# File 'lib/semantic_range.rb', line 57 def self.gtr?(version, range, loose: false, platform: nil) outside?(version, range, '>', loose: loose, platform: platform) end |
.increment!(version, release, identifier, loose: false) ⇒ Object
254 255 256 257 258 |
# File 'lib/semantic_range.rb', line 254 def self.increment!(version, release, identifier, loose: false) Version.new(version, loose: loose).increment!(release, identifier).version rescue InvalidIncrement, InvalidVersion nil end |
.lt?(a, b, loose: false) ⇒ Boolean Also known as: lt
203 204 205 |
# File 'lib/semantic_range.rb', line 203 def self.lt?(a, b, loose: false) compare(a, b, loose: loose) < 0 end |
.lte?(a, b, loose: false) ⇒ Boolean Also known as: lte
223 224 225 |
# File 'lib/semantic_range.rb', line 223 def self.lte?(a, b, loose: false) compare(a, b, loose: loose) <= 0 end |
.ltr?(version, range, loose: false, platform: nil) ⇒ Boolean Also known as: ltr
53 54 55 |
# File 'lib/semantic_range.rb', line 53 def self.ltr?(version, range, loose: false, platform: nil) outside?(version, range, '<', loose: loose, platform: platform) end |
.max_satisfying(versions, range, loose: false, platform: nil) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/semantic_range.rb', line 165 def self.(versions, range, loose: false, platform: nil) versions.select { |version| satisfies?(version, range, loose: loose, platform: platform) }.sort { |a, b| rcompare(a, b, loose: loose) }[0] || nil end |
.neq?(a, b, loose: false) ⇒ Boolean Also known as: neq
215 216 217 |
# File 'lib/semantic_range.rb', line 215 def self.neq?(a, b, loose: false) compare(a, b, loose: loose) != 0 end |
.outside?(version, range, hilo, loose: false, platform: nil) ⇒ Boolean Also known as: outside
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 148 149 |
# File 'lib/semantic_range.rb', line 88 def self.outside?(version, range, hilo, loose: false, platform: nil) version = Version.new(version, loose: loose) range = Range.new(range, loose: loose, platform: platform) return false if satisfies?(version, range, loose: loose, platform: platform) 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: loose) high = comparator elsif lt?(comparator.semver, low.semver, loose: loose) low = comparator end when '<' if lt?(comparator.semver, high.semver, loose: loose) high = comparator elsif gt?(comparator.semver, low.semver, loose: 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: loose) return false; elsif (low.operator == ecomp && lt?(version, low.semver, loose: loose)) return false; end when '<' if (low.operator.empty? || low.operator == comp) && gte?(version, low.semver, loose: loose) return false; elsif low.operator == ecomp && gt?(version, low.semver, loose: loose) return false; end end end true end |
.parse(version, loose: false) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/semantic_range.rb', line 247 def self.parse(version, loose: false) return version if version.is_a?(Version) return nil unless valid?(version, loose: loose) Version.new(version.strip, loose: loose) end |
.rcompare(a, b, loose: false) ⇒ Object
191 192 193 |
# File 'lib/semantic_range.rb', line 191 def self.rcompare(a, b, loose: false) compare(b, a, loose: true) end |
.rsort(list, loose: false) ⇒ Object
199 200 201 |
# File 'lib/semantic_range.rb', line 199 def self.rsort(list, loose: false) # TODO end |
.satisfies?(version, range, loose: false, platform: nil) ⇒ Boolean Also known as: satisfies
151 152 153 154 155 156 157 |
# File 'lib/semantic_range.rb', line 151 def self.satisfies?(version, range, loose: false, platform: nil) if valid?(range, loose: loose) return version == range end return false if !valid_range(range, loose: loose, platform: platform) Range.new(range, loose: loose, platform: platform).test(version) end |
.sort(list, loose: false) ⇒ Object
195 196 197 |
# File 'lib/semantic_range.rb', line 195 def self.sort(list, loose: false) # TODO end |
.to_comparators(range, loose: false, platform: nil) ⇒ Object
271 272 273 274 275 |
# File 'lib/semantic_range.rb', line 271 def self.to_comparators(range, loose: false, platform: nil) Range.new(range, loose: loose, platform: platform).set.map do |comp| comp.map(&:to_s) end end |
.valid(version, loose: false) ⇒ Object
237 238 239 240 |
# File 'lib/semantic_range.rb', line 237 def self.valid(version, loose: false) v = parse(version, loose: loose) return v ? v.version : nil end |
.valid?(version, loose: false) ⇒ Boolean
227 228 229 230 231 232 233 234 235 |
# File 'lib/semantic_range.rb', line 227 def self.valid?(version, loose: false) return false unless version.is_a?(String) stripped_version = version.strip return false if stripped_version.length > MAX_LENGTH rxp = loose ? LOOSE : FULL return rxp.match?(stripped_version) end |
.valid_range(range, loose: false, platform: nil) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/semantic_range.rb', line 173 def self.valid_range(range, loose: false, platform: nil) begin r = Range.new(range, loose: loose, platform: platform).range r = '*' if r.nil? || r.empty? r rescue nil end end |