[Gradle, JS] Small refactoring
^KT-41054 fixed
This commit is contained in:
+36
-65
@@ -44,17 +44,11 @@ data class NpmRange(
|
|||||||
}
|
}
|
||||||
|
|
||||||
infix fun NpmRange.union(other: NpmRange): List<NpmRange> {
|
infix fun NpmRange.union(other: NpmRange): List<NpmRange> {
|
||||||
if (!isIntersect(other)) return listOf(this, other)
|
if (!hasIntersection(other)) return listOf(this, other)
|
||||||
|
|
||||||
val startVersion = when {
|
val startVersion = minStart(this, other)
|
||||||
this.startVersion == null || other.startVersion == null -> null
|
|
||||||
else -> min(this.startVersion, other.startVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
val endVersion = when {
|
val endVersion = maxEnd(this, other)
|
||||||
this.endVersion == null || other.endVersion == null -> null
|
|
||||||
else -> max(this.endVersion, other.endVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
return NpmRange(
|
return NpmRange(
|
||||||
startVersion = startVersion,
|
startVersion = startVersion,
|
||||||
@@ -96,19 +90,11 @@ infix fun List<NpmRange>.intersect(others: List<NpmRange>): List<NpmRange> = fla
|
|||||||
}
|
}
|
||||||
|
|
||||||
infix fun NpmRange.intersect(other: NpmRange): NpmRange? {
|
infix fun NpmRange.intersect(other: NpmRange): NpmRange? {
|
||||||
if (!isIntersect(other)) return null
|
if (!hasIntersection(other)) return null
|
||||||
|
|
||||||
val startVersion = when {
|
val startVersion = maxStart(this, other)
|
||||||
startVersion == null -> other.startVersion
|
|
||||||
other.startVersion == null -> startVersion
|
|
||||||
else -> max(this.startVersion, other.startVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
val endVersion = when {
|
val endVersion = minEnd(this, other)
|
||||||
endVersion == null -> other.endVersion
|
|
||||||
other.endVersion == null -> endVersion
|
|
||||||
else -> min(this.endVersion, other.endVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
return NpmRange(
|
return NpmRange(
|
||||||
startVersion = startVersion,
|
startVersion = startVersion,
|
||||||
@@ -118,49 +104,34 @@ infix fun NpmRange.intersect(other: NpmRange): NpmRange? {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun NpmRange.isIntersect(other: NpmRange): Boolean {
|
infix fun NpmRange.hasIntersection(other: NpmRange): Boolean {
|
||||||
if (this.startVersion == null) {
|
val maxStart = maxStart(this, other)
|
||||||
return other.startVersion == null || this.endVersion == null || other.startVersion < this.endVersion ||
|
val minEnd = minEnd(this, other)
|
||||||
(other.startInclusive && this.endInclusive && other.startVersion == this.endVersion)
|
return maxStart == null || minEnd == null || maxStart < minEnd
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun maxStart(a: NpmRange, b: NpmRange): SemVer? =
|
||||||
|
when {
|
||||||
|
a.startVersion == null -> b.startVersion
|
||||||
|
b.startVersion == null -> a.startVersion
|
||||||
|
else -> max(a.startVersion, b.startVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun minStart(a: NpmRange, b: NpmRange): SemVer? =
|
||||||
|
when {
|
||||||
|
a.startVersion == null || b.startVersion == null -> null
|
||||||
|
else -> min(a.startVersion, b.startVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun maxEnd(a: NpmRange, b: NpmRange): SemVer? =
|
||||||
|
when {
|
||||||
|
a.endVersion == null || b.endVersion == null -> null
|
||||||
|
else -> max(a.endVersion, b.endVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun minEnd(a: NpmRange, b: NpmRange): SemVer? =
|
||||||
|
when {
|
||||||
|
a.endVersion == null -> b.endVersion
|
||||||
|
b.endVersion == null -> a.endVersion
|
||||||
|
else -> min(a.endVersion, b.endVersion)
|
||||||
|
}
|
||||||
+10
-10
@@ -62,16 +62,16 @@ fun buildNpmVersion(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun NpmRange.caretizeSingleVersion(): NpmRange {
|
private fun NpmRange.caretizeSingleVersion(): NpmRange {
|
||||||
if (startVersion?.toVersion() != endVersion?.toVersion() || !(startInclusive || endInclusive)) {
|
if (startVersion?.toVersion() == endVersion?.toVersion() && (startInclusive || endInclusive)) {
|
||||||
return this
|
return NpmRange(
|
||||||
|
startVersion = startVersion,
|
||||||
|
startInclusive = startInclusive,
|
||||||
|
endVersion = startVersion
|
||||||
|
?.toVersion()
|
||||||
|
?.incrementMajor()
|
||||||
|
?.toSemVer()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return NpmRange(
|
return this
|
||||||
startVersion = startVersion,
|
|
||||||
startInclusive = startInclusive,
|
|
||||||
endVersion = startVersion
|
|
||||||
?.toVersion()
|
|
||||||
?.incrementMajor()
|
|
||||||
?.toSemVer()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user