Skip to content

Commit

Permalink
Add format.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Oct 6, 2021
1 parent f6cfa39 commit 8a7776f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Principal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ module {
nat32ToNat8Array(CRC32.checksum(bs)),
bs,
));
Principal.fromText(format(b32));
};

public let toBlob : (p : Principal) -> Blob = Principal.toBlob;

public func format(bs : [Nat8]) : Text {
var id = "";
for (i in b32.keys()) {
let c = Prim.charToLower(Char.fromNat32(nat8ToNat32(b32[i])));
for (i in bs.keys()) {
let c = Prim.charToLower(Char.fromNat32(nat8ToNat32(bs[i])));
id #= Char.toText(c);
if ((i + 1) % 5 == 0 and i + 1 != b32.size()) {
if ((i + 1) % 5 == 0 and i + 1 != bs.size()) {
id #= "-"
}
};
};
Principal.fromText(id);
id;
};

public let toBlob : (p : Principal) -> Blob = Principal.toBlob;

private func nat8ToNat32(n : Nat8) : Nat32 {
Nat32.fromNat(Nat8.toNat(n));
};
Expand Down

0 comments on commit 8a7776f

Please sign in to comment.