Skip to content

Commit 709d1bc

Browse files
committed
[docs] getUniqueId
1 parent 85cff6f commit 709d1bc

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/@types/common/docs.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,39 @@
111111
*/
112112
/// defaultSorter
113113
/**
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+
* ```
115147
* @category Convenience
116148
* @since v5.0.0
117149
*/

0 commit comments

Comments
 (0)