Skip to content

format() function returns incorrect results until first call triggers initialization #66

@boramuyar

Description

@boramuyar

Bug: format() function returns incorrect results until first call triggers initialization

Description

The format("0.00",0.02) function returns incorrect results (e.g., 0 instead of 0.02) until the module is properly initialized by making an initial call. This appears to be a lazy initialization issue that manifests specifically in webpack-bundled environments.

Environment

  • numfmt version: 3.2.3
  • Node.js version: v24.7.0
  • Bundler: webpack 5.94.0
  • Environment: Browser (Office.js add-in)

Steps to Reproduce

  1. Create a webpack-bundled project
  2. Import and use format() function:
    import { format } from 'numfmt';
    
    console.log(format("0.00", 0.02)); // Returns "0" instead of "0.02"
  3. The function returns incorrect results on the first and subsequent calls

Expected Behavior

format("0.00", 0.02) // Should return "0.02"

Actual Behavior

format("0.00", 0.02) // Returns "0"

Workaround

The issue can be resolved by forcing initialization with a dummy call during module loading:

import { format } from 'numfmt';

// Force initialization
format('0.00', 0.01);

// Now subsequent calls work correctly
console.log(format("0.00", 0.02)); // Returns "0.02" ✅

Additional Context

  • This issue does not occur with Vite bundler - only with webpack
  • The problem appears to be related to lazy initialization of internal module state
  • Adding console.log statements that call the format function also "fixes" the issue, suggesting the first call triggers proper initialization
  • Both named imports (import { format }) and namespace imports (import * as numfmt) exhibit this behavior

Possible Root Cause

The module appears to have lazy initialization that only occurs on the first function call. In webpack environments, this initialization may not complete properly, causing subsequent calls to use uninitialized state.

Suggested Fix

Consider eagerly initializing any required internal state during module load rather than waiting for the first function call, or ensure that the lazy initialization is more robust across different bundling environments.

Thanks for the time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions