Skip to content

Commit

Permalink
feat: Update utils
Browse files Browse the repository at this point in the history
Add includeBytes function declaration
Add helper for returning value from collection methods
Add helper types
Refactor code
  • Loading branch information
petarvujovic98 committed Sep 19, 2022
1 parent cedd904 commit e2639a7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 60 deletions.
11 changes: 8 additions & 3 deletions lib/utils.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 22 additions & 26 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 46 additions & 30 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,65 @@
import { GetOptions } from "./types/collections";

export type Bytes = string;
export type PromiseIndex = number | bigint;
export type NearAmount = number | bigint;
export type Register = number | bigint;

export function u8ArrayToBytes(array: Uint8Array) {
let ret = "";
for (const e of array) {
ret += String.fromCharCode(e);
}
return ret;
export function u8ArrayToBytes(array: Uint8Array): Bytes {
return array.reduce(
(result, value) => `${result}${String.fromCharCode(value)}`,
""
);
}

// TODO this function is a bit broken and the type can't be string
// TODO for more info: https://github.com/near/near-sdk-js/issues/78
export function bytesToU8Array(bytes: Bytes): Uint8Array {
const ret = new Uint8Array(bytes.length);
for (let i = 0; i < bytes.length; i++) {
ret[i] = bytes.charCodeAt(i);
}
return ret;
return Uint8Array.from([...bytes].map((byte) => byte.charCodeAt(0)));
}

export function bytes(strOrU8Array: string | Uint8Array): Bytes {
if (typeof strOrU8Array == "string") {
return checkStringIsBytes(strOrU8Array);
} else if (strOrU8Array instanceof Uint8Array) {
return u8ArrayToBytes(strOrU8Array);
export function bytes(stringOrU8Array: string | Uint8Array): Bytes {
if (typeof stringOrU8Array === "string") {
return checkStringIsBytes(stringOrU8Array);
}

if (stringOrU8Array instanceof Uint8Array) {
return u8ArrayToBytes(stringOrU8Array);
}

throw new Error("bytes: expected string or Uint8Array");
}

function checkStringIsBytes(str: string) {
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 255) {
throw new Error(
`string ${str} at index ${i}: ${str[i]} is not a valid byte`
);
}
}
return str;
function checkStringIsBytes(value: string): string {
[...value].forEach((character, index) => {
assert(
character.charCodeAt(0) <= 255,
`string ${value} at index ${index}: ${character} is not a valid byte`
);
});

return value;
}

export function assert(b: boolean, str: string) {
if (b) {
return;
} else {
throw Error("assertion failed: " + str);
export function assert(expression: boolean, message: string): void {
if (!expression) {
throw Error("assertion failed: " + message);
}
}

export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

export function getValueWithOptions<DataType>(
value: unknown,
options?: GetOptions<DataType>
): DataType | null {
if (value === undefined || value === null) {
return options?.defaultValue ?? null;
}

if (options?.reconstructor) {
return options.reconstructor(value);
}

return value as DataType;
}
2 changes: 1 addition & 1 deletion tests/src/promise_batch_api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { near, bytes } from "near-sdk-js";
import { near, bytes, includeBytes } from "near-sdk-js";

export function test_promise_batch_stake() {
let promiseId = near.promiseBatchCreate("caller2.test.near");
Expand Down

0 comments on commit e2639a7

Please sign in to comment.