|
111 | 111 | */ |
112 | 112 | /// defaultSorter |
113 | 113 | /** |
114 | | - * The getUniqueId function. |
| 114 | + * The getUniqueId function returns a unique string of a given length. |
| 115 | + * |
| 116 | + * This is used internally by TinyBase for the synchronizer protocol and for |
| 117 | + * unique MergeableStore identifiers. But it is useful enough for it to be |
| 118 | + * publicly exposes for purposes such as identifying shared collaboration rooms, |
| 119 | + * or creating other Ids that need to be unique. |
| 120 | + * |
| 121 | + * The string may contain numbers, lower or upper case letters, or the '-' or |
| 122 | + * '_' characters. This makes them URL-safe, and means they can be identified |
| 123 | + * with a regex like `/[-_0-9A-Za-z]+/`. |
| 124 | + * |
| 125 | + * This function prefers to use the `crypto` module to generate random numbers, |
| 126 | + * but where that is not available (such as in React Native), a Math.random |
| 127 | + * implementation is used. Whilst that my not be cryptographically sound, it |
| 128 | + * should suffice for most TinyBase-related purposes. |
| 129 | + * @param length The desired length of the unique Id, defaulting to 16. |
| 130 | + * @returns A unique Id of the required length. |
| 131 | + * @example |
| 132 | + * This example creates two 8 character long Ids and compares them. |
| 133 | + * |
| 134 | + * ```js |
| 135 | + * import {getUniqueId} from 'tinybase'; |
| 136 | + * |
| 137 | + * const id1 = getUniqueId(8); |
| 138 | + * const id2 = getUniqueId(8); |
| 139 | + * |
| 140 | + * console.log(id1.length); |
| 141 | + * // -> 8 |
| 142 | + * console.log(id2.length); |
| 143 | + * // -> 8 |
| 144 | + * console.log(id1 == id2); |
| 145 | + * // -> false |
| 146 | + * ``` |
115 | 147 | * @category Convenience |
116 | 148 | * @since v5.0.0 |
117 | 149 | */ |
|
0 commit comments