@neutrium/utilities - v5.0.0
    Preparing search index...

    Migration guide

    Utilities 5 moves numerical adapters, numeric contracts, rational helpers and factor operations to @neutrium/math. The former numeric entry points and root numeric exports are removed, with no compatibility re-exports. Existing version 4 adapter contracts and numerical behavior are preserved by the move.

    Cache, validation, configuration, integer, immutable and async APIs remain in utilities. In particular, BigInt integer helpers still come from @neutrium/utilities/integer; the BigIntAdapter backend moves to math. Node.js 24 or later is still required.

    Keep utilities for its remaining features and add math if you use numerical APIs:

    pnpm add @neutrium/utilities@^5.0.0 @neutrium/math
    

    Utilities no longer declares a Decimal peer dependency. Math declares its own Decimal dependency. If your application imports @neutrium/decimal directly, keep it as a direct application dependency as well.

    The following paths are public entry points; do not replace them with deep imports into src or dist.

    Previous import Replacement import
    Numeric contracts and helpers from @neutrium/utilities @neutrium/math/numeric
    @neutrium/utilities/numeric @neutrium/math/numeric for contracts and helpers; import concrete adapters from the paths below
    @neutrium/utilities/numeric/core @neutrium/math/numeric/core
    @neutrium/utilities/numeric/arithmetic @neutrium/math/numeric/arithmetic
    @neutrium/utilities/numeric/scientific @neutrium/math/numeric/scientific
    @neutrium/utilities/numeric/helpers @neutrium/math/numeric/helpers
    @neutrium/utilities/numeric/factors @neutrium/math/numeric/factors
    @neutrium/utilities/numeric/rational @neutrium/math/numeric/rational
    @neutrium/utilities/number @neutrium/math/adapters/number
    @neutrium/utilities/bigint @neutrium/math/adapters/bigint
    @neutrium/utilities/decimal @neutrium/math/adapters/decimal
    @neutrium/utilities/decimal/core @neutrium/math/adapters/decimal/core
    @neutrium/utilities/decimal/arithmetic @neutrium/math/adapters/decimal/arithmetic
    @neutrium/utilities/decimal/scientific @neutrium/math/adapters/decimal/scientific

    Move NumberAdapter and BigIntAdapter imports from the utilities root or numeric barrel to their dedicated math adapter entry points too. Math's root, /number and /decimal entries expose curves; they are not replacements for the old utilities adapter entries. The /adapters/* entries do not load curves, and Decimal core and arithmetic tiers remain independently importable.

    Before (version 4):

    import { LruCache, toSafeInteger } from '@neutrium/utilities';
    import type { ArithmeticAdapter } from '@neutrium/utilities/numeric';
    import { NumberAdapter } from '@neutrium/utilities/number';
    import { DecimalArithmeticAdapter } from '@neutrium/utilities/decimal/arithmetic';

    After (version 5):

    import { LruCache } from '@neutrium/utilities';
    import { toSafeInteger } from '@neutrium/math/numeric';
    import type { ArithmeticAdapter } from '@neutrium/math/numeric';
    import { NumberAdapter } from '@neutrium/math/adapters/number';
    import { DecimalArithmeticAdapter } from '@neutrium/math/adapters/decimal/arithmetic';

    const numeric = new NumberAdapter();
    const decimal = new DecimalArithmeticAdapter({ precision: 40 });

    Update source imports, type-only imports, public re-exports and test fixtures. Remove aliases or bundler rules targeting the old utilities numeric paths, then run your application's type checks and runtime tests. Check Decimal precision, rounding and callback binding where those are part of your application's contract.

    Utilities does not depend on or re-export math. Math depends on utilities for shared helpers. When releasing dependent Neutrium libraries, publish utilities 5 and the math build containing the migrated APIs before publishing consumers such as quantity or pipe.

    Apply the version 5 import changes above and the version 4 contract changes below. The “Before” examples use the historical utilities APIs; the replacement examples use the current math entry points.

    Before (3.x):

    import { numberAdapter } from '@neutrium/utilities/number';
    // The /numeric entry also exported numberAdapter and its doubleAdapter alias.
    numberAdapter.add(1, 2);

    After:

    import { NumberAdapter } from '@neutrium/math/adapters/number';
    const numeric = new NumberAdapter();
    numeric.add(1, 2);

    NumberAdapter now comes from @neutrium/math/adapters/number; the math /numeric entry contains backend-independent contracts and helpers. Replace BinaryAdapter type imports with NumberAdapter when the native implementation matters, or an appropriate generic adapter interface when it does not. BinaryConfig is removed; Number no longer has config, identity, or configurable precision. Methods live on the prototype, so object spread does not copy an adapter's methods. Extend the class to customize it. Native Number methods still support detached calls. Use Object.freeze(new NumberAdapter()) if you need a frozen instance like the old singleton.

    Number methods now trust their typed operands instead of checking their runtime types on every call. Use from to convert external values or isValue to validate them at the boundary. JavaScript consumers must not rely on arithmetic or toBytes throwing for non-number operands. Literal parsing in from and pow, and integer-exponent validation in powInteger, remain in place.

    BigIntAdapter follows the same policy for typed BigInt operands. Its conversion boundary still rejects unsafe Numbers and fractional values, and integer powers still reject negative or noninteger exponents. BigInt byte sizing uses hexadecimal digit counts with the same minimum-one-byte results.

    The /float entry and floatAdapter are removed. NumberAdapter uses JavaScript binary64 arithmetic; replacing the float32 adapter with it changes precision, range, rounding, overflow and underflow. For example, adding 1 to 16777216 previously rounded to 16777216 with float32, but yields 16777217 with NumberAdapter.

    If binary32 behavior is required, provide a custom adapter that consistently applies float32 quantization to construction, operands and operation results. Applying Math.fround only to the final result does not reproduce rounding at each intermediate operation. Use a Decimal adapter when decimal precision and rounding are required.

    The createDecimalAdapter factory and DecimalAdapter singleton exports are removed from all Decimal entry points. Construct DecimalCoreAdapter, DecimalArithmeticAdapter, or DecimalScientificAdapter from the matching tier path instead. The math /adapters/decimal entry exports DecimalScientificAdapter. Constructor-created instances are extensible; their configuration and returned scalars remain immutable. Use Object.freeze(new DecimalArithmeticAdapter(options)) if you also need a frozen adapter instance. withConfig continues to return an independent frozen instance.

    Before, factory methods were closures and could be detached:

    import { createDecimalAdapter } from '@neutrium/utilities/decimal/arithmetic';
    const numeric = createDecimalAdapter({ precision: 40 });
    const parse = numeric.from;
    const values = ['0.1', '0.2'].map(parse);

    After, bind the receiver or call through it:

    import { DecimalArithmeticAdapter } from '@neutrium/math/adapters/decimal/arithmetic';
    const numeric = new DecimalArithmeticAdapter({ precision: 40 });
    const parse = numeric.from.bind(numeric);
    const values = ['0.1', '0.2'].map(parse);
    const higherPrecision = numeric.withConfig({ precision: 60 });

    Use inheritance instead of spreading a Decimal adapter to extend it. withConfig creates the concrete tier class; it does not preserve a custom subclass. Core and arithmetic imports continue to exclude higher-tier implementations.

    The built-in classes now also inherit from one another: scientific extends arithmetic, which extends core. instanceof therefore recognises the lower-tier base classes. Inherited operations retain the scalar type and configuration of the selected tier. Constructors accept the same configuration options as the removed factories.

    Previously, every NumericAdapter<T> exposed identity and config. Those properties are no longer part of the generic contract. Consumers must choose their own compatibility and cache-scoping rules; equal id strings do not establish compatibility. Configurable implementations can expose config and withConfig; use isConfigurableAdapter and NumericOptions from @neutrium/math/numeric/helpers when working with that optional capability.

    Every core adapter must now implement toBytes(value). Every arithmetic or scientific adapter must also implement powInteger(value, exponent). For a custom Number representation, the added methods can look like this:

    import type { ArithmeticAdapter } from '@neutrium/math/numeric';
    import { assertInteger } from '@neutrium/utilities/validation';

    const addedMethods: Pick<ArithmeticAdapter<number>, 'toBytes' | 'powInteger'> = {
    toBytes(value) {
    return 8;
    },
    powInteger(value, exponent) {
    assertInteger(exponent, 'exponent');
    return value ** exponent;
    },
    };

    Add these methods to the existing implementation alongside its other required operations; the example is not a complete adapter. Adapt their implementation to the backend rather than converting custom scalars to Number. powInteger receives the original integer Number exponent, avoiding rounding the exponent through the scalar representation. Reject fractional and nonfinite exponents. Decimal additionally requires a safe integer; BigInt rejects negative powers.

    toBytes describes scalar storage size or a documented estimate, not total retained heap size. Cache owners remain responsible for keys, per-entry overhead and retention policies. Number reports 8 bytes; Decimal delegates to its stored-state estimate. Use toBytes instead of the provisional estimateBytes/estimateScalarBytes APIs, and powInteger instead of the provisional adapterPower helper.

    RationalArithmeticAdapter<T> and hasRationalArithmetic are exported from @neutrium/math/numeric/rational and @neutrium/math/numeric. They describe and detect toRationalTerm, fromRationalSum and compareRationalSum. Decimal arithmetic and scientific adapters provide these methods; custom adapters do not need to implement them to satisfy ArithmeticAdapter<T>.

    Native Number operations keep their native representation. The rational helpers do not introduce a Decimal dependency into native or generic imports.