version.ts 871 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. let 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. let compared = new SemVersion(version);
  19. return !(
  20. this.major < compared.major ||
  21. this.minor < compared.minor ||
  22. this.patch < compared.patch
  23. );
  24. }
  25. isValid(): boolean {
  26. return _.isNumber(this.major);
  27. }
  28. }
  29. export function isVersionGtOrEq(a: string, b: string): boolean {
  30. let a_semver = new SemVersion(a);
  31. return a_semver.isGtOrEq(b);
  32. }