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

Number.parseInt #4453

Open
jessekelly881 opened this issue Feb 14, 2025 · 1 comment · May be fixed by #4522
Open

Number.parseInt #4453

jessekelly881 opened this issue Feb 14, 2025 · 1 comment · May be fixed by #4522
Labels
enhancement New feature or request

Comments

@jessekelly881
Copy link
Contributor

jessekelly881 commented Feb 14, 2025

What is the problem this feature would solve?

Like Number.parse this would be a nice addition.

What is the feature you are proposing to solve the problem?

const parseInt = (str: string, radix?: number): Option.Option<number> => {
  const num = Number.parseInt(str, radix);
  if (Number.isNaN(num)) {
    return Option.none();
  }
  return Option.some(num);
};

Exists internally here:

const parseInteger = (str: string): Option.Option<number> => {

What alternatives have you considered?

No response

@jessekelly881 jessekelly881 added the enhancement New feature or request label Feb 14, 2025
@jessekelly881 jessekelly881 changed the title Number.parseInt :: Option<number> Number.parseInt Feb 14, 2025
@giuliobracci
Copy link

@jessekelly881 you can define a simple parseInt from Effect's Schema library.

import { Schema } from "effect"

const parseInt = Schema.decodeOption(Schema.NumberFromString);

const decodedNumber = parseInt("2");
const decodedNaN = parseInt("");

console.log(decodedNumber); // decodes to { _id: 'Option', _tag: 'Some', value: 2 }
console.log(decodedNaN); // decodes to { _id: 'Option', _tag: 'None' }

Demo

Let me know if that works for you.

@jessekelly881 jessekelly881 linked a pull request Feb 26, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants