mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Fixup remaining type errors from core
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
894553cdea
commit
271f8860e2
72 files changed
+345
-278
No files matched your search
@@ -5,6 +5,7 @@
|
||||
|
||||
import assert from "assert";
|
||||
import { iter } from "./iter";
|
||||
import { TypedRegEx } from "typed-regex";
|
||||
|
||||
// Helper to convert memory from units Ki, Mi, Gi, Ti, Pi to bytes and vise versa
|
||||
|
||||
@@ -18,25 +19,25 @@ const magnitudes = new Map([
|
||||
["TiB", baseMagnitude ** 4] as const,
|
||||
maxMagnitude,
|
||||
]);
|
||||
const unitRegex = /(?<value>[0-9]+(\.[0-9]*)?)(?<suffix>(B|[KMGTP]iB?))?/;
|
||||
const unitRegex = TypedRegEx("^(?<value>\\d+(\\.\\d+)?)\\s*(?<suffix>B|KiB|MiB|GiB|TiB|PiB)?$");
|
||||
|
||||
type BinaryUnit = typeof magnitudes extends Map<infer Key, any> ? Key : never;
|
||||
|
||||
export function unitsToBytes(value: string): number {
|
||||
const unitsMatch = value.match(unitRegex);
|
||||
const unitsMatch = unitRegex.captures(value.trim());
|
||||
|
||||
if (!unitsMatch?.groups) {
|
||||
if (!unitsMatch?.value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const parsedValue = parseFloat(unitsMatch.groups.value);
|
||||
const parsedValue = parseFloat(unitsMatch.value);
|
||||
|
||||
if (!unitsMatch.groups?.suffix) {
|
||||
if (!unitsMatch.suffix) {
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
const magnitude = magnitudes.get(unitsMatch.groups.suffix as BinaryUnit)
|
||||
?? magnitudes.get(`${unitsMatch.groups.suffix}B` as BinaryUnit);
|
||||
const magnitude = magnitudes.get(unitsMatch.suffix as BinaryUnit)
|
||||
?? magnitudes.get(`${unitsMatch.suffix}B` as BinaryUnit);
|
||||
|
||||
assert(magnitude, "UnitRegex is wrong some how");
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ export function formatDuration(timeValue: number, compact = true) {
|
||||
|
||||
function getMeaningfulValues(values: number[], suffixes: string[], separator = " ") {
|
||||
return values
|
||||
.map((a, i): [number, string] => [a, suffixes[i]])
|
||||
.map((a, i) => [a, suffixes[i]] as [number, string])
|
||||
.filter(([dur]) => dur > 0)
|
||||
.map(([dur, suf]) => dur + suf)
|
||||
.join(separator);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import type { IInterceptable, IInterceptor, IListenable, ISetWillChange, ObservableMap } from "mobx";
|
||||
import { observable, ObservableSet, runInAction } from "mobx";
|
||||
import { iter } from "./iter";
|
||||
|
||||
export function makeIterableIterator<T>(iterator: Iterator<T>): IterableIterator<T> {
|
||||
(iterator as IterableIterator<T>)[Symbol.iterator] = () => iterator as IterableIterator<T>;
|
||||
@@ -73,20 +74,8 @@ export class HashSet<T> implements Set<T> {
|
||||
return this.#hashmap.size;
|
||||
}
|
||||
|
||||
entries(): IterableIterator<[T, T]> {
|
||||
let nextIndex = 0;
|
||||
const keys = Array.from(this.keys());
|
||||
const values = Array.from(this.values());
|
||||
|
||||
return makeIterableIterator<[T, T]>({
|
||||
next() {
|
||||
const index = nextIndex++;
|
||||
|
||||
return index < values.length
|
||||
? { value: [keys[index], values[index]], done: false }
|
||||
: { done: true, value: undefined };
|
||||
},
|
||||
});
|
||||
entries() {
|
||||
return iter.zip(this.keys(), this.values());
|
||||
}
|
||||
|
||||
keys(): IterableIterator<T> {
|
||||
@@ -94,16 +83,7 @@ export class HashSet<T> implements Set<T> {
|
||||
}
|
||||
|
||||
values(): IterableIterator<T> {
|
||||
let nextIndex = 0;
|
||||
const observableValues = Array.from(this.#hashmap.values());
|
||||
|
||||
return makeIterableIterator<T>({
|
||||
next: () => {
|
||||
return nextIndex < observableValues.length
|
||||
? { value: observableValues[nextIndex++], done: false }
|
||||
: { done: true, value: undefined };
|
||||
},
|
||||
});
|
||||
return this.#hashmap.values();
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
@@ -197,19 +177,7 @@ export class ObservableHashSet<T> implements Set<T>, IInterceptable<ISetWillChan
|
||||
}
|
||||
|
||||
entries(): IterableIterator<[T, T]> {
|
||||
let nextIndex = 0;
|
||||
const keys = Array.from(this.keys());
|
||||
const values = Array.from(this.values());
|
||||
|
||||
return makeIterableIterator<[T, T]>({
|
||||
next() {
|
||||
const index = nextIndex++;
|
||||
|
||||
return index < values.length
|
||||
? { value: [keys[index], values[index]], done: false }
|
||||
: { done: true, value: undefined };
|
||||
},
|
||||
});
|
||||
return iter.zip(this.keys(), this.values());
|
||||
}
|
||||
|
||||
keys(): IterableIterator<T> {
|
||||
@@ -217,16 +185,7 @@ export class ObservableHashSet<T> implements Set<T>, IInterceptable<ISetWillChan
|
||||
}
|
||||
|
||||
values(): IterableIterator<T> {
|
||||
let nextIndex = 0;
|
||||
const observableValues = Array.from(this.#hashmap.values());
|
||||
|
||||
return makeIterableIterator<T>({
|
||||
next: () => {
|
||||
return nextIndex < observableValues.length
|
||||
? { value: observableValues[nextIndex++], done: false }
|
||||
: { done: true, value: undefined };
|
||||
},
|
||||
});
|
||||
return this.#hashmap.values();
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { getOrInsert } from "./collection-functions";
|
||||
import type { Tuple } from "./tuple";
|
||||
import type { ReadonlyTuple, Tuple } from "./tuple";
|
||||
|
||||
export type Falsy = false | 0 | "" | null | undefined;
|
||||
|
||||
@@ -295,8 +295,13 @@ function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]
|
||||
return res;
|
||||
}
|
||||
|
||||
function chunks<T, ChunkSize extends number>(src: Iterable<T>, size: ChunkSize): Tuple<T, ChunkSize>[] {
|
||||
const res: Tuple<T, ChunkSize>[] = [];
|
||||
/**
|
||||
*
|
||||
* @param src The source iterable to chunk over
|
||||
* @param size The size of each chunk
|
||||
* @returns An iterator over the chunks of `src`
|
||||
*/
|
||||
function* chunks<T, ChunkSize extends number>(src: Iterable<T>, size: ChunkSize): IterableIterator<Tuple<T, ChunkSize>> {
|
||||
const iterating = src[Symbol.iterator]();
|
||||
|
||||
top: for (;;) {
|
||||
@@ -312,10 +317,33 @@ function chunks<T, ChunkSize extends number>(src: Iterable<T>, size: ChunkSize):
|
||||
item.push(result.value);
|
||||
}
|
||||
|
||||
res.push(item as Tuple<T, ChunkSize>);
|
||||
yield item as Tuple<T, ChunkSize>;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
function zip<T, Count extends 0>(...sources: readonly []): IterableIterator<[]>;
|
||||
function zip<T, Count extends 1>(...sources: readonly [Iterable<T>]): IterableIterator<[T]>;
|
||||
function zip<T, Count extends 2>(...sources: readonly [Iterable<T>, Iterable<T>]): IterableIterator<[T, T]>;
|
||||
function zip<T, Count extends 3>(...sources: readonly [Iterable<T>, Iterable<T>, Iterable<T>]): IterableIterator<[T, T, T]>;
|
||||
|
||||
function* zip<T, Count extends number>(...sources: ReadonlyTuple<Iterable<T>, Count>): IterableIterator<Tuple<T, Count>> {
|
||||
const iterating = sources.map((iter) => iter[Symbol.iterator]());
|
||||
|
||||
for (;;) {
|
||||
const item: T[] = [];
|
||||
|
||||
for (const iter of iterating) {
|
||||
const result = iter.next();
|
||||
|
||||
if (result.done === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
item.push(result.value);
|
||||
}
|
||||
|
||||
yield item as Tuple<T, Count>;
|
||||
}
|
||||
}
|
||||
|
||||
export const iter = {
|
||||
@@ -337,4 +365,5 @@ export const iter = {
|
||||
nth,
|
||||
reduce,
|
||||
take,
|
||||
zip,
|
||||
};
|
||||
@@ -40,7 +40,7 @@ export function convertKubectlJsonPathToNodeJsonPath(jsonPath: string) {
|
||||
let { pathExpression } = captures;
|
||||
|
||||
if (pathExpression.match(slashDashSearch)) {
|
||||
const [first, ...rest] = pathExpression.split(pathByBareDots);
|
||||
const [first, ...rest] = pathExpression.split(pathByBareDots) as [string, ...string[]];
|
||||
|
||||
pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,10 @@ function parseKeyDownDescriptor(descriptor: string): (event: KeyboardEvent) => b
|
||||
throw new Error("only single key combinations are currently supported");
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new Error("no key specified");
|
||||
}
|
||||
|
||||
return (event) => {
|
||||
return event.altKey === hasAlt
|
||||
&& event.shiftKey === hasShift
|
||||
|
||||
@@ -17,6 +17,18 @@ type TupleOfImpl<T, N extends number, R extends unknown[]> = R["length"] extends
|
||||
? R
|
||||
: TupleOfImpl<T, N, [T, ...R]>;
|
||||
|
||||
/**
|
||||
* A strict N-tuple of type T
|
||||
*/
|
||||
export type ReadonlyTuple<T, N extends number> = N extends N
|
||||
? number extends N
|
||||
? T[]
|
||||
: ReadonlyTupleOfImpl<T, N, []>
|
||||
: never;
|
||||
type ReadonlyTupleOfImpl<T, N extends number, R extends readonly unknown[]> = R["length"] extends N
|
||||
? R
|
||||
: ReadonlyTupleOfImpl<T, N, readonly [T, ...R]>;
|
||||
|
||||
/**
|
||||
* Iterates over `sources` yielding full tuples until one of the tuple arrays
|
||||
* is empty. Then it returns a tuple with the rest of each of tuples
|
||||
|
||||
@@ -79,15 +79,15 @@ export function isTypedArray<T>(val: unknown, isEntry: (entry: unknown) => entry
|
||||
return Array.isArray(val) && val.every(isEntry);
|
||||
}
|
||||
|
||||
export function hasLengthGreaterThan<T, Len extends number>(x: T[], len: Len): x is Tuple<T, Len> & T[] {
|
||||
export function hasLengthAtLeast<T, Len extends number>(x: T[], len: Len): x is Tuple<T, Len> & T[] {
|
||||
return x.length > len;
|
||||
}
|
||||
|
||||
export function isTruthy<T>(x: T): x is Exclude<T, Falsy> {
|
||||
export function isTruthy<T>(x: T): x is Exclude<T, Falsy | void> {
|
||||
return !!(x as unknown);
|
||||
}
|
||||
|
||||
export function isFalsy<T>(x: T): x is Extract<T, Falsy> {
|
||||
export function isFalsy<T>(x: T): x is Extract<T, Falsy | void> {
|
||||
return !(x as unknown);
|
||||
}
|
||||
|
||||
@@ -122,6 +122,10 @@ export function isBoolean(val: unknown): val is boolean {
|
||||
return typeof val === "boolean";
|
||||
}
|
||||
|
||||
export function extractObject<T>(val: T): val is Extract<T, object> {
|
||||
return typeof val === "object" && val !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if val is of type object and isn't null
|
||||
* @param val the value to be checked
|
||||
|
||||
Reference in new issue
Block a user