UNPKG

1.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.uid = exports.generateUID = void 0;
4/**
5 * generates a UID factory
6 * @internal
7 * @example
8 * const uid = generateUID();
9 * uid(object) = 1;
10 * uid(object) = 1;
11 * uid(anotherObject) = 2;
12 */
13var generateUID = function () {
14 var counter = 1;
15 var map = new WeakMap();
16 /**
17 * @borrows {uid}
18 */
19 var uid = function (item, index) {
20 if (typeof item === 'number' || typeof item === 'string') {
21 return index ? "idx-".concat(index) : "val-".concat(item);
22 }
23 if (!map.has(item)) {
24 map.set(item, counter++);
25 return uid(item);
26 }
27 return 'uid' + map.get(item);
28 };
29 return uid;
30};
31exports.generateUID = generateUID;
32/**
33 * @name uid
34 * returns an UID associated with {item}
35 * @param {Object} item - object to generate UID for
36 * @param {Number} index, a fallback index
37 * @example
38 * uid(object) == 1;
39 * uid(object) == 1;
40 * uid(anotherObject) == 2;
41 * uid("not object", 42) == 42
42 *
43 * @see {@link useUID}
44 */
45exports.uid = (0, exports.generateUID)();