Skip to content

Commit

Permalink
extend ComparableVersion static methods
Browse files Browse the repository at this point in the history
* move compareComparableVersions to ComparableVersion.compare
* add ComparableVersion.max
  • Loading branch information
BuZZ-T committed Apr 18, 2022
1 parent a0963ff commit 530be1a
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 85 deletions.
116 changes: 116 additions & 0 deletions commons/ComparableVersion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @group unit/class/ComparableVersion
*/

import { Compared } from '../interfaces/enums'
import { ComparableVersion } from './ComparableVersion'

describe('ComparableVersion', () => {
Expand Down Expand Up @@ -236,4 +237,119 @@ describe('ComparableVersion', () => {
expect(cVersion.toString()).toEqual(versionString)
})
})

describe('compare', () => {
it('should compare major versions', () => {
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(20, 0, 0, 0)
)).toEqual(Compared.LESS)
expect(ComparableVersion.compare(
new ComparableVersion(20, 0, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare minor versions', () => {
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 1, 0, 0)
)).toEqual(Compared.LESS)
expect(ComparableVersion.compare(
new ComparableVersion(10, 1, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare branch versions', () => {
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 1, 0)
)).toEqual(Compared.LESS)
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 1, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare patch versions', () => {
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 0, 1)
)).toEqual(Compared.LESS)
expect(ComparableVersion.compare(
new ComparableVersion(10, 0, 0, 1),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare equal versions', () => {
expect(ComparableVersion.compare(
new ComparableVersion(10, 1, 2, 3),
new ComparableVersion(10, 1, 2, 3)
)).toEqual(Compared.EQUAL)
})
})

describe('max', () => {
it('should select the greater major version', () => {
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(15, 0, 0, 0),
new ComparableVersion(20, 0, 0, 0)
)).toEqual(new ComparableVersion(20, 0, 0, 0))
expect(ComparableVersion.max(
new ComparableVersion(20, 0, 0, 0),
new ComparableVersion(15, 0, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(new ComparableVersion(20, 0, 0, 0))
})

it('should select the greater minor version', () => {
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 1, 0, 0),
new ComparableVersion(10, 2, 0, 0)
)).toEqual(new ComparableVersion(10, 2, 0, 0))
expect(ComparableVersion.max(
new ComparableVersion(10, 2, 0, 0),
new ComparableVersion(10, 1, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(new ComparableVersion(10, 2, 0, 0))
})

it('should select the greater branch version', () => {
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 1, 0),
new ComparableVersion(10, 0, 2, 0),

)).toEqual(new ComparableVersion(10, 0, 2, 0))
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 2, 0),
new ComparableVersion(10, 0, 1, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(new ComparableVersion(10, 0, 2, 0))
})

it('should select the greater patch version', () => {
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 0, 1),
new ComparableVersion(10, 0, 0, 2),
)).toEqual(new ComparableVersion(10, 0, 0, 2))
expect(ComparableVersion.max(
new ComparableVersion(10, 0, 0, 2),
new ComparableVersion(10, 0, 0, 1),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(new ComparableVersion(10, 0, 0, 2))
})

it('should select one of both equal versions', () => {
expect(ComparableVersion.max(
new ComparableVersion(10, 1, 2, 3),
new ComparableVersion(10, 1, 2, 3)
)).toEqual(new ComparableVersion(10, 1, 2, 3))
})
})
})
30 changes: 30 additions & 0 deletions commons/ComparableVersion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Compared } from '../interfaces/enums'
import type { IVersion } from '../interfaces/interfaces'
import { isIVersion } from '../utils/typeguards'

Expand Down Expand Up @@ -51,4 +52,33 @@ export class ComparableVersion implements IVersion {
public toString(): string {
return `${this.major}.${this.minor}.${this.branch}.${this.patch}`
}

/**
* Compares two ComparableVersions with each other.
* if version < other, the result is Compared.LESS
* if version > other, the result is Compared.GREATER
* if version === other, the result is Compared.EQUAL
*
* @param version
* @param other
*/
public static compare(version: ComparableVersion, other: ComparableVersion): Compared {
if (version.major > other.major) { return Compared.GREATER }
if (version.major < other.major) { return Compared.LESS }

if (version.minor > other.minor) { return Compared.GREATER }
if (version.minor < other.minor) { return Compared.LESS }

if (version.branch > other.branch) { return Compared.GREATER }
if (version.branch < other.branch) { return Compared.LESS }

if (version.patch > other.patch) { return Compared.GREATER }
if (version.patch < other.patch) { return Compared.LESS }

return Compared.EQUAL
}

public static max(...versions: ComparableVersion[]): ComparableVersion {
return versions.reduce((currentMax, version) => ComparableVersion.compare(currentMax, version) === Compared.LESS ? version : currentMax)
}
}
56 changes: 1 addition & 55 deletions utils/sort.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,11 @@

import { ComparableVersion } from '../commons/ComparableVersion'
import { MappedVersion } from '../commons/MappedVersion'
import { Compared } from '../interfaces/enums'
import { createStore } from '../test/test.utils'
import { sortDescendingMappedVersions, sortAscendingMappedVersions, compareComparableVersions, sortStoreEntries, sortAscendingComparableVersions, sortDescendingComparableVersions } from './sort.utils'
import { sortDescendingMappedVersions, sortAscendingMappedVersions, sortStoreEntries, sortAscendingComparableVersions, sortDescendingComparableVersions } from './sort.utils'

describe('sort utils', () => {

describe('compareComparableVersions', () => {
it('should compare major versions', () => {
expect(compareComparableVersions(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(20, 0, 0, 0)
)).toEqual(Compared.LESS)
expect(compareComparableVersions(
new ComparableVersion(20, 0, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare minor versions', () => {
expect(compareComparableVersions(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 1, 0, 0)
)).toEqual(Compared.LESS)
expect(compareComparableVersions(
new ComparableVersion(10, 1, 0, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare branch versions', () => {
expect(compareComparableVersions(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 1, 0)
)).toEqual(Compared.LESS)
expect(compareComparableVersions(
new ComparableVersion(10, 0, 1, 0),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare patch versions', () => {
expect(compareComparableVersions(
new ComparableVersion(10, 0, 0, 0),
new ComparableVersion(10, 0, 0, 1)
)).toEqual(Compared.LESS)
expect(compareComparableVersions(
new ComparableVersion(10, 0, 0, 1),
new ComparableVersion(10, 0, 0, 0)
)).toEqual(Compared.GREATER)
})

it('should compare equal versions', () => {
expect(compareComparableVersions(
new ComparableVersion(10, 1, 2, 3),
new ComparableVersion(10, 1, 2, 3)
)).toEqual(Compared.EQUAL)
})
})

describe('sortAscendingComparableVersions', () => {
let versionMajor1: ComparableVersion
let versionMajor2: ComparableVersion
Expand Down
29 changes: 2 additions & 27 deletions utils/sort.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,11 @@ import type { MappedVersion } from '../commons/MappedVersion'
import { Compared } from '../interfaces/enums'
import type { IListStore } from '../interfaces/store.interfaces'

/**
* Compares two ComparableVersions with each other.
* if version < other, the result is Compared.LESS
* if version > other, the result is Compared.GREATER
* if version === other, the result is Compared.EQUAL
*
* @param version
* @param other
*/
export function compareComparableVersions(version: ComparableVersion, other: ComparableVersion): Compared {
if (version.major > other.major) { return Compared.GREATER }
if (version.major < other.major) { return Compared.LESS }

if (version.minor > other.minor) { return Compared.GREATER }
if (version.minor < other.minor) { return Compared.LESS }

if (version.branch > other.branch) { return Compared.GREATER }
if (version.branch < other.branch) { return Compared.LESS }

if (version.patch > other.patch) { return Compared.GREATER }
if (version.patch < other.patch) { return Compared.LESS }

return Compared.EQUAL
}

/**
* Ascending sort comparator for ComparableVersion
*/
export function sortAscendingComparableVersions(a: ComparableVersion, b: ComparableVersion): -1 | 0 | 1 {
const compared = compareComparableVersions(a, b)
const compared = ComparableVersion.compare(a, b)

if (compared === Compared.GREATER) {
return 1
Expand All @@ -48,7 +23,7 @@ export function sortAscendingComparableVersions(a: ComparableVersion, b: Compara
* Descending sort comparator for ComparableVersion
*/
export function sortDescendingComparableVersions(a: ComparableVersion, b: ComparableVersion): -1 | 0 | 1 {
const compared = compareComparableVersions(a, b)
const compared = ComparableVersion.compare(a, b)

if (compared === Compared.LESS) {
return 1
Expand Down
7 changes: 4 additions & 3 deletions versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse, HTMLElement as NodeParserHTMLElement } from 'node-html-parser'

import { fetchBranchPosition, fetchChromeUrl, fetchChromiumTags } from './api'
import { ComparableVersion } from './commons/ComparableVersion'
import { SEARCH_BINARY } from './commons/loggerTexts'
import { MappedVersion } from './commons/MappedVersion'
import { Compared } from './interfaces/enums'
Expand All @@ -16,7 +17,7 @@ import { userSelectedVersion } from './select'
import { Store } from './store/Store'
import { storeNegativeHit } from './store/storeNegativeHit'
import { detectOperatingSystem } from './utils'
import { compareComparableVersions, sortDescendingMappedVersions } from './utils/sort.utils'
import { sortDescendingMappedVersions } from './utils/sort.utils'

export async function getChromeDownloadUrl(config: IChromeConfig, mappedVersions: MappedVersion[]): Promise<GetChromeDownloadUrlReturn> {
const oSSetting = detectOperatingSystem(config)
Expand Down Expand Up @@ -220,8 +221,8 @@ export function mapVersions(versions: string[], config: IChromeConfig, store: St
const filteredVersions = versions
.map(version => new MappedVersion(version, versionSet.has(version)))
.filter(version => !config.hideNegativeHits || !version.disabled)
.filter(version => compareComparableVersions(version.comparable, config.min) !== Compared.LESS
&& compareComparableVersions(version.comparable, config.max) !== Compared.GREATER)
.filter(version => ComparableVersion.compare(version.comparable, config.min) !== Compared.LESS
&& ComparableVersion.compare(version.comparable, config.max) !== Compared.GREATER)
.sort(sortDescendingMappedVersions)

// Don't reduce the amount of filtered versions when --only-newest-major is set
Expand Down

0 comments on commit 530be1a

Please sign in to comment.