UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

24 lines (23 loc) 709 B
import sanitize from 'sanitize-filename'; const DEFAULT_GROUP = Symbol('undefined'); export class NameDeduper { map = {}; add(name, options) { name = sanitize(name ?? '') || options?.fallback; if (!name) { throw Error('Invalid "name" provided'); } const groupKey = options?.group ?? DEFAULT_GROUP; const match = this.map[groupKey]?.[name]; if (match) { const dedupedName = `${name} (${match})`; this.map[groupKey][name] += 1; return dedupedName; } if (!this.map[groupKey]) { this.map[groupKey] = {}; } this.map[groupKey][name] = 1; return name; } }