For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/1/proposal-number-fromstring.md.
  • 简体中文
  • {BigInt,Number}.fromString S1

    中文标题:{BigInt, Number}.fromString

    提案概览
    提案速览

    该提案为 NumberBigInt 引入了静态方法 fromString,作为它们 toString 方法的逆运算,解决了 parseInt 的缺点,例如对无效输入返回 NaN 以及缺乏对某些基数的支持。提议的 API 针对无效字符串、无效基数或非字符串输入抛出相应的错误(SyntaxError、RangeError、TypeError),并特意忽略整数字面量前缀和分隔符。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    ECMAScript 提案:{BigInt, Number}.fromString

    状态

    该提案目前处于 TC39 流程的第 1 阶段。

    背景

    BigInt 提案最初包含一个静态的 BigInt.parseInt 方法。经过一些讨论,该方法被移除,取而代之的是一项单独的提案,为 BigIntNumber 都添加一个静态的 fromString 方法。本提案正是如此。

    动机

    {BigInt, Number}.prototype.toString(radix) 使得将数字值转换为该值的字符串表示成为可能。特别是对于 BigInt,目前没有内置的方法来实现逆操作,即将给定基数的 BigInt 字符串表示转换回 BigInt

    对于 Number 值,有 parseInt(string, radix = 10)Number.parseInt(string, radix = 10),但其行为并不理想:

    • string 不表示数字时,它返回 NaN 而不是抛出 SyntaxError 异常。
    • radix 无效(即 radix !== 0 && radix < 2radix > 36)时,它返回 NaN 而不是抛出 RangeError 异常。
    • 它接受基数 0,但将其视为 10,这没有意义。
    • 它忽略前导空白和尾随非数字字符。
    • 它支持十六进制整数字面量前缀 0x0X,但不支持八进制整数字面量前缀 0o0O,也不支持二进制整数字面量前缀 0b0B,这是不一致的。
    • parseInt 对整数字面量前缀有一定程度的支持,这一事实意味着它并不是 toString 的明确对应物。

    提议的解决方案

    我们提议为 BigIntNumber 都扩展一个新的静态 fromString(string, radix = 10) 方法,该方法充当 {BigInt, Number}.prototype.toString(radix = 10) 的逆运算。它只接受可以由 {BigInt, Number}.prototype.toString(radix = 10) 生成的字符串,并对任何其他输入抛出异常。

    高级 API

    Number.fromString('42');
    // → 42
    Number.fromString('42', 10);
    // → 42
    
    BigInt.fromString('42');
    // → 42n
    BigInt.fromString('42', 10);
    // → 42n

    示例

    以下示例使用 Number.fromStringBigInt.fromString 的语义相同,只是它返回 BigInt 而不是 Number

    parseInt 不同,fromString 特意没有对整数字面量前缀进行特殊处理。

    Number.parseInt('0xc0ffee');
    // → 12648430
    Number.parseInt('0o755');
    // → 0
    Number.parseInt('0b00101010');
    // → 0
    
    Number.fromString('0xc0ffee');
    // → SyntaxError
    Number.fromString('0o755');
    // → SyntaxError
    Number.fromString('0b00101010');
    // → SyntaxError
    
    Number.fromString('C0FFEE', 16);
    // → SyntaxError (toString 产生小写数字)
    Number.fromString('c0ffee', 16);
    // → 12648430 === 0xc0ffee
    Number.fromString('755', 8);
    // → 493 === 0o755
    Number.fromString('00101010', 2);
    // → 42 === 0b00101010

    parseInt 不同,当 string 不代表数字时,fromString 会抛出 SyntaxError 异常。

    Number.parseInt('');
    // → NaN
    Number.parseInt(' \n ');
    // → NaN
    Number.parseInt('x');
    // → NaN
    
    Number.fromString('');
    // → SyntaxError
    Number.fromString(' \n ');
    // → SyntaxError
    Number.fromString('x');
    // → SyntaxError

    parseInt 不同,当 radix < 2radix > 36 时,fromString 会抛出 RangeError 异常。

    Number.parseInt('1234', 0);
    // → 1234
    Number.parseInt('1234', 1);
    // → NaN
    Number.parseInt('1234', 37);
    // → NaN
    
    Number.fromString('1234', 0);
    // → RangeError
    Number.fromString('1234', 1);
    // → RangeError
    Number.fromString('1234', 37);
    // → RangeError

    parseInt 不同,当 string 不是字符串时,fromString 会抛出 TypeError 异常。

    Number.parseInt(true, 32);
    // → 978894
    
    Number.fromString(true, 32);
    // → TypeError

    常见问题

    那传统的八进制整数呢?

    fromString 特意没有对传统八进制整数字面量(即没有显式 0o0O 前缀的,如 010)进行特殊处理。换句话说,Number.fromString('010') 会抛出 SyntaxError 异常。

    那数字分隔符呢?

    fromString 不需要支持数字分隔符,因为它们不会出现在 {BigInt, Number}.prototype.toString(radix) 的输出中。Number.fromString('1_000_000_000') 会抛出 SyntaxError 异常。

    BigInt.fromString(string) 支持 n 后缀吗?

    BigInt.fromString 不需要支持 BigInt 字面量中使用的 n 后缀,因为它不会出现在 BigInt.prototype.toString(radix) 的输出中。此外,支持它会在某些基数下产生歧义,其中 n 是一个有效数字:BigInt.fromString('1n', 32) 应该返回 1 还是 55?根据当前提案,BigInt.fromString('1n', 32) 返回 55,而 BigInt.fromString('1n') 抛出 SyntaxError 异常。

    规范

    实现

    • 暂无