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

optimization of the adjustNumber function #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions eo2js-runtime/src/runtime/bytes-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const hexToInt = function(bytes) {
byte = parseInt(hex, 16)
} else {
throw new Error(
`Wrong format of element ${hex} in byte array ${bytes}\nShould be either integer of hexadecimal starting with '0x'`
`Wrong format of element ${hex} in byte array ${bytes}\nShould be either integer of hexadecimal starting with '0x'`
Copy link
Member

Choose a reason for hiding this comment

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

@Akvadevka there's something wrong with indentation

)
}
return byte
Expand All @@ -38,19 +38,16 @@ const hexToInt = function(bytes) {
* @return {Array.<Number>} - Adjusted byte array
*/
const adjustNumber = function(bytes) {
if (bytes.length === 8) {
const number = bytesOf(
if (bytes.length !== 8) return bytes;
Copy link
Member

Choose a reason for hiding this comment

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

@Akvadevka we're trying to avoid many return statements, here's why


const number = bytesOf(
new DataView(new Int8Array(bytes).buffer).getBigInt64(0)
).asBytes()
return bytes.map((byte, index) => {
if (Math.abs(byte - number[index]) === 256) {
return byte - 256
}
return byte
})
}
return bytes
}
).asBytes();

return bytes.map((byte, index) =>
Math.abs(byte - number[index]) === 256 ? byte - 256 : byte
Copy link
Member

Choose a reason for hiding this comment

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

@Akvadevka I don't see the purpose of the change. Replacing if with ternary operator didn't make the code more readable

);
};

/**
* Bytes of.
Expand Down
Loading