mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Turn on @typescript-eslint/strict-boolean-expressions
- Fix some bugs about bad conditions Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
d3f45bfe49
commit
1675c56e59
165 files changed
+853
-1141
No files matched your search
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import { runInAction } from "mobx";
|
||||
import type { Simplify, ValueOf } from "type-fest";
|
||||
import { inspect } from "util";
|
||||
import { isDefined } from "./type-narrowing";
|
||||
|
||||
@@ -141,3 +142,16 @@ export function toggle<K>(set: Set<K>, key: K): void {
|
||||
export function includes<T>(src: T[], value: T | null | undefined): boolean {
|
||||
return isDefined(value) && src.includes(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to get a value from an object where the key may not exist on the object
|
||||
* @param obj An object to key into
|
||||
* @param key Some key
|
||||
*/
|
||||
export function maybeGet<T extends object>(obj: T, key: string): Simplify<ValueOf<T>> | undefined {
|
||||
if (key in obj) {
|
||||
return (obj as Record<string, ValueOf<T>>)[key];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import { getOrInsert } from "./collection-functions";
|
||||
import type { Tuple } from "./tuple";
|
||||
|
||||
export type Falsy = false | 0 | "" | null | undefined;
|
||||
|
||||
@@ -288,11 +289,30 @@ function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]
|
||||
for (const item of from) {
|
||||
const index = parts.indexOf(item[field]);
|
||||
|
||||
if (index < 0) {
|
||||
continue;
|
||||
res[index]?.push(item);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function chunks<T, ChunkSize extends number>(src: Iterable<T>, size: ChunkSize): Tuple<T, ChunkSize>[] {
|
||||
const res: Tuple<T, ChunkSize>[] = [];
|
||||
const iterating = src[Symbol.iterator]();
|
||||
|
||||
top: for (;;) {
|
||||
const item: T[] = [];
|
||||
|
||||
for (let i = 0; i < size; i += 1) {
|
||||
const result = iterating.next();
|
||||
|
||||
if (result.done === true) {
|
||||
break top;
|
||||
}
|
||||
|
||||
item.push(result.value);
|
||||
}
|
||||
|
||||
res[index].push(item);
|
||||
res.push(item as Tuple<T, ChunkSize>);
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -300,6 +320,7 @@ function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]
|
||||
|
||||
export const iter = {
|
||||
chain,
|
||||
chunks,
|
||||
concat,
|
||||
every,
|
||||
filter,
|
||||
@@ -311,8 +332,8 @@ export const iter = {
|
||||
flatMap,
|
||||
join,
|
||||
map,
|
||||
nFircate,
|
||||
newEmpty,
|
||||
nFircate,
|
||||
nth,
|
||||
reduce,
|
||||
take,
|
||||
|
||||
@@ -22,7 +22,7 @@ type TupleOfImpl<T, N extends number, R extends unknown[]> = R["length"] extends
|
||||
* is empty. Then it returns a tuple with the rest of each of tuples
|
||||
* @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
|
||||
* @returns The tuple of all the sources as soon as at least one of the sources is exhausted
|
||||
*/
|
||||
function zip<T>(...sources: Tuple<T[], 0>): Iterator<Tuple<T, 0>, Tuple<T[], 0>>;
|
||||
function zip<T>(...sources: Tuple<T[], 1>): Iterator<Tuple<T, 1>, Tuple<T[], 1>>;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
import type { ExecException, ExecFileException } from "child_process";
|
||||
import type { IncomingMessage } from "http";
|
||||
import type { Falsy } from "./iter";
|
||||
import type { Tuple } from "./tuple";
|
||||
|
||||
/**
|
||||
* Narrows `val` to include the property `key` (if true is returned)
|
||||
@@ -77,6 +79,18 @@ 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[] {
|
||||
return x.length > len;
|
||||
}
|
||||
|
||||
export function isTruthy<T>(x: T): x is Exclude<T, Falsy> {
|
||||
return !!(x as unknown);
|
||||
}
|
||||
|
||||
export function isFalsy<T>(x: T): x is Extract<T, Falsy> {
|
||||
return !(x as unknown);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if val is of type string
|
||||
* @param val the value to be checked
|
||||
|
||||
Reference in new issue
Block a user