-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split
utils.ts
into multiple files in a utils/
directory.
- Loading branch information
Showing
8 changed files
with
49 additions
and
50 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** Determine if a number is a byte. */ | ||
function isByte(b: number): boolean { | ||
return (b & 0xff) === b | ||
} | ||
|
||
/** Ensure every number in bytes is an actual byte value. */ | ||
export function checkBytes(bytes: readonly number[]): void { | ||
for (const b of bytes) { | ||
if (!isByte(b)) { | ||
throw new RangeError('non-byte found') | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** Debug representation of a byte value. */ | ||
function prettyByte(byte: number): string { | ||
return ('0' + byte.toString(16).toUpperCase()).slice(-2) | ||
} | ||
|
||
/** Debug representation of a byte list. */ | ||
export function prettyBytes(bytes: readonly number[] | Uint8Array): string { | ||
// Explicitly apply the Array map function so this works on both arrays and | ||
// typed arrays. | ||
return `[${Array.prototype.map.call(bytes, prettyByte).join(' ')}]` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** Return an unresolved promise and resolve/reject functions for it. */ | ||
export function promiseWithResolvers<T>(): { | ||
promise: Promise<T> | ||
resolve: (value: T) => void | ||
reject: (reason?: any) => void | ||
} { | ||
let promiseResolve: (value: T) => void | ||
let promiseReject: (reason?: any) => void | ||
const promise = new Promise<T>((resolve: (value: T) => void, reject: (reason?: any) => void) => { | ||
promiseResolve = resolve | ||
promiseReject = reject | ||
}) | ||
|
||
return { | ||
promise, | ||
resolve: promiseResolve!, | ||
reject: promiseReject!, | ||
} | ||
} |
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
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
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
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
This file was deleted.
Oops, something went wrong.