Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git-base/src/util.ts
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export interface IDisposable {
7
dispose(): void;
8
}
9
10
export function toDisposable(dispose: () => void): IDisposable {
11
return { dispose };
12
}
13
14
export function done<T>(promise: Promise<T>): Promise<void> {
15
return promise.then<void>(() => undefined);
16
}
17
18
export namespace Versions {
19
declare type VersionComparisonResult = -1 | 0 | 1;
20
21
export interface Version {
22
major: number;
23
minor: number;
24
patch: number;
25
pre?: string;
26
}
27
28
export function compare(v1: string | Version, v2: string | Version): VersionComparisonResult {
29
if (typeof v1 === 'string') {
30
v1 = fromString(v1);
31
}
32
if (typeof v2 === 'string') {
33
v2 = fromString(v2);
34
}
35
36
if (v1.major > v2.major) { return 1; }
37
if (v1.major < v2.major) { return -1; }
38
39
if (v1.minor > v2.minor) { return 1; }
40
if (v1.minor < v2.minor) { return -1; }
41
42
if (v1.patch > v2.patch) { return 1; }
43
if (v1.patch < v2.patch) { return -1; }
44
45
if (v1.pre === undefined && v2.pre !== undefined) { return 1; }
46
if (v1.pre !== undefined && v2.pre === undefined) { return -1; }
47
48
if (v1.pre !== undefined && v2.pre !== undefined) {
49
return v1.pre.localeCompare(v2.pre) as VersionComparisonResult;
50
}
51
52
return 0;
53
}
54
55
export function from(major: string | number, minor: string | number, patch?: string | number, pre?: string): Version {
56
return {
57
major: typeof major === 'string' ? parseInt(major, 10) : major,
58
minor: typeof minor === 'string' ? parseInt(minor, 10) : minor,
59
patch: patch === undefined || patch === null ? 0 : typeof patch === 'string' ? parseInt(patch, 10) : patch,
60
pre: pre,
61
};
62
}
63
64
export function fromString(version: string): Version {
65
const [ver, pre] = version.split('-');
66
const [major, minor, patch] = ver.split('.');
67
return from(major, minor, patch, pre);
68
}
69
}
70
71