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

Adding space formatter utils functions #168

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

marc-aurele-besner
Copy link
Collaborator

@marc-aurele-besner marc-aurele-besner commented Nov 13, 2024

User description

Adding space formatter utils functions

To facilitate the conversion to PiB or PB


PR Type

enhancement


Description

  • Added utility functions formatSpaceToBinary and formatSpaceToDecimal to format space values into human-readable strings using binary and decimal units respectively.
  • Updated the index.ts to export the newly added format utility functions.

Changes walkthrough 📝

Relevant files
Enhancement
format.ts
Add space formatting utility functions for binary and decimal

packages/auto-consensus/src/utils/format.ts

  • Added utility functions for formatting space in binary and decimal
    formats.
  • Supports conversion to units like KiB, MiB, GiB, etc., for binary.
  • Supports conversion to units like KB, MB, GB, etc., for decimal.
  • Handles zero value conversion.
  • +25/-0   
    index.ts
    Export format utility functions in index                                 

    packages/auto-consensus/src/utils/index.ts

    • Exported new format utility functions.
    +1/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling
    The functions formatSpaceToBinary and formatSpaceToDecimal do not handle cases where the input value is negative. This might lead to unexpected behavior or incorrect formatting.

    Possible Bug
    Both functions use Math.log which can return NaN if the input value is negative or zero. The zero case is handled, but negative values are not, which could result in runtime errors.

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Prevent division by zero errors in logarithmic calculations

    Handle potential division by zero error when Math.log(value) results in a negative
    infinity which can occur if value is less than 1.

    packages/auto-consensus/src/utils/format.ts [9]

    -const i = Math.floor(Math.log(value) / Math.log(k))
    +const i = value < 1 ? 0 : Math.floor(Math.log(value) / Math.log(k))
    Suggestion importance[1-10]: 7

    Why: This suggestion correctly identifies a potential issue with logarithmic calculations when the value is less than 1, which can lead to incorrect results. Implementing this check improves the robustness of the function.

    7
    Validate input to ensure it is a number and not NaN

    Add error handling or checks to ensure that the value parameter is a number and not
    NaN, which could lead to incorrect calculations and outputs.

    packages/auto-consensus/src/utils/format.ts [2]

     export const formatSpaceToBinary = (value: number, decimals = 2) => {
    +  if (isNaN(value)) throw new Error('Input must be a number');
    Suggestion importance[1-10]: 7

    Why: Adding a check for NaN values at the beginning of the function is crucial to avoid incorrect calculations and potential errors downstream. This suggestion significantly improves the function's reliability by ensuring valid input.

    7
    Ensure the decimals parameter is a valid integer

    Validate that the decimals parameter is an integer to ensure the .toFixed() method
    does not throw an error.

    packages/auto-consensus/src/utils/format.ts [6]

    -const dm = decimals < 0 ? 0 : decimals
    +const dm = Number.isInteger(decimals) && decimals >= 0 ? decimals : 2
    Suggestion importance[1-10]: 6

    Why: Validating the decimals parameter to ensure it is a non-negative integer is a good practice to prevent runtime errors from the .toFixed() method. This suggestion enhances the function's input handling.

    6
    Performance
    Optimize performance by caching repeated calculations

    Consider caching the results of Math.log(k) since k is a constant, to improve
    performance by avoiding repeated calculations.

    packages/auto-consensus/src/utils/format.ts [9]

    -const i = Math.floor(Math.log(value) / Math.log(k))
    +const logK = Math.log(k);
    +const i = Math.floor(Math.log(value) / logK)
    Suggestion importance[1-10]: 5

    Why: Caching the result of Math.log(k) is a valid optimization since k is a constant and the log value is used multiple times. This suggestion offers a performance improvement by reducing redundant calculations.

    5

    @marc-aurele-besner marc-aurele-besner merged commit 8265421 into main Nov 14, 2024
    2 checks passed
    @marc-aurele-besner marc-aurele-besner deleted the feat/add-space-formatter-function branch November 14, 2024 10:39
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants