Created
September 13, 2024 06:54
-
-
Save TheLucifurry/9776a964951f924b4643e26dede6846f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export class Autoincrement { | |
| value!: number | |
| constructor(readonly initialValue = 0) { | |
| this.set(initialValue) | |
| } | |
| set = (newValue: number) => { | |
| this.value = newValue | |
| } | |
| next = () => this.value++ | |
| // reset = () => { | |
| // this.value = this.initialValue | |
| // } | |
| // exclude = (maxExistingId: number) => { | |
| // if (maxExistingId > this.value) | |
| // this.set(maxExistingId) | |
| // } | |
| } | |
| export class AutoincrementMap<Key> { | |
| map = new Map<Key, Autoincrement>() | |
| constructor(readonly initialValue = 0) {} | |
| nextFor(key: Key) { | |
| if (!this.map.has(key)) | |
| this.map.set(key, new Autoincrement(this.initialValue)) | |
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
| const itemAutoincrement = this.map.get(key)! | |
| return itemAutoincrement.next() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment