All files / emverLite mod.ts

96.99% Statements 129/133
91.89% Branches 34/37
97.56% Functions 40/41
97.65% Lines 125/128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 85 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 3241x   1x             4x 4x 4x               1x 19x               1x 6x 1x   5x 5x               1x 5x 1x   4x 4x               1x 1x           1x             124x     124x             149x 317x 149x 316x 3x     146x     150x 150x             4x       106x 220x 18x   202x 35x     167x 19x     34x       61x 23x   38x 74x 18x     20x     24x     9x     18x                 6x 2x 4x 2x   2x                 3x   1x 1x 1x         7x               1x               55x 19x   36x 36x 6x   33x 8x   29x 1x 12x 9x     28x 28x 3x 22x   25x 25x 4x 4x   4x 24x 24x               21x   1x 1x 6x 6x       2x 2x 9x 9x         18x   7x 7x 30x 30x       4x 4x 18x 18x       7x 7x 21x 21x                     38x 38x                     5x 5x   20x 8x   12x 12x 6x     6x   5x               4x 4x   17x 4x   13x 13x 4x     9x   4x                 1x 1x      
import * as matches from "ts-matches"
 
const starSub = /((\d+\.)*\d+)\.\*/
// prettier-ignore
export type ValidEmVer = string;
// prettier-ignore
export type ValidEmVerRange = string;
 
function incrementLastNumber(list: number[]) {
  const newList = [...list]
  newList[newList.length - 1]++
  return newList
}
/**
 * Will take in a range, like `>1.2` or `<1.2.3.4` or `=1.2` or `1.*`
 * and return a checker, that has the check function for checking that a version is in the valid
 * @param range
 * @returns
 */
export function rangeOf(range: string | Checker): Checker {
  return Checker.parse(range)
}
 
/**
 * Used to create a checker that will `and` all the ranges passed in
 * @param ranges
 * @returns
 */
export function rangeAnd(...ranges: (string | Checker)[]): Checker {
  if (ranges.length === 0) {
    throw new Error("No ranges given")
  }
  const [firstCheck, ...rest] = ranges
  return Checker.parse(firstCheck).and(...rest)
}
 
/**
 * Used to create a checker that will `or` all the ranges passed in
 * @param ranges
 * @returns
 */
export function rangeOr(...ranges: (string | Checker)[]): Checker {
  if (ranges.length === 0) {
    throw new Error("No ranges given")
  }
  const [firstCheck, ...rest] = ranges
  return Checker.parse(firstCheck).or(...rest)
}
 
/**
 * This will negate the checker, so given a checker that checks for >= 1.0.0, it will check for < 1.0.0
 * @param range
 * @returns
 */
export function notRange(range: string | Checker): Checker {
  return rangeOf(range).not()
}
 
/**
 * EmVer is a set of versioning of any pattern like 1 or 1.2 or 1.2.3 or 1.2.3.4 or ..
 */
export class EmVer {
  /**
   * Convert the range, should be 1.2.* or * into a emver
   * Or an already made emver
   * IsUnsafe
   */
  static from(range: string | EmVer): EmVer {
    Iif (range instanceof EmVer) {
      return range
    }
    return EmVer.parse(range)
  }
  /**
   * Convert the range, should be 1.2.* or * into a emver
   * IsUnsafe
   */
  static parse(rangeExtra: string): EmVer {
    const [range, extra] = rangeExtra.split("-")
    const values = range.split(".").map((x) => parseInt(x))
    for (const value of values) {
      if (isNaN(value)) {
        throw new Error(`Couldn't parse range: ${range}`)
      }
    }
    return new EmVer(values, extra)
  }
  private constructor(
    public readonly values: number[],
    readonly extra: string | null,
  ) {}
 
  /**
   * Used when we need a new emver that has the last number incremented, used in the 1.* like things
   */
  public withLastIncremented() {
    return new EmVer(incrementLastNumber(this.values), null)
  }
 
  public greaterThan(other: EmVer): boolean {
    for (const i in this.values) {
      if (other.values[i] == null) {
        return true
      }
      if (this.values[i] > other.values[i]) {
        return true
      }
 
      if (this.values[i] < other.values[i]) {
        return false
      }
    }
    return false
  }
 
  public equals(other: EmVer): boolean {
    if (other.values.length !== this.values.length) {
      return false
    }
    for (const i in this.values) {
      if (this.values[i] !== other.values[i]) {
        return false
      }
    }
    return true
  }
  public greaterThanOrEqual(other: EmVer): boolean {
    return this.greaterThan(other) || this.equals(other)
  }
  public lessThanOrEqual(other: EmVer): boolean {
    return !this.greaterThan(other)
  }
  public lessThan(other: EmVer): boolean {
    return !this.greaterThanOrEqual(other)
  }
  /**
   * Return a enum string that describes (used for switching/iffs)
   * to know comparison
   * @param other
   * @returns
   */
  public compare(other: EmVer) {
    if (this.equals(other)) {
      return "equal" as const
    } else if (this.greaterThan(other)) {
      return "greater" as const
    } else {
      return "less" as const
    }
  }
  /**
   * Used when sorting emver's in a list using the sort method
   * @param other
   * @returns
   */
  public compareForSort(other: EmVer) {
    return matches
      .matches(this.compare(other))
      .when("equal", () => 0 as const)
      .when("greater", () => 1 as const)
      .when("less", () => -1 as const)
      .unwrap()
  }
 
  toString() {
    return `${this.values.join(".")}${this.extra ? `-${this.extra}` : ""}` as ValidEmVer
  }
}
 
/**
 * A checker is a function that takes a version and returns true if the version matches the checker.
 * Used when we are doing range checking, like saying ">=1.0.0".check("1.2.3") will be true
 */
export class Checker {
  /**
   * Will take in a range, like `>1.2` or `<1.2.3.4` or `=1.2` or `1.*`
   * and return a checker, that has the check function for checking that a version is in the valid
   * @param range
   * @returns
   */
  static parse(range: string | Checker): Checker {
    if (range instanceof Checker) {
      return range
    }
    range = range.trim()
    if (range.indexOf("||") !== -1) {
      return rangeOr(...range.split("||").map((x) => Checker.parse(x)))
    }
    if (range.indexOf("&&") !== -1) {
      return rangeAnd(...range.split("&&").map((x) => Checker.parse(x)))
    }
    if (range === "*") {
      return new Checker((version) => {
        EmVer.from(version)
        return true
      }, range)
    }
    Iif (range.startsWith("!!")) return Checker.parse(range.substring(2))
    if (range.startsWith("!")) {
      const tempValue = Checker.parse(range.substring(1))
      return new Checker((x) => !tempValue.check(x), range)
    }
    const starSubMatches = starSub.exec(range)
    if (starSubMatches != null) {
      const emVarLower = EmVer.parse(starSubMatches[1])
      const emVarUpper = emVarLower.withLastIncremented()
 
      return new Checker((version) => {
        const v = EmVer.from(version)
        return (
          (v.greaterThan(emVarLower) || v.equals(emVarLower)) &&
          !v.greaterThan(emVarUpper) &&
          !v.equals(emVarUpper)
        )
      }, range)
    }
 
    switch (range.substring(0, 2)) {
      case ">=": {
        const emVar = EmVer.parse(range.substring(2))
        return new Checker((version) => {
          const v = EmVer.from(version)
          return v.greaterThanOrEqual(emVar)
        }, range)
      }
      case "<=": {
        const emVar = EmVer.parse(range.substring(2))
        return new Checker((version) => {
          const v = EmVer.from(version)
          return v.lessThanOrEqual(emVar)
        }, range)
      }
    }
 
    switch (range.substring(0, 1)) {
      case ">": {
        const emVar = EmVer.parse(range.substring(1))
        return new Checker((version) => {
          const v = EmVer.from(version)
          return v.greaterThan(emVar)
        }, range)
      }
      case "<": {
        const emVar = EmVer.parse(range.substring(1))
        return new Checker((version) => {
          const v = EmVer.from(version)
          return v.lessThan(emVar)
        }, range)
      }
      case "=": {
        const emVar = EmVer.parse(range.substring(1))
        return new Checker((version) => {
          const v = EmVer.from(version)
          return v.equals(emVar)
        }, `=${emVar.toString()}`)
      }
    }
    throw new Error("Couldn't parse range: " + range)
  }
  constructor(
    /**
     * Check is the function that will be given a emver or unparsed emver and should give if it follows
     * a pattern
     */
    public readonly check: (value: ValidEmVer | EmVer) => boolean,
    private readonly _range: string,
  ) {}
 
  get range() {
    return this._range as ValidEmVerRange
  }
 
  /**
   * Used when we want the `and` condition with another checker
   */
  public and(...others: (Checker | string)[]): Checker {
    const othersCheck = others.map(Checker.parse)
    return new Checker(
      (value) => {
        if (!this.check(value)) {
          return false
        }
        for (const other of othersCheck) {
          if (!other.check(value)) {
            return false
          }
        }
        return true
      },
      othersCheck.map((x) => x._range).join(" && "),
    )
  }
 
  /**
   * Used when we want the `or` condition with another checker
   */
  public or(...others: (Checker | string)[]): Checker {
    const othersCheck = others.map(Checker.parse)
    return new Checker(
      (value) => {
        if (this.check(value)) {
          return true
        }
        for (const other of othersCheck) {
          if (other.check(value)) {
            return true
          }
        }
        return false
      },
      othersCheck.map((x) => x._range).join(" || "),
    )
  }
 
  /**
   * A useful example is making sure we don't match an exact version, like !=1.2.3
   * @returns
   */
  public not(): Checker {
    let newRange = `!${this._range}`
    return Checker.parse(newRange)
  }
}