[Gradle, JS] Small refactoring

^KT-41054 fixed
This commit is contained in:
Ilya Goncharov
2020-08-06 12:04:02 +03:00
parent b95eb66032
commit d4524e4050
2 changed files with 41 additions and 70 deletions
@@ -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
}
fun maxStart(a: NpmRange, b: NpmRange): SemVer? =
when {
a.startVersion == null -> b.startVersion
b.startVersion == null -> a.startVersion
else -> max(a.startVersion, b.startVersion)
} }
if (other.startVersion == null) { fun minStart(a: NpmRange, b: NpmRange): SemVer? =
return other.endVersion == null || this.startVersion < other.endVersion || when {
(this.startInclusive && other.endInclusive && this.startVersion == other.endVersion) a.startVersion == null || b.startVersion == null -> null
else -> min(a.startVersion, b.startVersion)
} }
if (this.endVersion == null) { fun maxEnd(a: NpmRange, b: NpmRange): SemVer? =
return other.endVersion == null || other.endVersion > this.startVersion || when {
(this.startInclusive && other.endInclusive && other.endVersion == this.startVersion) a.endVersion == null || b.endVersion == null -> null
else -> max(a.endVersion, b.endVersion)
} }
if (other.endVersion == null) { fun minEnd(a: NpmRange, b: NpmRange): SemVer? =
return this.endVersion > other.startVersion || when {
(other.startInclusive && this.endInclusive && this.endVersion == other.startVersion) a.endVersion == null -> b.endVersion
} b.endVersion == null -> a.endVersion
else -> min(a.endVersion, b.endVersion)
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
}
@@ -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()
)
} }