Skip to content

Commit

Permalink
chore(core): Add Uuid.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Sep 15, 2024
1 parent ce378c5 commit 1d61ad2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/celest_core/lib/src/util/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class Uuid {

factory Uuid.v7() => Uuid._(_uuidv7());

factory Uuid.parse(String uuid) {
final buffer = Uint8List(16);
var i = 0;
for (var j = 0; j < uuid.length; j += 2) {
if (uuid[j] == '-') {
j++;
}
buffer[i++] = int.parse(uuid.substring(j, j + 2), radix: 16);
}
return Uuid._(buffer);
}

final Uint8List value;

static bool _addSeparator(int i) => i == 4 || i == 6 || i == 8 || i == 10;
Expand Down
12 changes: 12 additions & 0 deletions packages/celest_core/test/util/uuid_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:celest_core/_internal.dart';
import 'package:test/test.dart';

void main() {
group('Uuid', () {
test('v7', () {
final uuid = Uuid.v7();
final rt = Uuid.parse(uuid.hexValue);
expect(uuid.value, rt.value);
});
});
}

0 comments on commit 1d61ad2

Please sign in to comment.