podhmo / python-node-semver

python version of node-semver
MIT License
21 stars 15 forks source link

refactoring -- remove make_xxx() function #12

Closed podhmo closed 7 years ago

podhmo commented 7 years ago

define function such like below.

def make_range(range_, loose):
    if isinstance(range_, Range) and range_.loose == loose:
        return range_

    # if (!(this instanceof Range))
    #    return new Range(range, loose);
    return Range(range_, loose)

I have defined these functions for js and python compatibility. but these are needless.

using __new__()

podhmo commented 7 years ago
class Range(object):
    def __new__(cls, range_, loose):
        if isinstance(range_, Range) and range_.loose == loose:
            return range_
        return super().__new__(cls)
podhmo commented 7 years ago

this is not good. for complete porting.

function Range(range, loose) {
  if (range instanceof Range) {
    if (range.loose === loose) {
      return range; // b.
    } else {
      return new Range(range.range, loose) //b2. 
    }
  }

  if (!(this instanceof Range))
    return new Range(range, loose); // c.

  // d.
  this.loose = loose;
  this.raw = range;
  // do something
}

and, b2 path's code is not ported by this method.