aboutsummaryrefslogblamecommitdiffhomepage
path: root/shared/core-utils/common/version.ts
blob: 8a64f8c4d02d4c145c138d0379432191baa36f35 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                         
                                                                        






                                            
        
               
 
// Thanks https://stackoverflow.com/a/16187766
function compareSemVer (a: string, b: string) {
  const regExStrip0 = /(\.0+)+$/
  const segmentsA = a.replace(regExStrip0, '').split('.')
  const segmentsB = b.replace(regExStrip0, '').split('.')

  const l = Math.min(segmentsA.length, segmentsB.length)

  for (let i = 0; i < l; i++) {
    const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10)

    if (diff) return diff
  }

  return segmentsA.length - segmentsB.length
}

export {
  compareSemVer
}