[Gradle, JS] Intersect of NpmRange

^KT-41054 fixed
This commit is contained in:
Ilya Goncharov
2020-08-04 17:53:39 +03:00
parent 90bee78dda
commit 14fac83e2b
2 changed files with 97 additions and 3 deletions
@@ -8,16 +8,88 @@ package org.jetbrains.kotlin.gradle.targets.js.npm
import com.github.gundy.semver4j.model.Version
data class NpmRange(
val startVersion: Version? = null,
val startVersion: SemVer? = null,
val startInclusive: Boolean = false,
val endVersion: Version? = null,
val endVersion: SemVer? = null,
val endInclusive: Boolean = false
)
val NONE_RANGE = NpmRange(
startVersion = Version.fromString(NONE_VERSION)
.toSemVer()
)
infix operator fun NpmRange.plus(other: NpmRange) {
infix fun NpmRange.or(other: NpmRange): List<NpmRange> {
val startVersion = min(this.startVersion, other.startVersion)
val endVersion = max(this.endVersion, other.endVersion)
return NpmRange(
startVersion = startVersion,
startInclusive = if (startVersion == other.startVersion) other.startInclusive else this.startInclusive,
endVersion = endVersion,
endInclusive = if (startVersion == other.startVersion) other.startInclusive else this.startInclusive
)
}
infix fun NpmRange.intersect(other: NpmRange): NpmRange? {
if (!isIntersect(other)) return null
val startVersion = max(this.startVersion, other.startVersion)
val endVersion = min(this.endVersion, other.endVersion)
return NpmRange(
startVersion = startVersion,
startInclusive = if (startVersion == this.startVersion) this.startInclusive else other.startInclusive,
endVersion = endVersion,
endInclusive = if (endVersion == this.endVersion) this.endInclusive else other.endInclusive
)
}
infix fun NpmRange.isIntersect(other: NpmRange): Boolean {
if (this.startVersion == null) {
return other.startVersion == null || this.endVersion == null || other.startVersion < this.endVersion ||
(other.startInclusive && this.endInclusive && other.startVersion == this.endVersion)
}
if (other.startVersion == null) {
return other.endVersion == null || this.startVersion < other.endVersion ||
(this.startInclusive && other.endInclusive && this.startVersion == other.endVersion)
}
if (this.endVersion == null) {
return other.endVersion == null || other.endVersion > this.startVersion ||
(this.startInclusive && other.endInclusive && other.endVersion == this.startVersion)
}
if (other.endVersion == null) {
return this.endVersion > other.startVersion ||
(other.startInclusive && this.endInclusive && this.endVersion == other.startVersion)
}
if (other.startVersion < this.startVersion) {
return this.startVersion < other.endVersion ||
(this.startInclusive && other.endInclusive && this.startVersion == other.endVersion)
}
if (this.startVersion < other.startVersion) {
return other.startVersion < this.endVersion ||
(other.startInclusive && this.endInclusive && other.startVersion == this.endVersion)
}
if (this.endVersion < other.endVersion) {
return this.startVersion < other.endVersion ||
(this.startInclusive && other.endInclusive && this.startVersion == other.endVersion)
}
if (this.startVersion < other.startVersion) {
return other.startVersion < this.endVersion ||
(other.startInclusive && this.endInclusive && other.startVersion == this.endVersion)
}
return this.startInclusive && other.startInclusive && this.startVersion == other.startVersion ||
this.startInclusive && other.endInclusive && this.startVersion == other.endVersion ||
this.endInclusive && other.startInclusive && this.endVersion == other.startVersion ||
this.endInclusive && other.endInclusive && this.endVersion == other.endVersion
}
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.gradle.targets.js.npm
import com.github.gundy.semver4j.model.Version
import java.math.BigInteger
data class SemVer(
@@ -170,4 +171,25 @@ private fun String.foldDelimiters(): String {
}
}
return result.toString()
}
fun Version.toSemVer(): SemVer =
SemVer(
major.toBigInteger(),
minor.toBigInteger(),
patch.toBigInteger(),
preRelease = preReleaseIdentifiers.joinToString("."),
build = buildIdentifiers.joinToString(".")
)
fun min(a: SemVer?, b: SemVer?): SemVer? {
if (a == null || b == null) return null
return if (a < b) a else b
}
fun max(a: SemVer?, b: SemVer?): SemVer? {
if (a == null || b == null) return null
return if (a > b) a else b
}