/
version.ts
93 lines (76 loc) · 2.65 KB
/
version.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { diff, major, minor, patch, prerelease } from '@snyk/ruby-semver';
import { create } from '@snyk/ruby-semver/lib/ruby/gem-version';
interface RubyVersion {
major: number;
minor: number;
patch: number;
prerelease: string[];
}
const parse = (version: string): RubyVersion => ({
major: major(version),
minor: minor(version),
patch: patch(version),
prerelease: prerelease(version),
});
const adapt = (left: string, right: string): string =>
left.split('.').slice(0, right.split('.').length).join('.');
const floor = (version: string): string =>
[...create(version).release().getSegments().slice(0, -1), 0].join('.');
// istanbul ignore next
const incrementLastSegment = (version: string): string => {
const segments = create(version).release().getSegments();
const nextLast = parseInt(segments[segments.length - 1], 10) + 1;
return [...segments.slice(0, -1), nextLast].join('.');
};
// istanbul ignore next
const incrementMajor = (
maj: number,
min: number,
ptch: number,
pre: string[]
): number => (min === 0 || ptch === 0 || pre.length === 0 ? maj + 1 : maj);
// istanbul ignore next
const incrementMinor = (min: number, ptch: number, pre: string[]): number =>
ptch === 0 || pre.length === 0 ? min + 1 : min;
// istanbul ignore next
const incrementPatch = (ptch: number, pre: string[]): number =>
pre.length === 0 ? ptch + 1 : ptch;
// istanbul ignore next
const increment = (from: string, to: string): string => {
const { major: maj, minor: min, patch: ptch, prerelease: pre } = parse(from);
let nextVersion: string;
switch (diff(from, adapt(to, from))) {
case 'major':
nextVersion = [incrementMajor(maj, min, ptch, pre || []), 0, 0].join('.');
break;
case 'minor':
nextVersion = [maj, incrementMinor(min, ptch, pre || []), 0].join('.');
break;
case 'patch':
nextVersion = [maj, min, incrementPatch(ptch, pre || [])].join('.');
break;
case 'prerelease':
nextVersion = [maj, min, ptch].join('.');
break;
default:
return incrementLastSegment(from);
}
return increment(nextVersion, to);
};
// istanbul ignore next
const decrement = (version: string): string => {
const segments = create(version).release().getSegments();
const nextSegments = segments
.reverse()
.reduce((accumulator: number[], segment: number, index: number) => {
if (index === 0) {
return [segment - 1];
}
if (accumulator[index - 1] === -1) {
return [...accumulator.slice(0, index - 1), 0, segment - 1];
}
return [...accumulator, segment];
}, []);
return nextSegments.reverse().join('.');
};
export { parse, floor, increment, decrement };