The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/logs/UniqueKeyMaker.ts

25 lines
715 B

// this class generates react-keys that are guaranteed to be unique.
// it will try to use the provided `maybeId`, but if that's a duplicate,
// it will use an index-based key.
// NOTE: it will always add a prefix to the string, this is necessary
// to avoid the problem if your proposed key would conflict with
// the index-based names
export class UniqueKeyMaker {
seen: Set<string>;
count: number;
constructor() {
this.seen = new Set();
this.count = 0;
}
getKey(proposedKey: string) {
this.count += 1;
const maybeKey = `k_${proposedKey}`;
if (this.seen.has(maybeKey)) {
return `i_${this.count}`;
} else {
this.seen.add(maybeKey);
return maybeKey;
}
}
}