Source: lib/deprecate/version.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.deprecate.Version');
  7. /**
  8. * A class that defines what a library version is within the deprecation
  9. * system. Within deprecation we only care about the major and minor versions.
  10. *
  11. * @final
  12. */
  13. shaka.deprecate.Version = class {
  14. /**
  15. * @param {number} major
  16. * @param {number} minor
  17. */
  18. constructor(major, minor) {
  19. this.major_ = major;
  20. this.minor_ = minor;
  21. }
  22. /** @return {number} */
  23. major() { return this.major_; }
  24. /** @return {number} */
  25. minor() { return this.minor_; }
  26. /**
  27. * Returns:
  28. * - positive if |this| > |other|
  29. * - zero if |this| == |other|
  30. * - negative if |this| < |other|
  31. *
  32. * @param {!shaka.deprecate.Version} other
  33. * @return {number}
  34. */
  35. compareTo(other) {
  36. const majorCheck = this.major_ - other.major_;
  37. const minorCheck = this.minor_ - other.minor_;
  38. return majorCheck || minorCheck;
  39. }
  40. /** @override */
  41. toString() {
  42. return 'v' + this.major_ + '.' + this.minor_;
  43. }
  44. /**
  45. * Parse the major and minor values out of a version string that is assumed
  46. * to follow the grammar: "vMAJOR.MINOR.". What comes after the last "." we
  47. * will ignore.
  48. *
  49. * @param {string} versionString
  50. * @return {!shaka.deprecate.Version}
  51. */
  52. static parse(versionString) {
  53. // Make sure to drop the "v" from the front. We limit the number of splits
  54. // to two as we don't care what happens after the minor version number.
  55. // For example: 'a.b.c.d'.split('.', 2) == ['a', 'b']
  56. const components = versionString.substring(1).split('.', /* limit= */ 2);
  57. return new shaka.deprecate.Version(
  58. Number(components[0]),
  59. Number(components[1]));
  60. }
  61. };