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

feat: now allows a custom number parser to be passed as an option. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

neverendingqs
Copy link

the PR looks ok, but I'm a bit worried about growing nimber of options like yours. Maybe we should move to much simple api, for example toNumber() callback that takes number-like string as an input and decides what to return - JS number, BigNumber or something else. If not present the behaviour is automatic ( BigNumber if not all integer digits can be preserved accurately )
~#20 (comment)

Is this what you had in mind @sidorares?

@DonBrinn
Copy link

How would the custom number parser be used to solve the defect in parsing long decimal numbers when using the native BigInt with json-bigint?

@neverendingqs
Copy link
Author

neverendingqs commented Dec 15, 2020

How would the custom number parser be used to solve the defect in parsing long decimal numbers when using the native BigInt with json-bigint?

I'm thinking something like this:

const JSONBI = require('json-bigint')({
  numberParser: str => {
    const attempt = Number(str);
    
    if(str.indexOf('.') >= 0) {
      // Or do something else if the precision of decimal numbers is important
      return attempt;
    }
    
    if(attempt <= Number.MIN_SAFE_INTEGER || attempt >= Number.MAX_SAFE_INTEGER) {
      return BigInt(str);
    }
    
    return attempt;
  }
});

@neverendingqs
Copy link
Author

@sidorares - bump :)

1 similar comment
@neverendingqs
Copy link
Author

@sidorares - bump :)

@neverendingqs
Copy link
Author

@sidorares - bump :). Any concerns with the pull request?

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