Skip to content

Instantly share code, notes, and snippets.

@TheLucifurry
Created September 13, 2024 06:54
Show Gist options
  • Select an option

  • Save TheLucifurry/9776a964951f924b4643e26dede6846f to your computer and use it in GitHub Desktop.

Select an option

Save TheLucifurry/9776a964951f924b4643e26dede6846f to your computer and use it in GitHub Desktop.
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