source: node_modules/short-unique-id/dist/short-unique-id.d.ts

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/**
2 * @packageDocumentation
3 **/
4export interface ShortUniqueIdRanges {
5 [k: string]: [number, number];
6}
7export interface ShortUniqueIdRangesMap {
8 [k: string]: ShortUniqueIdRanges;
9}
10export type ShortUniqueIdDefaultDictionaries = 'number' | 'alpha' | 'alpha_lower' | 'alpha_upper' | 'alphanum' | 'alphanum_lower' | 'alphanum_upper' | 'hex';
11/**
12 * ```js
13 * {
14 * dictionary: ['z', 'a', 'p', 'h', 'o', 'd', ...],
15 * shuffle: false,
16 * debug: false,
17 * length: 6,
18 * }
19 * ```
20 * <br/>
21 * @see {@link DEFAULT_OPTIONS}
22 */
23export interface ShortUniqueIdOptions {
24 /** User-defined character dictionary */
25 dictionary: string[] | ShortUniqueIdDefaultDictionaries;
26 /** If true, sequentialUUID use the dictionary in the given order */
27 shuffle: boolean;
28 /** If true the instance will console.log useful info */
29 debug: boolean;
30 /** From 1 to infinity, the length you wish your UUID to be */
31 length: number;
32 /** From 0 to infinity, the current value for the sequential UUID counter */
33 counter: number;
34}
35/**
36 * 6 was chosen as the default UUID length since for most cases
37 * it will be more than aptly suitable to provide millions of UUIDs
38 * with a very low probability of producing a duplicate UUID.
39 *
40 * For example, with a dictionary including digits from 0 to 9,
41 * as well as the alphabet from a to z both in UPPER and lower case,
42 * the probability of generating a duplicate in 1,000,000 rounds
43 * is ~0.00000002, or about 1 in 50,000,000.
44 */
45export declare const DEFAULT_UUID_LENGTH: number;
46export declare const DEFAULT_OPTIONS: ShortUniqueIdOptions;
47/**
48 * Generate random or sequential UUID of any length.
49 *
50 * ### Use as module
51 *
52 * ```js
53 * // Deno (web module) Import
54 * import ShortUniqueId from 'https://cdn.jsdelivr.net/npm/short-unique-id@latest/src/index.ts';
55 *
56 * // ES6 / TypeScript Import
57 * import ShortUniqueId from 'short-unique-id';
58 *
59 * // or Node.js require
60 * const ShortUniqueId = require('short-unique-id');
61 *
62 * // Instantiate
63 * const uid = new ShortUniqueId();
64 *
65 * // Random UUID
66 * console.log(uid.rnd());
67 *
68 * // Sequential UUID
69 * console.log(uid.seq());
70 * ```
71 *
72 * ### Use in browser
73 *
74 * ```html
75 * <!-- Import -->
76 * <script src="https://cdn.jsdelivr.net/npm/short-unique-id@latest/dist/short-unique-id.min.js"></script>
77 *
78 * <!-- Usage -->
79 * <script>
80 * // Instantiate
81 * var uid = new ShortUniqueId();
82 *
83 * // Random UUID
84 * document.write(uid.rnd());
85 *
86 * // Sequential UUID
87 * document.write(uid.seq());
88 * </script>
89 * ```
90 *
91 * ### Options
92 *
93 * Options can be passed when instantiating `uid`:
94 *
95 * ```js
96 * const options = { ... };
97 *
98 * const uid = new ShortUniqueId(options);
99 * ```
100 *
101 * For more information take a look at the [ShortUniqueIdOptions type definition](/interfaces/shortuniqueidoptions.html).
102 */
103export default class ShortUniqueId {
104 /** @hidden */
105 static default: typeof ShortUniqueId;
106 counter: number;
107 debug: boolean;
108 dict: string[];
109 version: string;
110 dictIndex: number;
111 dictRange: number[];
112 lowerBound: number;
113 upperBound: number;
114 dictLength: number;
115 uuidLength: number;
116 protected _digit_first_ascii: number;
117 protected _digit_last_ascii: number;
118 protected _alpha_lower_first_ascii: number;
119 protected _alpha_lower_last_ascii: number;
120 protected _hex_last_ascii: number;
121 protected _alpha_upper_first_ascii: number;
122 protected _alpha_upper_last_ascii: number;
123 protected _number_dict_ranges: ShortUniqueIdRanges;
124 protected _alpha_dict_ranges: ShortUniqueIdRanges;
125 protected _alpha_lower_dict_ranges: ShortUniqueIdRanges;
126 protected _alpha_upper_dict_ranges: ShortUniqueIdRanges;
127 protected _alphanum_dict_ranges: ShortUniqueIdRanges;
128 protected _alphanum_lower_dict_ranges: ShortUniqueIdRanges;
129 protected _alphanum_upper_dict_ranges: ShortUniqueIdRanges;
130 protected _hex_dict_ranges: ShortUniqueIdRanges;
131 protected _dict_ranges: ShortUniqueIdRangesMap;
132 protected log: (...args: any[]) => void;
133 /** Change the dictionary after initialization. */
134 setDictionary: (dictionary: string[] | ShortUniqueIdDefaultDictionaries, shuffle?: boolean) => void;
135 seq: () => string;
136 /**
137 * Generates UUID based on internal counter that's incremented after each ID generation.
138 * @alias `const uid = new ShortUniqueId(); uid.seq();`
139 */
140 sequentialUUID: () => string;
141 rnd: (uuidLength?: number) => string;
142 /**
143 * Generates UUID by creating each part randomly.
144 * @alias `const uid = new ShortUniqueId(); uid.rnd(uuidLength: number);`
145 */
146 randomUUID: (uuidLength?: number) => string;
147 fmt: (format: string, date?: Date) => string;
148 /**
149 * Generates custom UUID with the provided format string.
150 * @alias `const uid = new ShortUniqueId(); uid.fmt(format: string);`
151 */
152 formattedUUID: (format: string, date?: Date) => string;
153 /**
154 * Calculates total number of possible UUIDs.
155 *
156 * Given that:
157 *
158 * - `H` is the total number of possible UUIDs
159 * - `n` is the number of unique characters in the dictionary
160 * - `l` is the UUID length
161 *
162 * Then `H` is defined as `n` to the power of `l`:
163 *
164 * <div style="background: white; padding: 5px; border-radius: 5px; overflow: hidden;">
165 * <img src="https://render.githubusercontent.com/render/math?math=%5CHuge%20H=n%5El"/>
166 * </div>
167 *
168 * This function returns `H`.
169 */
170 availableUUIDs: (uuidLength?: number) => number;
171 /**
172 * Calculates approximate number of hashes before first collision.
173 *
174 * Given that:
175 *
176 * - `H` is the total number of possible UUIDs, or in terms of this library,
177 * the result of running `availableUUIDs()`
178 * - the expected number of values we have to choose before finding the
179 * first collision can be expressed as the quantity `Q(H)`
180 *
181 * Then `Q(H)` can be approximated as the square root of the product of half
182 * of pi times `H`:
183 *
184 * <div style="background: white; padding: 5px; border-radius: 5px; overflow: hidden;">
185 * <img src="https://render.githubusercontent.com/render/math?math=%5CHuge%20Q(H)%5Capprox%5Csqrt%7B%5Cfrac%7B%5Cpi%7D%7B2%7DH%7D"/>
186 * </div>
187 *
188 * This function returns `Q(H)`.
189 *
190 * (see [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution))
191 */
192 approxMaxBeforeCollision: (rounds?: number) => number;
193 /**
194 * Calculates probability of generating duplicate UUIDs (a collision) in a
195 * given number of UUID generation rounds.
196 *
197 * Given that:
198 *
199 * - `r` is the maximum number of times that `randomUUID()` will be called,
200 * or better said the number of _rounds_
201 * - `H` is the total number of possible UUIDs, or in terms of this library,
202 * the result of running `availableUUIDs()`
203 *
204 * Then the probability of collision `p(r; H)` can be approximated as the result
205 * of dividing the square root of the product of half of pi times `r` by `H`:
206 *
207 * <div style="background: white; padding: 5px; border-radius: 5px; overflow: hidden;">
208 * <img src="https://render.githubusercontent.com/render/math?math=%5CHuge%20p(r%3B%20H)%5Capprox%5Cfrac%7B%5Csqrt%7B%5Cfrac%7B%5Cpi%7D%7B2%7Dr%7D%7D%7BH%7D"/>
209 * </div>
210 *
211 * This function returns `p(r; H)`.
212 *
213 * (see [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution))
214 *
215 * (Useful if you are wondering _"If I use this lib and expect to perform at most
216 * `r` rounds of UUID generations, what is the probability that I will hit a duplicate UUID?"_.)
217 */
218 collisionProbability: (rounds?: number, uuidLength?: number) => number;
219 /**
220 * Calculate a "uniqueness" score (from 0 to 1) of UUIDs based on size of
221 * dictionary and chosen UUID length.
222 *
223 * Given that:
224 *
225 * - `H` is the total number of possible UUIDs, or in terms of this library,
226 * the result of running `availableUUIDs()`
227 * - `Q(H)` is the approximate number of hashes before first collision,
228 * or in terms of this library, the result of running `approxMaxBeforeCollision()`
229 *
230 * Then `uniqueness` can be expressed as the additive inverse of the probability of
231 * generating a "word" I had previously generated (a duplicate) at any given iteration
232 * up to the the total number of possible UUIDs expressed as the quotiend of `Q(H)` and `H`:
233 *
234 * <div style="background: white; padding: 5px; border-radius: 5px; overflow: hidden;">
235 * <img src="https://render.githubusercontent.com/render/math?math=%5CHuge%201-%5Cfrac%7BQ(H)%7D%7BH%7D"/>
236 * </div>
237 *
238 * (Useful if you need a value to rate the "quality" of the combination of given dictionary
239 * and UUID length. The closer to 1, higher the uniqueness and thus better the quality.)
240 */
241 uniqueness: (rounds?: number) => number;
242 /**
243 * Return the version of this module.
244 */
245 getVersion: () => string;
246 /**
247 * Generates a UUID with a timestamp that can be extracted using `uid.parseStamp(stampString);`.
248 *
249 * ```js
250 * const uidWithTimestamp = uid.stamp(32);
251 * console.log(uidWithTimestamp);
252 * // GDa608f973aRCHLXQYPTbKDbjDeVsSb3
253 *
254 * console.log(uid.parseStamp(uidWithTimestamp));
255 * // 2021-05-03T06:24:58.000Z
256 * ```
257 */
258 stamp: (finalLength: number, date?: Date) => string;
259 /**
260 * Extracts the date embeded in a UUID generated using the `uid.stamp(finalLength);` method.
261 *
262 * ```js
263 * const uidWithTimestamp = uid.stamp(32);
264 * console.log(uidWithTimestamp);
265 * // GDa608f973aRCHLXQYPTbKDbjDeVsSb3
266 *
267 * console.log(uid.parseStamp(uidWithTimestamp));
268 * // 2021-05-03T06:24:58.000Z
269 * ```
270 */
271 parseStamp: (suid: string, format?: string) => Date;
272 /**
273 * Set the counter to a specific value.
274 */
275 setCounter: (counter: number) => void;
276 constructor(argOptions?: Partial<ShortUniqueIdOptions>);
277}
Note: See TracBrowser for help on using the repository browser.