semvercache.js 862 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { maxSatisfying } from 'semver';
  4. /**
  5. * A cache using semver ranges to retrieve values.
  6. */
  7. export class SemVerCache {
  8. constructor() {
  9. this._cache = Object.create(null);
  10. }
  11. set(key, version, object) {
  12. if (!(key in this._cache)) {
  13. this._cache[key] = Object.create(null);
  14. }
  15. if (!(version in this._cache[key])) {
  16. this._cache[key][version] = object;
  17. }
  18. else {
  19. throw `Version ${version} of key ${key} already registered.`;
  20. }
  21. }
  22. get(key, semver) {
  23. if (key in this._cache) {
  24. let versions = this._cache[key];
  25. let best = maxSatisfying(Object.keys(versions), semver);
  26. return versions[best];
  27. }
  28. }
  29. }