Skip to content

Commit

Permalink
move devel logic into the compare method
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacov committed Aug 26, 2020
1 parent a5f557c commit 7db54ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { iGetName } from '../../../components/create-vm-wizard/selectors/immutable/selectors';
import { ITemplate } from '../../../types/template';
import { iGetCreationTimestamp, iGetLabels } from '../common';
import { compareVersions, splitVersion } from '../../../utils/sort';
import { compareVersions } from '../../../utils/sort';

type FindTemplateOptions = {
userTemplateName?: string;
Expand Down Expand Up @@ -83,16 +83,9 @@ export const iGetRelevantTemplates = (
const aVersion = aLabels?.get(TEMPLATE_VERSION_LABEL);
const bVersion = bLabels?.get(TEMPLATE_VERSION_LABEL);

const versionCMP = compareVersions(splitVersion(aVersion), splitVersion(bVersion)) * -1; // descending
const versionCMP = compareVersions(aVersion, bVersion) * -1; // descending

if (versionCMP !== 0) {
// 'devel' version if exist is always the highst version.
if (aVersion === 'devel') {
return -1;
}
if (bVersion === 'devel') {
return 1;
}
return versionCMP;
}

Expand Down
25 changes: 15 additions & 10 deletions frontend/packages/kubevirt-plugin/src/utils/__tests__/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import { OperatingSystemRecord } from '../../types/types';
*/

describe('compareVersions', () => {
const osVersion0: number[] = null;
const osVersion1: number[] = [1, 2, 3];
const osVersion2: number[] = [1, 2, 4];
const osVersion3: number[] = [1, 1];
const osVersion4: number[] = [1];
const osVersion5: number[] = [2];
const osVersion6: number[] = [1, 0];
const osVersion7: number[] = [1, 0, 0];
const osVersion8: number[] = [0];
const osVersion9: number[] = [0, 0, 0];
const osVersion0: string = null;
const osVersion1: string = 'v1.2.3';
const osVersion2: string = 'v1.2.4';
const osVersion3: string = '1.1';
const osVersion4: string = '1';
const osVersion5: string = '2';
const osVersion6: string = '1.0';
const osVersion7: string = '1.0.0';
const osVersion8: string = '0';
const osVersion9: string = '0.0.0';
const osVersion10: string = 'devel';

it('check non-equal versions with same length', () => {
expect(compareVersions(osVersion1, osVersion2)).toEqual(-1);
Expand Down Expand Up @@ -59,6 +60,10 @@ describe('compareVersions', () => {
it('check equal versions when both of them are null', () => {
expect(compareVersions(osVersion0, osVersion0)).toEqual(0);
});

it('check devel version when both of them are not null', () => {
expect(compareVersions(osVersion10, osVersion9)).toEqual(1);
});
});

describe('removeOSDups', () => {
Expand Down
18 changes: 13 additions & 5 deletions frontend/packages/kubevirt-plugin/src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ignoreCaseSort = <T>(
return array.sort((a, b) => resolve(a).localeCompare(resolve(b)));
};

export const splitVersion = (osID: string): number[] =>
const splitVersion = (osID: string): number[] =>
(osID || '')
.split(/\D/)
.filter((x) => x)
Expand All @@ -72,13 +72,21 @@ export const splitVersion = (osID: string): number[] =>
* return 0 when equal.
*
*/
export const compareVersions = (version1: number[], version2: number[]): number => {
export const compareVersions = (version1: string, version2: string): number => {
if (!version1 && !version2) {
return 0;
}

const finalVersion1 = version1 || [];
const finalVersion2 = version2 || [];
// 'devel' version if exist is always the highst version.
if (version1 === 'devel') {
return 1;
}
if (version2 === 'devel') {
return -1;
}

const finalVersion1 = splitVersion(version1) || [];
const finalVersion2 = splitVersion(version2) || [];

const zipped = _.zip(finalVersion1, finalVersion2);
let idx = 0;
Expand Down Expand Up @@ -111,7 +119,7 @@ const descSortOSes = (os1: OperatingSystemRecord, os2: OperatingSystemRecord): n
return nameCMP * -1;
}

return compareVersions(splitVersion(os1.id), splitVersion(os2.id)) * -1;
return compareVersions(os1.id, os2.id) * -1;
};

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
Expand Down

0 comments on commit 7db54ae

Please sign in to comment.