Sort pod names by convering sub-parts (#3314)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-10-19 10:36:49 -04:00
1 parent e3e7c620a1
commit 64175b24e0
12 files changed
+427 -44

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
/**
*
* @param sources The source arrays
* @yields A tuple of the next element from each of the sources
* @returns The tuple of all the sources as soon as at least one of the sources is exausted
*/
export function* zipStrict<T, N extends number>(...sources: Tuple<T[], N>): Iterator<Tuple<T, N>, Tuple<T[], N>> {
const maxSafeLength = sources.reduce((prev, cur) => Math.min(prev, cur.length), Number.POSITIVE_INFINITY);
if (!isFinite(maxSafeLength)) {
// There are no sources, thus just return
return [] as Tuple<T[], N>;
}
for (let i = 0; i < maxSafeLength; i += 1) {
yield sources.map(source => source[i]) as Tuple<T, N>;
}
return sources.map(source => source.slice(maxSafeLength)) as Tuple<T[], N>;
}
+2 -1
View File
@@ -60,5 +60,6 @@ export * from "./convertMemory";
export * from "./convertCpu";
import * as iter from "./iter";
import * as array from "./array";
export { iter };
export { iter, array };
+16
View File
@@ -185,3 +185,19 @@ export function reduce<T, R = T>(src: Iterable<T>, reducer: (acc: R, cur: T) =>
export function join(src: Iterable<string>, connector = ","): string {
return reduce(src, (acc, cur) => `${acc}${connector}${cur}`, "");
}
/**
* Iterate through `src` and return `true` if `fn` returns a thruthy value for every yielded value.
* Otherwise, return `false`. This function short circuits.
* @param src The type to be iterated over
* @param fn A function to check each iteration
*/
export function every<T>(src: Iterable<T>, fn: (val: T) => any): boolean {
for (const val of src) {
if (!fn(val)) {
return false;
}
}
return true;
}
+35 -7
View File
@@ -24,16 +24,44 @@ import * as iter from "./iter";
import type { RawHelmChart } from "../k8s-api/endpoints/helm-charts.api";
import logger from "../logger";
export function sortCompare<T>(left: T, right: T): -1 | 0 | 1 {
export enum Ordering {
LESS = -1,
EQUAL = 0,
GREATER = 1,
}
/**
* This function switches the direction of `ordering` if `direction` is `"desc"`
* @param ordering The original ordering (assumed to be an "asc" ordering)
* @param direction The new desired direction
*/
export function rectifyOrdering(ordering: Ordering, direction: "asc" | "desc"): Ordering {
if (direction === "desc") {
return -ordering;
}
return ordering;
}
/**
* An ascending sorting function
* @param left An item from an array
* @param right An item from an array
* @returns The relative ordering in an ascending manner.
* - Less if left < right
* - Equal if left == right
* - Greater if left > right
*/
export function sortCompare<T>(left: T, right: T): Ordering {
if (left < right) {
return -1;
return Ordering.LESS;
}
if (left === right) {
return 0;
return Ordering.EQUAL;
}
return 1;
return Ordering.GREATER;
}
interface ChartVersion {
@@ -41,17 +69,17 @@ interface ChartVersion {
__version?: SemVer;
}
export function sortCompareChartVersions(left: ChartVersion, right: ChartVersion): -1 | 0 | 1 {
export function sortCompareChartVersions(left: ChartVersion, right: ChartVersion): Ordering {
if (left.__version && right.__version) {
return semver.compare(right.__version, left.__version);
}
if (!left.__version && right.__version) {
return 1;
return Ordering.GREATER;
}
if (left.__version && !right.__version) {
return -1;
return Ordering.LESS;
}
return sortCompare(left.version, right.version);