import { combineVersion , extractComparator , pipe } from './utils' ; import { parseHyphen , parseComparatorTrim , parseTildeTrim , parseCaretTrim , parseCarets , parseTildes , parseXRanges , parseStar , parseGTE0 , } from './parser' ; import { compare , CompareAtom } from './compare' ; function parseComparatorString ( range ) { return pipe ( parseCarets , parseTildes , parseXRanges , parseStar ) ( range ) ; } function parseRange ( range ) { return pipe ( parseHyphen , parseComparatorTrim , parseTildeTrim , parseCaretTrim ) ( range . trim ( ) ) . split ( /\s+/ ) . join ( ' ' ) ; } export function satisfy ( version , range ) { if ( ! version ) { return false ; } const parsedRange = parseRange ( range ) ; const parsedComparator = parsedRange . split ( ' ' ) . map ( ( rangeVersion ) => parseComparatorString ( rangeVersion ) ) . join ( ' ' ) ; const comparators = parsedComparator . split ( /\s+/ ) . map ( ( comparator ) => parseGTE0 ( comparator ) ) ; const extractedVersion = extractComparator ( version ) ; if ( ! extractedVersion ) { return false ; } const [ , versionOperator , , versionMajor , versionMinor , versionPatch , versionPreRelease ] = extractedVersion ; const versionAtom = { operator : versionOperator , version : combineVersion ( versionMajor , versionMinor , versionPatch , versionPreRelease ) , major : versionMajor , minor : versionMinor , patch : versionPatch , preRelease : versionPreRelease ?. split ( '.' ) , } ; for ( const comparator of comparators ) { const extractedComparator = extractComparator ( comparator ) ; if ( ! extractedComparator ) { return false ; } const [ , rangeOperator , , rangeMajor , rangeMinor , rangePatch , rangePreRelease ] = extractedComparator ; const rangeAtom = { operator : rangeOperator , version : combineVersion ( rangeMajor , rangeMinor , rangePatch , rangePreRelease ) , major : rangeMajor , minor : rangeMinor , patch : rangePatch , preRelease : rangePreRelease ?. split ( '.' ) , } ; if ( ! compare ( rangeAtom , versionAtom ) ) { return false ; } } return true ; }