From 2774a13c176e8d51756a0b512da673e1c32facb9 Mon Sep 17 00:00:00 2001 From: Quint Daenen Date: Thu, 16 Dec 2021 15:43:14 +0100 Subject: [PATCH] Add FUNDING file. --- .github/FUNDING.yml | 1 + README.md | 4 ++-- src/GUID.mo | 23 +++++++++++++++++++++++ src/UUID.mo | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .github/FUNDING.yml create mode 100644 src/GUID.mo diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..c326dcd --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: ["https://icdevs.org/donations.html"] diff --git a/README.md b/README.md index 2a5556d..e26d173 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/GUID.mo b/src/GUID.mo new file mode 100644 index 0000000..58371be --- /dev/null +++ b/src/GUID.mo @@ -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); + +}; diff --git a/src/UUID.mo b/src/UUID.mo index 74effbc..6046a0e 100644 --- a/src/UUID.mo +++ b/src/UUID.mo @@ -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. @@ -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(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); };