version.ts 851 B

12345678910111213141516171819202122232425262728293031323334
  1. import _ from 'lodash';
  2. const versionPattern = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z\.]+))?/;
  3. export class SemVersion {
  4. major: number;
  5. minor: number;
  6. patch: number;
  7. meta: string;
  8. constructor(version: string) {
  9. const match = versionPattern.exec(version);
  10. if (match) {
  11. this.major = Number(match[1]);
  12. this.minor = Number(match[2] || 0);
  13. this.patch = Number(match[3] || 0);
  14. this.meta = match[4];
  15. }
  16. }
  17. isGtOrEq(version: string): boolean {
  18. const compared = new SemVersion(version);
  19. return !(this.major < compared.major || this.minor < compared.minor || this.patch < compared.patch);
  20. }
  21. isValid(): boolean {
  22. return _.isNumber(this.major);
  23. }
  24. }
  25. export function isVersionGtOrEq(a: string, b: string): boolean {
  26. const aSemver = new SemVersion(a);
  27. return aSemver.isGtOrEq(b);
  28. }