-
Notifications
You must be signed in to change notification settings - Fork 948
/
Copy pathsemvercache.ts
39 lines (34 loc) · 1.01 KB
/
semvercache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { maxSatisfying } from 'semver';
/**
* A cache using semver ranges to retrieve values.
*/
export class SemVerCache<T> {
set(key: string, version: string, object: T): void {
if (!(key in this._cache)) {
this._cache[key] = Object.create(null);
}
if (!(version in this._cache[key])) {
this._cache[key][version] = object;
} else {
throw `Version ${version} of key ${key} already registered.`;
}
}
get(key: string, semver: string): T | undefined {
if (key in this._cache) {
const versions = this._cache[key];
const best = maxSatisfying(Object.keys(versions), semver);
if (best !== null) {
return versions[best];
}
}
}
getAllVersions(key: string): Object | undefined {
if (key in this._cache) {
return this._cache[key];
}
}
private _cache: { [key: string]: { [version: string]: T } } =
Object.create(null);
}