UNPKG

@wocker/ws

Version:

Docker workspace for web projects

69 lines (68 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VersionRule = void 0; const Version_1 = require("./Version"); class VersionRule { constructor(data) { Object.assign(this, data); } get version() { return new Version_1.Version({ major: this.major ?? 0, minor: this.minor ?? 0, patch: this.patch ?? 0, tag: this.tag, build: this.build }); } match(version, withTag) { if (typeof version === "string") { version = Version_1.Version.parse(version); } if ((!withTag || this.tag) && this.tag !== version.tag) { return false; } const cmp = version.compare(this.version); switch (this.prefix) { case ">": return cmp > 0; case ">=": return cmp >= 0; case "<": return cmp < 0; case "<=": return cmp <= 0; case "^": return (this.major === version.major && cmp >= 0); case "~": return (this.major === version.major && this.minor === version.minor && cmp >= 0); default: return ((this.major === undefined || this.major === version.major) && (this.minor === undefined || this.minor === version.minor) && (this.patch === undefined || this.patch === version.patch)); } } static parse(rule) { if (!VersionRule.map.has(rule)) { if (!VersionRule.REGEXP.test(rule)) { throw new Error("Invalid version rule"); } const [, prefix, major, minor, patch, tag, build, onlyTag] = VersionRule.REGEXP.exec(rule) || []; VersionRule.map.set(rule, new VersionRule({ prefix, major: major && major !== "x" && major !== "*" ? parseInt(major) : undefined, minor: minor && minor !== "x" ? parseInt(minor) : undefined, patch: patch && patch !== "x" ? parseInt(patch) : undefined, tag: tag || onlyTag, build: build && build !== "x" ? parseInt(build) : undefined })); } return VersionRule.map.get(rule); } } exports.VersionRule = VersionRule; VersionRule.REGEXP = /^(?:(\^|~|<=|>=|<|>)?(x|\d+)(?:\.(\*|x|\d+))?(?:\.(x|\d+))?(?:-([a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)(?:\.(\d+))?)?|([a-zA-Z0-9]+(?:-[a-zAZ0-9]+)*))$/; VersionRule.map = new Map();