mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Split UserManagement into seperate Cluster and Namespace Roles and RoleBindings (#2755)
This commit is contained in:
1 parent
1ed7892b26
commit
a1a1c240e9
69 files changed
+2896
-901
No files matched your search
@@ -0,0 +1,528 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { HashSet, ObservableHashSet } from "../hash-set";
|
||||
|
||||
describe("ObservableHashSet<T>", () => {
|
||||
it("should not throw on creation", () => {
|
||||
expect(() => new ObservableHashSet<{ a: number }>([], item => item.a.toString())).not.toThrowError();
|
||||
});
|
||||
|
||||
it("should be initializable", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
});
|
||||
|
||||
it("has should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
it("forEach should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
let a = 1;
|
||||
|
||||
res.forEach((item) => {
|
||||
expect(item.a).toEqual(a++);
|
||||
});
|
||||
});
|
||||
|
||||
it("delete should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
expect(res.delete({ a: 1 })).toBe(true);
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
expect(res.delete({ a: 5 })).toBe(false);
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
it("toggle should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
res.toggle({ a: 1 });
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
|
||||
expect(res.has({ a: 6 })).toBe(false);
|
||||
res.toggle({ a: 6 });
|
||||
expect(res.has({ a: 6 })).toBe(true);
|
||||
});
|
||||
|
||||
it("add should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 6 })).toBe(false);
|
||||
res.add({ a: 6 });
|
||||
expect(res.has({ a: 6 })).toBe(true);
|
||||
});
|
||||
|
||||
it("add should treat the hash to be the same as equality", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1, foobar: "hello" },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
res.add({ a: 1, foobar: "goodbye" });
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
});
|
||||
|
||||
it("clear should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
res.clear();
|
||||
expect(res.size).toBe(0);
|
||||
});
|
||||
|
||||
it("replace should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
res.replace([{ a: 13 }]);
|
||||
expect(res.size).toBe(1);
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
expect(res.has({ a: 13 })).toBe(true);
|
||||
});
|
||||
|
||||
it("toJSON should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.toJSON()).toStrictEqual([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("values should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.values();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 1 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 2 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 3 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 4 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("keys should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.keys();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 1 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 2 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 3 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 4 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("entries should work as expected", () => {
|
||||
const res = new ObservableHashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.entries();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 1 }, { a: 1 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 2 }, { a: 2 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 3 }, { a: 3 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 4 }, { a: 4 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("HashSet<T>", () => {
|
||||
it("should not throw on creation", () => {
|
||||
expect(() => new HashSet<{ a: number }>([], item => item.a.toString())).not.toThrowError();
|
||||
});
|
||||
|
||||
it("should be initializable", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
});
|
||||
|
||||
it("has should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
it("forEach should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
let a = 1;
|
||||
|
||||
res.forEach((item) => {
|
||||
expect(item.a).toEqual(a++);
|
||||
});
|
||||
});
|
||||
|
||||
it("delete should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
expect(res.delete({ a: 1 })).toBe(true);
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
expect(res.delete({ a: 5 })).toBe(false);
|
||||
expect(res.has({ a: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
it("toggle should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
res.toggle({ a: 1 });
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
|
||||
expect(res.has({ a: 6 })).toBe(false);
|
||||
res.toggle({ a: 6 });
|
||||
expect(res.has({ a: 6 })).toBe(true);
|
||||
});
|
||||
|
||||
it("add should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 6 })).toBe(false);
|
||||
res.add({ a: 6 });
|
||||
expect(res.has({ a: 6 })).toBe(true);
|
||||
});
|
||||
|
||||
it("add should treat the hash to be the same as equality", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1, foobar: "hello" },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
res.add({ a: 1, foobar: "goodbye" });
|
||||
expect(res.has({ a: 1 })).toBe(true);
|
||||
});
|
||||
|
||||
it("clear should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
res.clear();
|
||||
expect(res.size).toBe(0);
|
||||
});
|
||||
|
||||
it("replace should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.size).toBe(4);
|
||||
res.replace([{ a: 13 }]);
|
||||
expect(res.size).toBe(1);
|
||||
expect(res.has({ a: 1 })).toBe(false);
|
||||
expect(res.has({ a: 13 })).toBe(true);
|
||||
});
|
||||
|
||||
it("toJSON should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
|
||||
expect(res.toJSON()).toStrictEqual([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("values should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.values();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 1 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 2 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 3 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 4 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("keys should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.keys();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 1 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 2 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 3 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: { a: 4 },
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("entries should work as expected", () => {
|
||||
const res = new HashSet([
|
||||
{ a: 1 },
|
||||
{ a: 2 },
|
||||
{ a: 3 },
|
||||
{ a: 4 },
|
||||
], item => item.a.toString());
|
||||
const iter = res.entries();
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 1 }, { a: 1 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 2 }, { a: 2 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 3 }, { a: 3 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: [{ a: 4 }, { a: 4 }],
|
||||
done: false,
|
||||
});
|
||||
|
||||
expect(iter.next()).toStrictEqual({
|
||||
value: undefined,
|
||||
done: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { nFircate } from "../n-fircate";
|
||||
|
||||
describe("nFircate", () => {
|
||||
it("should produce an empty array if no parts are provided", () => {
|
||||
expect(nFircate([{ a: 1 }, { a: 2 }], "a", []).length).toBe(0);
|
||||
});
|
||||
|
||||
it("should ignore non-matching parts", () => {
|
||||
const res = nFircate([{ a: 1 }, { a: 2 }], "a", [1]);
|
||||
|
||||
expect(res.length).toBe(1);
|
||||
expect(res[0].length).toBe(1);
|
||||
});
|
||||
|
||||
it("should include all matching parts in each type", () => {
|
||||
const res = nFircate([{ a: 1, b: "a" }, { a: 2, b: "b" }, { a: 1, b: "c" }], "a", [1, 2]);
|
||||
|
||||
expect(res.length).toBe(2);
|
||||
expect(res[0].length).toBe(2);
|
||||
expect(res[0][0].b).toBe("a");
|
||||
expect(res[0][1].b).toBe("c");
|
||||
expect(res[1].length).toBe(1);
|
||||
expect(res[1][0].b).toBe("b");
|
||||
});
|
||||
|
||||
it("should throw a type error if the same part is provided more than once", () => {
|
||||
try {
|
||||
nFircate([{ a: 1, b: "a" }, { a: 2, b: "b" }, { a: 1, b: "c" }], "a", [1, 2, 1]);
|
||||
fail("Expected error");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(TypeError);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { action, IInterceptable, IInterceptor, IListenable, ISetWillChange, observable, ObservableMap, ObservableSet } from "mobx";
|
||||
|
||||
export function makeIterableIterator<T>(iterator: Iterator<T>): IterableIterator<T> {
|
||||
(iterator as IterableIterator<T>)[Symbol.iterator] = () => iterator as IterableIterator<T>;
|
||||
|
||||
return iterator as IterableIterator<T>;
|
||||
}
|
||||
|
||||
export class HashSet<T> implements Set<T> {
|
||||
#hashmap: Map<string, T>;
|
||||
|
||||
constructor(initialValues: Iterable<T>, protected hasher: (item: T) => string) {
|
||||
this.#hashmap = new Map<string, T>(Array.from(initialValues, value => [this.hasher(value), value]));
|
||||
}
|
||||
|
||||
replace(other: ObservableHashSet<T> | ObservableSet<T> | Set<T> | readonly T[]): this {
|
||||
if (other === null || other === undefined) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!(Array.isArray(other) || other instanceof Set || other instanceof ObservableHashSet || other instanceof ObservableSet)) {
|
||||
throw new Error(`ObservableHashSet: Cannot initialize set from ${other}`);
|
||||
}
|
||||
|
||||
this.clear();
|
||||
|
||||
for (const value of other) {
|
||||
this.add(value);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.#hashmap.clear();
|
||||
}
|
||||
|
||||
add(value: T): this {
|
||||
this.#hashmap.set(this.hasher(value), value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
toggle(value: T): void {
|
||||
const hash = this.hasher(value);
|
||||
|
||||
if (this.#hashmap.has(hash)) {
|
||||
this.#hashmap.delete(hash);
|
||||
} else {
|
||||
this.#hashmap.set(hash, value);
|
||||
}
|
||||
}
|
||||
|
||||
delete(value: T): boolean {
|
||||
return this.#hashmap.delete(this.hasher(value));
|
||||
}
|
||||
|
||||
forEach(callbackfn: (value: T, key: T, set: Set<T>) => void, thisArg?: any): void {
|
||||
this.#hashmap.forEach(value => callbackfn(value, value, thisArg ?? this));
|
||||
}
|
||||
|
||||
has(value: T): boolean {
|
||||
return this.#hashmap.has(this.hasher(value));
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
keys(): IterableIterator<T> {
|
||||
return this.values();
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
return this.#hashmap.values();
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag](): string {
|
||||
return "Set";
|
||||
}
|
||||
|
||||
toJSON(): T[] {
|
||||
return Array.from(this);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object Set]";
|
||||
}
|
||||
}
|
||||
|
||||
export class ObservableHashSet<T> implements Set<T>, IInterceptable<ISetWillChange>, IListenable {
|
||||
#hashmap: ObservableMap<string, T>;
|
||||
|
||||
get interceptors_(): IInterceptor<ISetWillChange<T>>[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
get changeListeners_(): Function[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
constructor(initialValues: Iterable<T>, protected hasher: (item: T) => string) {
|
||||
this.#hashmap = observable.map<string, T>(Array.from(initialValues, value => [this.hasher(value), value]), undefined);
|
||||
}
|
||||
|
||||
@action
|
||||
replace(other: ObservableHashSet<T> | ObservableSet<T> | Set<T> | readonly T[]): this {
|
||||
if (other === null || other === undefined) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!(Array.isArray(other) || other instanceof Set || other instanceof ObservableHashSet || other instanceof ObservableSet)) {
|
||||
throw new Error(`ObservableHashSet: Cannot initialize set from ${other}`);
|
||||
}
|
||||
|
||||
this.clear();
|
||||
|
||||
for (const value of other) {
|
||||
this.add(value);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.#hashmap.clear();
|
||||
}
|
||||
|
||||
add(value: T): this {
|
||||
this.#hashmap.set(this.hasher(value), value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@action
|
||||
toggle(value: T): void {
|
||||
const hash = this.hasher(value);
|
||||
|
||||
if (this.#hashmap.has(hash)) {
|
||||
this.#hashmap.delete(hash);
|
||||
} else {
|
||||
this.#hashmap.set(hash, value);
|
||||
}
|
||||
}
|
||||
|
||||
delete(value: T): boolean {
|
||||
return this.#hashmap.delete(this.hasher(value));
|
||||
}
|
||||
|
||||
forEach(callbackfn: (value: T, key: T, set: Set<T>) => void, thisArg?: any): void {
|
||||
this.#hashmap.forEach(value => callbackfn(value, value, thisArg ?? this));
|
||||
}
|
||||
|
||||
has(value: T): boolean {
|
||||
return this.#hashmap.has(this.hasher(value));
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
keys(): IterableIterator<T> {
|
||||
return this.values();
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
return this.#hashmap.values();
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag](): string {
|
||||
return "Set";
|
||||
}
|
||||
|
||||
toJSON(): T[] {
|
||||
return Array.from(this);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object ObservableSet]";
|
||||
}
|
||||
}
|
||||
@@ -29,17 +29,17 @@ export * from "./app-version";
|
||||
export * from "./autobind";
|
||||
export * from "./base64";
|
||||
export * from "./camelCase";
|
||||
export * from "./toJS";
|
||||
export * from "./cloneJson";
|
||||
export * from "./debouncePromise";
|
||||
export * from "./defineGlobal";
|
||||
export * from "./delay";
|
||||
export * from "./disposer";
|
||||
export * from "./disposer";
|
||||
export * from "./downloadFile";
|
||||
export * from "./escapeRegExp";
|
||||
export * from "./extended-map";
|
||||
export * from "./getRandId";
|
||||
export * from "./hash-set";
|
||||
export * from "./n-fircate";
|
||||
export * from "./openExternal";
|
||||
export * from "./paths";
|
||||
export * from "./reject-promise";
|
||||
@@ -47,6 +47,7 @@ export * from "./singleton";
|
||||
export * from "./splitArray";
|
||||
export * from "./tar";
|
||||
export * from "./toggle-set";
|
||||
export * from "./toJS";
|
||||
export * from "./type-narrowing";
|
||||
|
||||
import * as iter from "./iter";
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Split an iterable into several arrays with matching fields
|
||||
* @param from The iterable of items to split up
|
||||
* @param field The field of each item to split over
|
||||
* @param parts What each array will be filtered to
|
||||
* @returns A `parts.length` tuple of `T[]` where each array has matching `field` values
|
||||
*/
|
||||
export function nFircate<T>(from: Iterable<T>, field: keyof T, parts: []): [];
|
||||
export function nFircate<T>(from: Iterable<T>, field: keyof T, parts: [T[typeof field]]): [T[]];
|
||||
export function nFircate<T>(from: Iterable<T>, field: keyof T, parts: [T[typeof field], T[typeof field]]): [T[], T[]];
|
||||
export function nFircate<T>(from: Iterable<T>, field: keyof T, parts: [T[typeof field], T[typeof field], T[typeof field]]): [T[], T[], T[]];
|
||||
|
||||
export function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]): T[][] {
|
||||
if (new Set(parts).size !== parts.length) {
|
||||
throw new TypeError("Duplicate parts entries");
|
||||
}
|
||||
|
||||
const res = Array.from(parts, () => [] as T[]);
|
||||
|
||||
for (const item of from) {
|
||||
const index = parts.indexOf(item[field]);
|
||||
|
||||
if (index < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
res[index].push(item);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
Reference in new issue
Block a user