1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/utils/iter.ts
Sebastian Malton 998f7aa934
Add the ability to sync kube config files (#2567)
* Add the ability to sync kube config files

- Will update when the files changes

- add KUBECONFIG_SYNC label

- fix rebase and change to addObservableSource

- move UI to user settings

- support shallow folder watching

- add some unit tests for the diff-er

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* responding to review comments

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix tests and add try/catch

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* always sync c&p folder, remove bad rebase

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix preferences

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix settings saving and catalog view

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix synced clusters not connectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* change to non-complete shallow watching

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix sizing

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Catch readStream errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* don't clear UserStore on non-existant preference field, fix unlinking not removing items from source

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* change label to file

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2021-04-30 16:48:20 +03:00

90 lines
2.4 KiB
TypeScript

/**
* Create a new type safe empty Iterable
* @returns An `Iterable` that yields 0 items
*/
export function* newEmpty<T>(): Iterable<T> {
return;
}
/**
* Creates a new `Iterable` that yields at most n items from src.
* Does not modify `src` which can be used later.
* @param src An initial iterator
* @param n The maximum number of elements to take from src. Yields up to the floor of `n` and 0 items if n < 0
*/
export function* take<T>(src: Iterable<T>, n: number): Iterable<T> {
outer: for (let i = 0; i < n; i += 1) {
for (const item of src) {
yield item;
continue outer;
}
// if we are here that means that `src` has been exhausted. Don't bother trying again.
break outer;
}
}
/**
* Creates a new iterator that iterates (lazily) over its input and yields the
* result of `fn` for each item.
* @param src A type that can be iterated over
* @param fn The function that is called for each value
*/
export function* map<T, U>(src: Iterable<T>, fn: (from: T) => U): Iterable<U> {
for (const from of src) {
yield fn(from);
}
}
export function* flatMap<T, U>(src: Iterable<T>, fn: (from: T) => Iterable<U>): Iterable<U> {
for (const from of src) {
yield* fn(from);
}
}
/**
* Creates a new iterator that iterates (lazily) over its input and yields the
* items that return a `truthy` value from `fn`.
* @param src A type that can be iterated over
* @param fn The function that is called for each value
*/
export function* filter<T>(src: Iterable<T>, fn: (from: T) => any): Iterable<T> {
for (const from of src) {
if (fn(from)) {
yield from;
}
}
}
/**
* Creates a new iterator that iterates (lazily) over its input and yields the
* result of `fn` when it is `truthy`
* @param src A type that can be iterated over
* @param fn The function that is called for each value
*/
export function* filterMap<T, U>(src: Iterable<T>, fn: (from: T) => U): Iterable<U> {
for (const from of src) {
const res = fn(from);
if (res) {
yield res;
}
}
}
/**
* Creates a new iterator that iterates (lazily) over its input and yields the
* result of `fn` when it is not null or undefined
* @param src A type that can be iterated over
* @param fn The function that is called for each value
*/
export function* filterMapStrict<T, U>(src: Iterable<T>, fn: (from: T) => U): Iterable<U> {
for (const from of src) {
const res = fn(from);
if (res != null) {
yield res;
}
}
}