Skip to content

Commit

Permalink
fix docs and add toStaticBytes (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody authored Jun 22, 2021
1 parent 9b1c9fc commit c61640b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
12 changes: 11 additions & 1 deletion assembly/__tests__/u128.spec.as.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,23 @@ describe("Buffer Conversion", () => {
it("Should convert to bytes Litte Endian 1", () => {
// var a: u8[] = (new u128(0x8877665544332211, 0x12ffeeddccbbaa99)).toBytes();
var u = new u128(0x8877665544332211, 0x12FFEEDDCCBBAA99);
var a = u.toBytes();
let a = u.toBytes();
expect(
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 &&
a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 &&
a[8] == 0x99 && a[9] == 0xAA && a[10] == 0xBB && a[11] == 0xCC &&
a[12] == 0xDD && a[13] == 0xEE && a[14] == 0xFF && a[15] == 0x12
).toBe(true);

{
let a = u.toStaticBytes();
expect(
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 &&
a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 &&
a[8] == 0x99 && a[9] == 0xAA && a[10] == 0xBB && a[11] == 0xCC &&
a[12] == 0xDD && a[13] == 0xEE && a[14] == 0xFF && a[15] == 0x12
).toBe(true);
}
});

it("Should convert to bytes Litte Endian 2", () => {
Expand Down
16 changes: 15 additions & 1 deletion assembly/__tests__/u256.spec.as.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("Buffer Conversion", () => {
0x8877665544332211,
0x12FFEEDDCCBBAA99
);
var a = u.toBytes();
let a = u.toBytes();
expect(
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 &&
a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 &&
Expand All @@ -111,6 +111,20 @@ describe("Buffer Conversion", () => {
a[24] == 0x99 && a[25] == 0xAA && a[26] == 0xBB && a[27] == 0xCC &&
a[28] == 0xDD && a[29] == 0xEE && a[30] == 0xFF && a[31] == 0x12
).toBe(true);

{
let a = u.toStaticBytes();
expect(
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 &&
a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 &&
a[8] == 0x99 && a[9] == 0xAA && a[10] == 0xBB && a[11] == 0xCC &&
a[12] == 0xDD && a[13] == 0xEE && a[14] == 0xFF && a[15] == 0x12 &&
a[16] == 0x11 && a[17] == 0x22 && a[18] == 0x33 && a[19] == 0x44 &&
a[20] == 0x55 && a[21] == 0x66 && a[22] == 0x77 && a[23] == 0x88 &&
a[24] == 0x99 && a[25] == 0xAA && a[26] == 0xBB && a[27] == 0xCC &&
a[28] == 0xDD && a[29] == 0xEE && a[30] == 0xFF && a[31] == 0x12
).toBe(true);
}
});

it("Should convert to bytes Litte Endian 2", () => {
Expand Down
17 changes: 15 additions & 2 deletions assembly/integer/i128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export class i128 {

/**
* Convert to byte array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Array of bytes
*/
@inline
Expand All @@ -379,9 +379,22 @@ export class i128 {
return result;
}


/**
* Convert to byte static array
* @param bigEndian Little or Big Endian? Default: false
* @returns StaticArray of bytes
*/
@inline
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
var result = new StaticArray<u8>(16);
this.toArrayBuffer(changetype<usize>(result), bigEndian);
return result;
}

/**
* Convert to Uint8Array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Uint8Array
*/
@inline
Expand Down
17 changes: 15 additions & 2 deletions assembly/integer/u128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ export class u128 {
else if (dummy instanceof u256) return <T>this.toU256();
else if (dummy instanceof u8[]) return <T>this.toBytes();
else if (dummy instanceof Uint8Array) return <T>this.toUint8Array();
else if (dummy instanceof StaticArray<u8>) return <T>this.toStaticBytes();
else if (dummy instanceof String) return <T>this.toString();
else throw new TypeError('Unsupported generic type');
}
Expand Down Expand Up @@ -882,7 +883,7 @@ export class u128 {

/**
* Convert to byte array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Array of bytes
*/
@inline
Expand All @@ -892,9 +893,21 @@ export class u128 {
return result;
}

/**
* Convert to byte static array
* @param bigEndian Little or Big Endian? Default: false
* @returns StaticArray of bytes
*/
@inline
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
var result = new StaticArray<u8>(16);
this.toArrayBuffer(changetype<usize>(result), bigEndian);
return result;
}

/**
* Convert to Uint8Array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Uint8Array
*/
@inline
Expand Down
17 changes: 15 additions & 2 deletions assembly/integer/u256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,14 @@ export class u256 {
else if (dummy instanceof u256) return <T>this.toU256();
else if (dummy instanceof u8[]) return <T>this.toBytes();
else if (dummy instanceof Uint8Array) return <T>this.toUint8Array();
else if (dummy instanceof StaticArray<u8>) return <T>this.toStaticBytes();
else if (dummy instanceof String) return <T>this.toString();
else throw new TypeError('Unsupported generic type');
}

/**
* Convert to byte array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Array of bytes
*/
@inline
Expand All @@ -600,9 +601,21 @@ export class u256 {
return result;
}

/**
* Convert to byte static array
* @param bigEndian Little or Big Endian? Default: false
* @returns StaticArray of bytes
*/
@inline
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
var result = new StaticArray<u8>(32);
this.toArrayBuffer(changetype<usize>(result), bigEndian);
return result;
}

/**
* Convert to Uint8Array
* @param le Little or Big Endian? Default: true
* @param bigEndian Little or Big Endian? Default: false
* @returns Uint8Array
*/
@inline
Expand Down

0 comments on commit c61640b

Please sign in to comment.