Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: uuid module (equal; hash); guid module #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: ["https://icdevs.org/donations.html"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ UUID.toText(await g.new());
// F253F1F0-2885-169F-169F-2885F253F1F0
```

## ICDevs.org
---

This library was incentivized by https://ICDevs.org. You can view more about the bounty at https://forum.dfinity.org/t/icdevs-org-bounty-4-uuid-motoko-library/8648 and https://icdevs.org/bounties/2021/11/17/UUID-motoko-library.html. The bounty was funded by The Dragginz Team and the award was graciously donated by di-wu to the treasury of ICDevs.org so that we could pursue more bounties. If you use this library and gain value from it, please consider a donation to ICDevs at https://icdevs.org/donations.html.
This library was incentivized by [ICDevs](https://ICDevs.org). You can view more about the bounty on the [forum](https://forum.dfinity.org/t/icdevs-org-bounty-4-uuid-motoko-library/8648) or [website](https://icdevs.org/bounties/2021/11/17/UUID-motoko-library.html). The bounty was funded by The Dragginz Team and the award was graciously donated by [di-wu](https://github.com/di-wu) to the treasury of ICDevs.org so that we could pursue more bounties. If you use this library and gain value from it, please consider a [donation](https://icdevs.org/donations.html) to ICDevs.
23 changes: 23 additions & 0 deletions src/GUID.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Hex "mo:encoding/Hex";
import Bool "mo:base/Bool";
import Hash "mo:base/Hash";
import Text "mo:base/Text";
import Result "mo:base/Result";
import UUID "UUID";

module {
public type GUID = Text;
type UUID = UUID.UUID;

public func toUUID(guid : GUID) : UUID {
var tr = Text.replace(guid, #text("-"), "");
switch(Hex.decode(tr)){
case(#err(e)) {return []};
case(#ok(an8)) { return an8 };
};
};

public func equal(guid : GUID, guid2 : GUID): Bool = Text.equal(guid, guid2);
public func hash(guid : GUID): Hash.Hash = Text.hash(guid);

};
19 changes: 19 additions & 0 deletions src/UUID.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import Hex "mo:encoding/Hex";
import List "mo:base/List";
import Bool "mo:base/Bool";
import Array "mo:base/Array";
import Nat8 "mo:base/Nat8";
import Nat32 "mo:base/Nat32";
import Hash "mo:base/Hash";
import Text "mo:base/Text";
import Buffer "mo:base/Buffer";

module {
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
Expand All @@ -18,4 +25,16 @@ module {
let (p3, xs3) = List.split(2, xs2);
t # Hex.encode(List.toArray(p3)) # "-" # Hex.encode(List.toArray(xs3));
};

public func hash(uuid : UUID): Hash.Hash{
var buffer = Buffer.Buffer<Nat32>(16); //128 bit - 16 byte
for(n8 in uuid.vals()){
var n = Nat8.toNat(n8);
var n32 = Nat32.fromNat(n);
buffer.add(n32);
};
return Hash.hashNat8(buffer.toArray());
};

public func equal(uuid : UUID, uuid2 : UUID): Bool = Array.equal(uuid, uuid2, Nat8.equal);
};