@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
24 lines (22 loc) • 736 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.assignId = assignId;
var _nanoid = require("nanoid");
// `WeakMap`s require the first type param to extend `object`
// eslint-disable-next-line @typescript-eslint/ban-types
var randomIdCache = new WeakMap();
/**
* given an object, this function randomly generates an ID and returns it. this
* result is then saved in a WeakMap so subsequent requests for the same object
* will receive the same ID
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function assignId(obj) {
var cachedValue = randomIdCache.get(obj);
if (cachedValue) return cachedValue;
var id = (0, _nanoid.nanoid)();
randomIdCache.set(obj, id);
return id;
}