import { Constants } from '../../core/constants' ; export class VersionComparer { public static isNewerVersion ( oldVersion , newVersion ) { const oldVersionParts = oldVersion . split ( '-' ) ; const newVersionParts = newVersion . split ( '-' ) ; const oldVersionNumberAsString = oldVersionParts [ 0 ] ; const newVersionNumberAsString = newVersionParts [ 0 ] ; const oldVersionHasTag = oldVersionParts . length > 1 ; const newVersionHasTag = newVersionParts . length > 1 ; let oldVersionTag ; let newVersionTag ; if ( oldVersionHasTag ) { oldVersionTag = oldVersionParts [ 1 ] ; } if ( newVersionHasTag ) { newVersionTag = newVersionParts [ 1 ] ; } if ( ! oldVersionHasTag && ! newVersionHasTag ) { return this . isNewerVersionNumber ( oldVersionNumberAsString , newVersionNumberAsString ) ; } if ( oldVersionHasTag && newVersionHasTag ) { if ( oldVersionNumberAsString === newVersionNumberAsString ) { return this . isNewerVersionTag ( oldVersionTag , newVersionTag ) ; } return ( this . isNewerVersionNumber ( oldVersionNumberAsString , newVersionNumberAsString ) && this . isNewerVersionTag ( oldVersionTag , newVersionTag ) ) ; } if ( ! oldVersionHasTag && newVersionHasTag ) { return this . isNewerVersionNumber ( oldVersionNumberAsString , newVersionNumberAsString ) ; } if ( oldVersionHasTag && ! newVersionHasTag ) { return this . isNewerVersionNumber ( oldVersionNumberAsString , newVersionNumberAsString ) ; } return false ; } private static isNewerVersionNumber ( oldVersionNumberAsString , newVersionNumberAsString ) { const oldVersionParts = oldVersionNumberAsString . split ( '.' ) ; const newVersionParts = newVersionNumberAsString . split ( '.' ) ; for ( let i = 0 ; i < newVersionParts . length ; i ++ ) { let newVersionNumber = parseInt ( newVersionParts [ i ] , 10 ) ; let oldVersionNumber = parseInt ( oldVersionParts [ i ] , 10 ) ; if ( newVersionNumber == undefined || Number . isNaN ( newVersionNumber ) ) { newVersionNumber = 0 ; } if ( oldVersionNumber == undefined || Number . isNaN ( oldVersionNumber ) ) { oldVersionNumber = 0 ; } if ( newVersionNumber > oldVersionNumber ) { return true ; } if ( newVersionNumber < oldVersionNumber ) { return false ; } } return false ; } private static isNewerVersionTag ( oldVersionTag , newVersionTag ) { if ( oldVersionTag . includes ( Constants . releaseCandidateApplicationTag ) && newVersionTag . includes ( Constants . previewApplicationTag ) ) { return false ; } if ( oldVersionTag . includes ( Constants . previewApplicationTag ) && newVersionTag . includes ( Constants . releaseCandidateApplicationTag ) ) { return true ; } if ( ( oldVersionTag . includes ( Constants . previewApplicationTag ) && newVersionTag . includes ( Constants . previewApplicationTag ) ) || ( oldVersionTag . includes ( Constants . releaseCandidateApplicationTag ) && newVersionTag . includes ( Constants . releaseCandidateApplicationTag ) ) ) { const oldVersionTagParts = oldVersionTag . split ( '.' ) ; const newVersionTagParts = newVersionTag . split ( '.' ) ; const oldVersionIteration = parseInt ( oldVersionTagParts [ 1 ] , 10 ) ; const newVersionIteration = parseInt ( newVersionTagParts [ 1 ] , 10 ) ; return newVersionIteration > oldVersionIteration ; } return false ; } }