import is from '@sindresorhus/is' ; import semver , { SemVer } from 'semver' ; import stable from 'semver-stable' ; import { regEx } from '../../util/regex' ; import type { NewValueConfig , VersioningApi } from '../types' ; export const id = 'semver-coerced' ; export const displayName = 'Coerced Semantic Versioning' ; export const urls = [ 'https://semver.org/' ] ; export const supportsRanges = false ; const { is : isStable } = stable ; function sortVersions ( a , b ) { const aCoerced = semver . coerce ( a ) ; const bCoerced = semver . coerce ( b ) ; return aCoerced && bCoerced ? semver . compare ( aCoerced , bCoerced ) : 0 ; } function getMajor ( a ) { const aCoerced = semver . coerce ( a ) ; return aCoerced ? semver . major ( aCoerced ) : null ; } function getMinor ( a ) { const aCoerced = semver . coerce ( a ) ; return aCoerced ? semver . minor ( aCoerced ) : null ; } function getPatch ( a ) { return semver . patch ( a ) ; } function matches ( version , range ) { const coercedVersion = semver . coerce ( version ) ; return coercedVersion ? semver . satisfies ( coercedVersion , range ) : false ; } function equals ( a , b ) { const aCoerced = semver . coerce ( a ) ; const bCoerced = semver . coerce ( b ) ; return aCoerced && bCoerced ? semver . eq ( aCoerced , bCoerced ) : false ; } function isValid ( version ) { return ! ! semver . valid ( semver . coerce ( version ) ) ; } function getSatisfyingVersion ( versions , range ) { const coercedVersions = versions . map ( ( version ) => semver . coerce ( version ) ?. version ) . filter ( is . string ) ; return semver . maxSatisfying ( coercedVersions , range ) ; } function minSatisfyingVersion ( versions , range ) { const coercedVersions = versions . map ( ( version ) => semver . coerce ( version ) ?. version ) . filter ( is . string ) ; return semver . minSatisfying ( coercedVersions , range ) ; } function isLessThanRange ( version , range ) { const coercedVersion = semver . coerce ( version ) ; return coercedVersion ? semver . ltr ( coercedVersion , range ) : false ; } function isGreaterThan ( version , other ) { const coercedVersion = semver . coerce ( version ) ; const coercedOther = semver . coerce ( other ) ; return coercedVersion && coercedOther ? semver . gt ( coercedVersion , coercedOther ) : false ; } const startsWithNumberRegex = regEx ( `^\\d` ) ; function isSingleVersion ( version ) { if ( ! version . startsWith ( 'v' ) && ! startsWithNumberRegex . exec ( version ) ) { return false ; } return ! ! semver . valid ( semver . coerce ( version ) ) ; } export const isVersion = ( input ) : boolean => isValid ( input ) ; export { isVersion as isValid , getSatisfyingVersion } ; function getNewValue ( { newVersion } ) { return newVersion ; } function isCompatible ( version ) { return isVersion ( version ) ; } export const api = { equals , getMajor , getMinor , getPatch , isCompatible , isGreaterThan , isLessThanRange , isSingleVersion , isStable , isValid , isVersion , matches , getSatisfyingVersion , minSatisfyingVersion , getNewValue , sortVersions , } ; export default api ;