blang/semver

Semver Range Support?

lightglitch opened this issue · 7 comments

Semver Range Support?

What's a "semver range?"

A version range is a set of comparators which specify versions that satisfy the range.

https://github.com/npm/node-semver#ranges
https://gist.github.com/FichteFoll/a93e064468a6d9b14ea2

blang commented

Already thought about that a while ago. There may be some interesting use cases and we could also build a command line utility like node-semver does. Sadly, i don't have time for this right now, maybe in 1-2 month

@blang I will play with it little bit over weekend and send pull if it will result in something useful.
I would appreciate if you could send me some pointers or links to similar implementations.
EDIT: For now I will reference getcomposer.org versions.

blang commented

I would take a look at the node.js implementation (linked above) but i would not make a cli out of it.

The scenario: Your app get "2.0.1" as a string and you need to check if the conditions >=2.0.0 and <3.0.0 are true, both represented as string.
I would not support logical operators, the user can do that using if v.satisfies(r1) && v.satisfies(r2)...

Here's a raw sketch how i would do it:

func (v Version) Satisfies(r VersionRange) bool {
    return r.Includes(v)
}

type conditionFunc  func(Version, Version) bool
type VersionRange struct{
     v Version
     c conditionFunc
}
func Range(s string) (*VersionRange, error) { 
    /* parse Range `>`,`>=`, `=`, `<`, `<=` */ 
    return &VersionRange{
        v: Parse(s[x:]),
        c: parseCondition(s[0:x]),
    }
}
func (r VersionRange) Includes(v Version) bool {
      return r.c(r.v, v)
}

You can build conditionFuncs using the already existing comparators of Version.

Nodes use regex all over but i would love to stick to a manual parser which might be not that hard since you only need to parse >, >=,.. in front of a valid Version string.

What do you think?

blang commented

Implemented in aea32c9

Thank you!