Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util functions for encoding and decoding bytes #34

Merged
merged 14 commits into from
Oct 2, 2022
Merged

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Oct 1, 2022

This adds some util functions for working with bytes (Uint8Array). These implementations don't rely on the Node.js Buffer class, meaning that it does not require a polyfill to be used in browsers.

These functions are implemented with performance in mind, so a faster implementation was chosen over a "pretty" implementation.

@Mrtenz Mrtenz marked this pull request as ready for review October 1, 2022 14:17
@Mrtenz Mrtenz requested a review from a team as a code owner October 1, 2022 14:17
package.json Outdated Show resolved Hide resolved
Copy link
Member

@ritave ritave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfinished review, left some comments

package.json Outdated Show resolved Hide resolved
src/bytes.ts Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/bytes.ts Show resolved Hide resolved
src/bytes.ts Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/bytes.ts Outdated Show resolved Hide resolved
src/hex.ts Show resolved Hide resolved
src/bytes.ts Show resolved Hide resolved
src/bytes.ts Show resolved Hide resolved
assert(bytes > 0);

/* eslint-disable no-bitwise */
const mask = value >> BigInt(31);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

31n

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

src/bytes.ts Outdated Show resolved Hide resolved
for (let i = 0; i < bytes.length; i++) {
bytes[i] = Number(BigInt.asUintN(8, numberValue));
// eslint-disable-next-line no-bitwise
numberValue >>= BigInt(8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8n

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

src/__fixtures__/bytes.ts Show resolved Hide resolved
@Mrtenz Mrtenz merged commit 6e6c24a into main Oct 2, 2022
@Mrtenz Mrtenz deleted the mrtenz/byte-utils branch October 2, 2022 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants