All files / layers / L1 mutationQueue.ts

36.36% Statements 8/22
40% Branches 4/10
50% Functions 5/10
30% Lines 6/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 5842x                   5280x           40x                                           6952x 5474x 5474x                                  
import type { QueryParams } from '../interfaces'

export const pendingDeleteMap = new Map<string, Set<string>>()

export function addPendingDelete(entityPath: string, id: string): void {
  const set = pendingDeleteMap.get(entityPath) ?? new Set()
  set.add(id)
  pendingDeleteMap.set(entityPath, set)
}
 
export function removePendingDelete(entityPath: string, id: string): void {
  pendingDeleteMap.get(entityPath)?.delete(id)
}

export function getPendingDeletes(entityPath: string): Set<string> {
  return pendingDeleteMap.get(entityPath) ?? new Set()
}

export function isMarkedDeleted(entityPath: string, id: string): boolean {
  return pendingDeleteMap.get(entityPath)?.has(id) ?? false
}

export function populatePendingDeletes(
  entries: Array<{ entityPath: string; id: string }>,
): void {
  for (const { entityPath, id } of entries) {
    addPendingDelete(entityPath, id)
  }
}
 
export function getAllPendingDeleteEntries(): Array<{
  entityPath: string
  id: string
}> {
  const entries: Array<{ entityPath: string; id: string }> = []
  for (const [entityPath, ids] of pendingDeleteMap) {
    for (const id of ids) {
      entries.push({ entityPath, id })
    }
  }
  return entries
}
 
export function purgePendingDeletes(entityPath: string): Set<string> {
  const set = pendingDeleteMap.get(entityPath) ?? new Set()
  pendingDeleteMap.delete(entityPath)
  return set
}
 
export function normalizeFilters<T extends QueryParams>(
  filters: T,
): Partial<Record<string, string | number | boolean>> {
  const entries = Object.entries(filters).filter(
    ([, v]) => v !== undefined && v !== '',
  )
  entries.sort(([a], [b]) => a.localeCompare(b))
  return Object.fromEntries(entries)
}