Skip to content

ICPSwap-Labs/ic-commons-v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ic-commons

IC Commons, a package of Motoko utility classes for the classes.

Base64

encode


func encode(value: Text) : Text

Encodes string by Base64.

CRC32

crc32


func crc32(value: [Nat8]) : [Nat8]

Encodes an array of Nat8 by CRC32, and return the encoded array.

ofBlob


func ofBlob(blob: Blob) : Nat32

Encodes Blob convert into Nat32 by CRC32.

ofArray


func ofArray(arr : [Nat8]) : Nat32

Encodes an array of Nat8 convert into Nat32 by CRC32.

Hex

encode


func encode(array: [Nat8]) : Text

Encodes an array of Nat8 by Hex, and return the encoded array.

decode


func decode(text: Text) : async [Nat8]

Decodes string by Hex, and return the decoded Nat8 array.

toHex


func toHex(_data: Text) : Text

Encodes string by Hex, and return the encoded string.

BitwiseInt

bitshiftLeft


func bitshiftLeft(x: Int, y: Int) : Int

The left shift operation, shifts x by y of bits to the left.

bitshiftRight


func bitshiftRight(x: Int, y: Int) : Int

The right shift operation, shifts x by y of bits to the right.

toIntArr


func toIntArr(x: Int, bit: Nat) : [Int64]

Converts x into a array of Int64 by bit number of bits.

bitOperation


func bitOperation(x: Int, y: Int, operationFunc: (x1: Int64, y1: Int64) -> Int64) : Int

Performs bitwise operation on x and y via operationFunc function, and return the calculated result.

bitand


func bitand(x: Int, y: Int) : Int

Bitwise AND operation on x and y.

bitor


func bitor(x: Int, y: Int) : Int

Bitwise OR operation on x and y.

bitnot


func bitnot(x: Int) : Int

Bitwise NOT operation on x and y.

IntUtils

toNat


func toNat(x: Int) : Nat

Converts the Int type of x into Nat.

BitwiseNat

bitshiftLeft


func bitshiftLeft(x: Nat, y: Nat) : Nat

The left shift operation, shifts x by y of bits to the left.

bitshiftRight


func bitshiftRight(x: Nat, y: Nat) : Nat

The right shift operation, shifts x by y of bits to the right.

toIntArr


func toIntArr(x: Nat, bit: Nat) : [Nat64]

Converts x into a array of Nat64 by bit number of bits.

bitOperation


func bitOperation(x: Nat, y: Nat, operationFunc: (x1: Nat64, y1: Nat64) -> Int64) : Nat

Performs bitwise operation on x and y via operationFunc function, and return the calculated result.

bitand


func bitand(x: Nat, y: Nat) : Nat

Bitwise AND operation on x and y.

bitor


func bitor(x: Nat, y: Nat) : Nat

Bitwise OR operation on x and y.

bitnot


func bitnot(x: Nat) : Nat

Bitwise NOT operation on x and y.

NatUtils

toInt


func toInt(x: Nat) : Int

Converts the Nat type of x into Int.

nat32ToNat8Arr


func nat32ToNat8Arr(nat: Nat32) : [Nat8]

Converts nat of Nat32 into an array of Nat8.

gt


func gt(x: Nat, y: Nat) : Nat

Compares x and y. If x greater than y, return 1, otherwise return 0.

lt


func lt(x: Nat, y: Nat) : Nat

Compares x and y. If x lower than y, return 1, otherwise return 0.

isZero


func isZero(v: Nat) : Bool

Checks if the v is zero.

TextUtils

encode


func encode(array: [Nat8]) : Text

Encodes an array of Nat8 by Base64, return the encoded string.

fromInt


func fromInt(v: Int) : Text

Converts an Int number into string.

fromNat


func fromNat(v: Nat) : Text

Converts an Nat number into string.

fromNat8


func fromNat8(u8: Nat8) : Text

Converts an Nat8 number into string.

toInt


func toInt(txt: Text) : Int

Converts a string into Int number.

toNat


func toNat(text: Text) : Nat

Converts a string into Nat number.

toUpperCase


func toUpperCase(text: Text) : Text

Return the calling string value converted to uppercase.

equalsIgnoreCase


func equalsIgnoreCase(t1: Text, t2: Text) : Bool

Compares two strings without case sensitive.

extractText


func extractText(t: Text, i: Nat, j: Nat) : Text

Returns the part of the string t between the start i and end j indexes.

toArray


func toArray(t: Text) : [Text]

Divides a string t into each character of the string, puts these characters into an array, and returns the array.

fromArray


func fromArray(array: [Text]) : Text

Returns a new string by concatenating all of the elements in an array.

completionZero


func completionZero(_t: Text, d: DirectionType, n: Nat) : Text

Fills vacated bits with zeros before/after string _t by DirectionType type. The n is the length of the string which has been filled vacated bits with zeros

binaryToDecimal


func binaryToDecimal(_num: Nat32) : Text

Converts a Nat32 number into a binary string.

scaleToDecimal


func scaleToDecimal(number: Text, scale: Nat) : Nat

Converts a scale-ary string into a Nat number.

hexToNumber


func hexToNumber(_t: Text) : Nat

Converts a Hex string into a Nat number.

CollectUtils

arrayContains


func arrayContains<T>(arr: [T], item: T, equal: (T, T) -> Bool) : Bool

Determines whether an array matchs a certain value among its entries, returning true or false as "equal" function returns.

listRemove


func listRemove<T>(list: List.List<T>, item: T, equal: (T, T) -> Bool) : List.List<T>

Removes matched item in the list, returning the list which removed item.

arrayRemove


func arrayRemove<T>(arr: [T], item: T, equal: (T, T) -> Bool) : [T]

Removes matched element in the array, returning the array which removed element.

listRange


func listRange<T>(list: List.List<T>, offset: Nat, limit: Nat) : List.List<T>

Returns a portion of the list, starting at the offset index and extending for a given number of items afterwards.

arrayRange


func arrayRange<T>(arr: [T], offset: Nat, limit: Nat) : [T]

Returns a portion of the array, starting at the offset index and extending for a given number of elements afterwards.

sort


func sort<A>(xs : [A], cmp : (A, A) -> Order.Order) : [A]

Sorts the elements of an array and returns the sorted array. The sort order is determined by cmp function.

sortInPlace


func sortInPlace<A>(xs : [var A], cmp : (A, A) -> Order.Order)

Sorts the elements of an array in place and returns the sorted array. The sort order is determined by cmp function.

PrincipalUtils

toAddress


func toAddress(p: Principal) : Text

Converts Principal into the Account Id string.

blobToAddress


func blobToAddress(blob : Blob) : Text

Converts Blob into the Account Id string.

toText


func toText(p: Principal) : Text

Converts Principal into the Principal Id string.

isEmptyIdentity


func isEmptyIdentity(caller: Principal) : Bool

Checks if the caller is empty.