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

Support number[] in uint8ArrayToHexString #485

Merged
merged 3 commits into from
Nov 24, 2023
Merged

Support number[] in uint8ArrayToHexString #485

merged 3 commits into from
Nov 24, 2023

Conversation

dskloetd
Copy link
Collaborator

Motivation

Candid changed what used to be Uint8Array to Uint8Array | number[].
This means we often have to convert a field to Uint8Array just to get rid of the | number[] bit.
To make this slightly less painful we can make some functions accept both.

Changes

Make uint8ArrayToHexString accept both Uint8Array | number[].
I considered changing the name but in that case I don't think it's worth it anymore. What do you think?

Tests

Unit test added

Todos

  • Add entry to changelog (if necessary).

@dskloetd dskloetd marked this pull request as ready for review November 24, 2023 14:47
Copy link
Contributor

github-actions bot commented Nov 24, 2023

size-limit report 📦

Path Size
@dfinity/ckbtc 6.88 KB (0%)
@dfinity/cmc 1.01 KB (0%)
@dfinity/ledger-icrc 2.92 KB (0%)
@dfinity/ledger-icp 14.41 KB (0%)
@dfinity/nns 33.83 KB (0%)
@dfinity/nns-proto 76.3 KB (0%)
@dfinity/sns 15.04 KB (0%)
@dfinity/utils 4.14 KB (+0.31% 🔺)
@dfinity/ic-management 1.94 KB (0%)

Copy link
Contributor

@mstrasinskis mstrasinskis left a comment

Choose a reason for hiding this comment

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

Thanks!

@dskloetd dskloetd merged commit 55e5f64 into main Nov 24, 2023
11 checks passed
@dskloetd dskloetd deleted the kloet/hex branch November 24, 2023 15:23
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
export const uint8ArrayToHexString = (bytes: Uint8Array | number[]) => {
if (!(bytes instanceof Uint8Array)) {
bytes = Uint8Array.from(bytes);
Copy link
Member

Choose a reason for hiding this comment

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

nice to have: it would be nice to not mutate the provided variables but to realocate for a more functional approach (?).

cont b = bytes instanceof Unit8Array ? bytes : Uint8Array.from(bytes);

return b.reduce(...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It doesn't really make a difference to me but since it's already merged, I'll accept your PR :-).

Copy link
Member

Choose a reason for hiding this comment

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

😉

packages/utils/src/utils/arrays.utils.ts Show resolved Hide resolved
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.

3 participants