-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_int_id_example.dart
39 lines (31 loc) · 1.38 KB
/
safe_int_id_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// Example of how to use `SafeIntId`.
/// Run it with `dart run --enable-asserts example/safe_int_id_example.dart`
import "dart:math";
import "package:safe_int_id/safe_int_id.dart";
void main() {
// Get a new uncoordinated unique ID
// that fits `Number.MAX_SAFE_INTEGER` on web platform for long enough time.
final id = safeIntId.getId();
print("id: $id");
// Get [DateTime] when given [id] was created at.
final createdAt = safeIntId.getCreatedAt(id, isUtc: true);
print("createdAt: $createdAt");
// Increment a counter instead of using a random value.
// Counter resets to zero each millisecond, blocks reaching max (1024 by default).
final id1 = safeIntId.incId();
final id2 = safeIntId.incId();
assert(id1 < id2);
// Custom configuration, defaults are:
final customId = SafeIntId(
firstYear: 2023, // The first year you use this ID.
random: Random(), // Random generator, e.g. `Random.secure()`
randomValues: 1024, // Number of possible random values per millisecond.
// Makes ID more unique in exchange for [safeYears].
);
// How many years since [firstYear] this ID will be safe:
assert(customId.safeYears == 278);
// Last year this ID will be safe:
assert(customId.lastSafeYear == 2300);
// Custom configuration has the same [getId] and [getCreatedAt] methods:
assert(!customId.getCreatedAt(customId.getId()).isBefore(createdAt));
}