@metreeca/blue - v0.9.1
    Preparing search index...

    Module number

    Numeric shape model and factories.

    Defines shapes and factories for validating numeric values, mapping the JSON number type to XSD 1.1 numeric datatypes.

    Warning

    Factories check structural integrity of constraints but not their logical consistency: contradictory constraints like minInclusive > maxInclusive won't be rejected.

    XSD Datatype ¹ Factory Description Range
    byte byte 8-bit signed integer [‑2⁷, 2⁷‑1]
    short short 16-bit signed integer [‑2¹⁵, 2¹⁵‑1]
    int int 32-bit signed integer [‑2³¹, 2³¹‑1]
    long long ² 64-bit signed integer [‑2⁶³, 2⁶³‑1]
    float float IEEE 754 32-bit float m < 2²⁴, e ∈ [‑126, 127]
    double double IEEE 754 64-bit float m < 2⁵³, e ∈ [‑1022, 1023]
    integer integer ² arbitrary-precision integer ±#
    decimal decimal ² arbitrary-precision decimal ±#.#

    ¹ XSD 1.1 datatypes are referenced by RDF 1.1 and JSON-LD 1.1 as normative

    ² Numeric types with ranges exceeding JavaScript's safe integer range (±2⁵³-1) or requiring arbitrary precision cannot be fully represented in JSON/JavaScript

    Compatibility

    JSON XSD JavaScript
    Integer/decimal byte, short, int: fully supported Safe integers within ±2⁵³-1
    long: may exceed ±2⁶³-1 Cannot represent beyond ±2⁵³-1
    No NaN/INF values float, double: has NaN, ±INF IEEE 754 with NaN, ±Infinity
    Safe integer range integer: arbitrary-precision Requires BigInt beyond ±2⁵³-1
    Double precision decimal: arbitrary-precision No native arbitrary decimal

    Defining Numeric Shapes

    import { number, integer, decimal } from '@metreeca/blue';

    const count = number(); // default model: 0
    const score = number({ minInclusive: 0, maxInclusive: 100 }); // constrained range
    const age = integer({ minInclusive: 0 }); // arbitrary-precision integer
    const price = decimal({ minInclusive: 0 }); // arbitrary-precision decimal

    Typed Numeric Factories

    Specialised factories map to XSD numeric datatypes with predefined precision:

    import { byte, short, int, long, float, double } from '@metreeca/blue';

    const priority = byte(); // 8-bit signed integer
    const port = short(); // 16-bit signed integer
    const quantity = int(); // 32-bit signed integer
    const offset = long(); // 64-bit signed integer
    const ratio = float(); // IEEE 754 single-precision
    const measurement = double(); // IEEE 754 double-precision

    Using in Resource Shapes

    import { resource, required, optional, integer, decimal } from '@metreeca/blue';

    const Product = resource({
    price: required(decimal({ minInclusive: 0 })),
    quantity: optional(integer({ minInclusive: 0 })),
    rating: optional(decimal({ minInclusive: 0, maxInclusive: 5 }))
    });

    Interfaces

    NumberShape

    Shape definition for numeric values.

    NumberConstraints

    Constraints for the number shape factory.

    NumericConstraints

    Constraints for numeric shape factories.

    Guards

    isNumberShape

    Checks whether a value is a NumberShape.

    isNumberConstraints

    Checks whether a value is a valid NumberConstraints object.

    isNumericConstraints

    Checks whether a value is a valid NumericConstraints object.

    Factories

    number

    Creates a numeric shape.

    byte

    Creates a shape for 8-bit signed integer values.

    short

    Creates a shape for 16-bit signed integer values.

    int

    Creates a shape for 32-bit signed integer values.

    long

    Creates a shape for 64-bit signed integer values.

    float

    Creates a shape for IEEE 754 single-precision floating-point values.

    double

    Creates a shape for IEEE 754 double-precision floating-point values.

    integer

    Creates a shape for arbitrary-precision integer values.

    decimal

    Creates a shape for arbitrary-precision decimal values.