Skip to content

Commit ca999ec

Browse files
Merge pull request #328 from technote-space/release/next-v4.2.2
release: v4.2.3
2 parents 8c17f8f + eb8cd96 commit ca999ec

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

__tests__/utils1.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,41 +132,49 @@ describe('parseVersion', () => {
132132
});
133133

134134
it('should parse version 7', () => {
135+
const result = parseVersion('v1.2.3.4', {slice: 2});
136+
expect(result).not.toBeUndefined();
137+
expect(result?.core).toBe('1.2');
138+
expect(result?.preRelease).toBe(undefined);
139+
expect(result?.build).toBe(undefined);
140+
});
141+
142+
it('should parse version 8', () => {
135143
const result = parseVersion('1.0.0-rc.1');
136144
expect(result).not.toBeUndefined();
137145
expect(result?.core).toBe('1.0.0');
138146
expect(result?.preRelease).toBe('rc.1');
139147
expect(result?.build).toBe(undefined);
140148
});
141149

142-
it('should parse version 7', () => {
150+
it('should parse version 9', () => {
143151
const result = parseVersion('v2.0.0-alpha01');
144152
expect(result).not.toBeUndefined();
145153
expect(result?.core).toBe('2.0.0');
146154
expect(result?.preRelease).toBe('alpha01');
147155
expect(result?.build).toBe(undefined);
148156
});
149157

150-
it('should parse version 7', () => {
158+
it('should parse version 10', () => {
151159
const result = parseVersion('v3.0.0+f2eed76');
152160
expect(result).not.toBeUndefined();
153161
expect(result?.core).toBe('3.0.0');
154162
expect(result?.preRelease).toBe(undefined);
155163
expect(result?.build).toBe('f2eed76');
156164
});
157165

158-
it('should parse version 7', () => {
166+
it('should parse version 11', () => {
159167
const result = parseVersion('v1.0.0-beta+exp.sha.5114f85');
160168
expect(result).not.toBeUndefined();
161169
expect(result?.core).toBe('1.0.0');
162170
expect(result?.preRelease).toBe('beta');
163171
expect(result?.build).toBe('exp.sha.5114f85');
164172
});
165173

166-
it('should parse version 7', () => {
167-
const result = parseVersion('v1.0.0-beta+exp.sha.5114f85');
174+
it('should parse version 12', () => {
175+
const result = parseVersion('v1.2.3-beta+exp.sha.5114f85', {slice: 4});
168176
expect(result).not.toBeUndefined();
169-
expect(result?.core).toBe('1.0.0');
177+
expect(result?.core).toBe('1.2.3.0');
170178
expect(result?.preRelease).toBe('beta');
171179
expect(result?.build).toBe('exp.sha.5114f85');
172180
});
@@ -183,13 +191,21 @@ describe('normalizeVersion', () => {
183191
expect(normalizeVersion('v1.2')).toBe('1.2.0');
184192
expect(normalizeVersion('v1.2.3')).toBe('1.2.3');
185193
expect(normalizeVersion('v1.2.3.4')).toBe('1.2.3');
194+
expect(normalizeVersion('v1.2.3.4', {length: 5})).toBe('1.2.3.4.0');
186195
expect(normalizeVersion('v1.2.3.4', {cut: false})).toBe('1.2.3.4');
196+
expect(normalizeVersion('v1.2.3.4.5.6.7.8.9', {slice: 2})).toBe('1.2');
197+
expect(normalizeVersion('v1.2.3.4.5.6.7.8.9', {slice: -1})).toBe('1.2.3.4.5.6.7.8');
198+
expect(normalizeVersion('v1', {slice: -1})).toBe('1.0');
199+
expect(normalizeVersion('v1', {slice: -1, length: 5})).toBe('1.0.0.0');
200+
expect(normalizeVersion('v1', {slice: 0})).toBe('');
187201
expect(normalizeVersion('1', {fill: false})).toBe('1');
188202
expect(normalizeVersion('1.0.0.123-rc.1')).toBe('1.0.0-rc.1');
189203
expect(normalizeVersion('v2.0-alpha01')).toBe('2.0.0-alpha01');
190204
expect(normalizeVersion('v3.0.0+f2eed76')).toBe('3.0.0+f2eed76');
191205
expect(normalizeVersion('v1-beta+exp.sha.5114f85')).toBe('1.0.0-beta+exp.sha.5114f85');
192206
expect(normalizeVersion('v1-beta+exp.sha.5114f85', {onlyCore: true})).toBe('1.0.0');
207+
expect(normalizeVersion('v1-beta+exp.sha.5114f85', {slice: 2})).toBe('1.0-beta+exp.sha.5114f85');
208+
expect(normalizeVersion('v1-beta+exp.sha.5114f85', {slice: 2, onlyCore: true})).toBe('1.0');
193209
});
194210

195211
it('should return undefined', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "4.2.2",
3+
"version": "4.2.3",
44
"description": "Helper for GitHub Action.",
55
"keywords": [
66
"github",

src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const getBuildInfo = (filepath: string): {
3131

3232
export const isCloned = (workDir: string): boolean => fs.existsSync(path.resolve(workDir, '.git'));
3333

34-
export const parseVersion = (version: string, options?: { fill?: boolean; cut?: boolean; strict?: boolean; }): {
34+
export const parseVersion = (version: string, options?: { fill?: boolean; cut?: boolean; slice?: number; length?: number; strict?: boolean; }): {
3535
core: string;
3636
preRelease: string | undefined;
3737
build: string | undefined;
@@ -48,20 +48,22 @@ export const parseVersion = (version: string, options?: { fill?: boolean; cut?:
4848

4949
const fragments = split(matches[1], '.');
5050
// eslint-disable-next-line no-magic-numbers
51-
while (options?.fill !== false && fragments.length < 3) {
51+
const length = options?.slice && options.slice < 0 ? (options.length ?? 3) : (options?.slice ?? options?.length ?? 3);
52+
// eslint-disable-next-line no-magic-numbers
53+
while (options?.fill !== false && fragments.length < length) {
5254
fragments.push('0');
5355
}
5456

5557
return {
5658
// eslint-disable-next-line no-magic-numbers
57-
core: (options?.cut === false ? fragments : fragments.slice(0, 3)).join('.'),
59+
core: (options?.cut === false ? fragments : fragments.slice(0, options?.slice ?? options?.length ?? 3)).join('.'),
5860
preRelease: matches[5],
5961
build: matches[6],
6062
fragments,
6163
};
6264
};
6365

64-
export const normalizeVersion = (version: string, options?: { fill?: boolean; cut?: boolean; onlyCore?: boolean; }): string | undefined => {
66+
export const normalizeVersion = (version: string, options?: { fill?: boolean; cut?: boolean; slice?: number; length?: number; onlyCore?: boolean; }): string | undefined => {
6567
const parsed = parseVersion(version, options);
6668
if (!parsed) {
6769
return undefined;

0 commit comments

Comments
 (0)