Skip to content

Number.parseInt #4453

@jessekelly881

Description

@jessekelly881
Contributor

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

Activity

changed the title [-]Number.parseInt :: Option<number>[/-] [+]Number.parseInt[/+] on Feb 14, 2025
giuliobracci

giuliobracci commented on Feb 20, 2025

@giuliobracci
Contributor

@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.

linked a pull request that will close this issue on Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jessekelly881@giuliobracci

      Issue actions

        Number.parseInt · Issue #4453 · Effect-TS/effect