-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Meta class and surrounding code
- Loading branch information
1 parent
02e22cb
commit e6bcfdb
Showing
50 changed files
with
1,583 additions
and
983 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
mathesar_ui/src/component-library/common/utils/ImmutableMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
export default class ImmutableMap< | ||
Key extends string | number | boolean | null, | ||
Value, | ||
> { | ||
private map: Map<Key, Value>; | ||
|
||
constructor(i: Iterable<[Key, Value]> = []) { | ||
this.map = new Map(i); | ||
} | ||
|
||
/** | ||
* This method exists to allow us to subclass this class and call the | ||
* constructor of the subclass from within this base class. | ||
* | ||
* If there's a way we can use generics to avoid `any` here, we'd love to | ||
* know. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private getNewInstance(...args: any[]): this { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any | ||
return new (this.constructor as any)(...args) as this; | ||
} | ||
|
||
/** | ||
* The value supplied here will overwrite any value that is already associated | ||
* with `key`. | ||
*/ | ||
with(key: Key, value: Value): this { | ||
const map = new Map(this.map); | ||
map.set(key, value); | ||
return this.getNewInstance(map); | ||
} | ||
|
||
/** | ||
* When the same keys exist in within the entries of this instance and the | ||
* entries supplied, the values from the entries supplied will be used instead | ||
* of the values in this instance. This behavior is consistent with the `with` | ||
* method. | ||
*/ | ||
withEntries(entries: Iterable<[Key, Value]>): this { | ||
const map = new Map(this.map); | ||
[...entries].forEach(([key, value]) => { | ||
map.set(key, value); | ||
}); | ||
return this.getNewInstance(map); | ||
} | ||
|
||
/** | ||
* If `key` already exists, its corresponding value will remain. If `key` does | ||
* not exist, then the value supplied here will be used. | ||
*/ | ||
coalesce(key: Key, value: Value): this { | ||
return this.has(key) ? this : this.with(key, value); | ||
} | ||
|
||
/** | ||
* When the same keys exist in within the entries of this instance and the | ||
* entries supplied, the values from this instance will be used instead of the | ||
* values from the supplied entries. This behavior is consistent with the | ||
* `coalesce` method. | ||
*/ | ||
coalesceEntries(other: Iterable<[Key, Value]>): this { | ||
const map = new Map(this.map); | ||
[...other].forEach(([key, value]) => { | ||
if (!this.has(key)) { | ||
map.set(key, value); | ||
} | ||
}); | ||
return this.getNewInstance(map); | ||
} | ||
|
||
without(key: Key): this { | ||
const map = new Map(this.map); | ||
map.delete(key); | ||
return this.getNewInstance(map); | ||
} | ||
|
||
has(key: Key): boolean { | ||
return this.map.has(key); | ||
} | ||
|
||
get(key: Key): Value | undefined { | ||
return this.map.get(key); | ||
} | ||
|
||
get size(): number { | ||
return this.map.size; | ||
} | ||
|
||
keys(): IterableIterator<Key> { | ||
return this.map.keys(); | ||
} | ||
|
||
values(): IterableIterator<Value> { | ||
return this.map.values(); | ||
} | ||
|
||
entries(): IterableIterator<[Key, Value]> { | ||
return this.map.entries(); | ||
} | ||
|
||
[Symbol.iterator](): IterableIterator<[Key, Value]> { | ||
return this.entries(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function hasLabelProperty(v: unknown): v is { label: unknown } { | ||
return typeof v === 'object' && v !== null && 'label' in v; | ||
} | ||
|
||
export function hasStringLabelProperty(v: unknown): v is { label: string } { | ||
return hasLabelProperty(v) && typeof v.label === 'string'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.