-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add includeBytes function declaration Add helper for returning value from collection methods Add helper types Refactor code
- Loading branch information
1 parent
cedd904
commit e2639a7
Showing
4 changed files
with
77 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters