Files
hnh-map/frontend-nuxt/lib/UniqueList.ts
Nikolay Tatarinov 6529d7370e Add configuration files and update project documentation
- Introduced .editorconfig for consistent coding styles across the project.
- Added .golangci.yml for Go linting configuration.
- Updated AGENTS.md to clarify project structure and components.
- Enhanced CONTRIBUTING.md with Makefile usage for common tasks.
- Updated Dockerfiles to use Go 1.24 and improved build instructions.
- Refined README.md and deployment documentation for clarity.
- Added testing documentation in testing.md for backend and frontend tests.
- Introduced Makefile for streamlined development commands and tasks.
2026-03-01 01:51:47 +03:00

53 lines
1.6 KiB
TypeScript

export interface Identifiable {
id: number | string
}
export interface UniqueList<T extends Identifiable> {
elements: Record<string, T>
}
export function createUniqueList<T extends Identifiable>(): UniqueList<T> {
return { elements: {} }
}
export function uniqueListUpdate<T extends Identifiable>(
list: UniqueList<T>,
dataList: T[],
addCallback?: (it: T) => void,
removeCallback?: (it: T) => void,
updateCallback?: (oldElement: T, newElement: T) => void
): void {
const elementsToAdd = dataList.filter((it) => list.elements[String(it.id)] === undefined)
const elementsToRemove: T[] = []
for (const id of Object.keys(list.elements)) {
if (dataList.find((up) => String(up.id) === id) === undefined) {
const el = list.elements[id]
if (el) elementsToRemove.push(el)
}
}
if (removeCallback) {
elementsToRemove.forEach((it) => removeCallback(it))
}
if (updateCallback) {
dataList.forEach((newElement) => {
const oldElement = list.elements[String(newElement.id)]
if (oldElement) {
updateCallback(oldElement, newElement)
}
})
}
if (addCallback) {
elementsToAdd.forEach((it) => addCallback(it))
}
elementsToRemove.forEach((it) => delete list.elements[String(it.id)])
elementsToAdd.forEach((it) => (list.elements[String(it.id)] = it))
}
export function uniqueListGetElements<T extends Identifiable>(list: UniqueList<T>): T[] {
return Object.values(list.elements)
}
export function uniqueListById<T extends Identifiable>(list: UniqueList<T>, id: number | string): T | undefined {
return list.elements[String(id)]
}