Module: Philiprehberger::VersionCompare

Defined in:
lib/philiprehberger/version_compare.rb,
lib/philiprehberger/version_compare/version.rb

Defined Under Namespace

Classes: Error, SemanticVersion

Constant Summary collapse

VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.filter(versions, constraint) ⇒ Array<String>

Filter an array of version strings by a constraint

Parameters:

  • versions (Array<String>)

    version strings to filter

  • constraint (String)

    constraint like “>= 1.0.0”, “~> 2.1”

Returns:

  • (Array<String>)

    versions that satisfy the constraint



193
194
195
# File 'lib/philiprehberger/version_compare.rb', line 193

def self.filter(versions, constraint)
  versions.select { |v| parse(v).satisfies?(constraint) }
end

.highest_satisfying(versions, constraint) ⇒ String?

Return the highest version from an array that satisfies a constraint

Performs a single pass over versions, tracking the maximum parsed version whose parsed form satisfies the constraint. Pre-release and stable resolution follow the same precedence used by sort and latest (pre-release versions rank lower than their stable counterparts per SemVer).

Parameters:

  • versions (Array<String>)

    version strings to search

  • constraint (String)

    constraint like “>= 1.0.0”, “~> 2.1”

Returns:

  • (String, nil)

    the highest version string that satisfies the constraint, or nil if versions is empty or nothing matches



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/philiprehberger/version_compare.rb', line 209

def self.highest_satisfying(versions, constraint)
  best_raw = nil
  best_parsed = nil

  versions.each do |v|
    parsed = parse(v)
    next unless parsed.satisfies?(constraint)

    if best_parsed.nil? || parsed > best_parsed
      best_parsed = parsed
      best_raw = v
    end
  end

  best_raw
end

.latest(versions) ⇒ String

Return the latest version from an array of version strings

Parameters:

  • versions (Array<String>)

    version strings

Returns:

  • (String)

    the latest version string

Raises:

  • (Error)

    if the array is empty



182
183
184
185
186
# File 'lib/philiprehberger/version_compare.rb', line 182

def self.latest(versions)
  raise Error, 'no versions provided' if versions.empty?

  versions.max_by { |v| parse(v) }
end

.parse(str) ⇒ SemanticVersion

Parse a version string into a SemanticVersion

Parameters:

  • str (String)

    version string like “1.2.3”, “1.2.3-beta.1”, or “1.2.3+build.123”

Returns:

Raises:

  • (Error)

    if the string is not a valid version



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/philiprehberger/version_compare.rb', line 153

def self.parse(str)
  str = str.to_s.strip
  str = str.sub(/\Av/, '')

  match = str.match(/\A(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([a-zA-Z0-9.]+))?(?:\+([a-zA-Z0-9.]+))?\z/)
  raise Error, "invalid version: #{str}" unless match

  SemanticVersion.new(
    match[1].to_i,
    (match[2] || '0').to_i,
    (match[3] || '0').to_i,
    pre_release: match[4],
    build_metadata: match[5]
  )
end

.sort(versions) ⇒ Array<String>

Sort an array of version strings

Parameters:

  • versions (Array<String>)

    version strings to sort

Returns:

  • (Array<String>)

    sorted version strings (ascending)



173
174
175
# File 'lib/philiprehberger/version_compare.rb', line 173

def self.sort(versions)
  versions.sort_by { |v| parse(v) }
end