npm.nicfv.com
    Preparing search index...

    Get Standard Names

    BACK | 4 | NEXT | Up next: Learn how to create an entirely new dimension and unit system from scratch, without using any defaults.

    You can use Dimensional to determine if a complex dimension matches a default dimension and return the name of that dimension.

    Using the builtin JavaScript function Object.entries() on the default dimensions object, we can iterate through the result until it matches with the argument.

    By passing in \(\textbf{L}\), the function will return Length, which isn't terribly groundbreaking. But we can use this for more complex cases. For example, is there a name for this dimension, once simplified?

    $$\frac{{\textbf{M}}}{{\textbf{L}} \times {\textbf{L}}^{2}}$$

    The same thing can be done for units on the default units object, with a caveat. Dimensions are always stored as a compound of their fundamental base dimensions, but not all units are "made up" of other units, and some units are scales of other units; so we can't give a compound of units and expect to reliably get a result. This is probably best explained in an example.

    By passing in \(\text{J}\), the function will return Joule, a unit of energy, which makes sense so far. Let's assume we have a torque measured in Newton-meters \([\text{N} \cdot \text{m}]\), a valid unit of torque and energy, while Joules are typically a measure of energy, but not torque. Additionally, one Joule is equivalent to one Newton-meter! By passing in that into our function, what should we expect it to return? How would it know the context of our measurement?

    These are a few reasons why it works differently with units versus dimensions. However, we can still get some useful information by asking instead for all default units matching that unit's dimension! Then we can pick and choose which unit best matches our measurement, and easily convert to and from our desired unit.

    Follow these steps to create a new project workspace and install the dimensional dependency to run this example.

    # Create and open project folder
    mkdir Get_Standard_Names_demo
    cd Get_Standard_Names_demo
    # Initialize project and install dependencies
    npm init -y
    npm i dimensional@1.3.1
    # Create and open source file
    touch "Get Standard Names.mjs"
    open "Get Standard Names.mjs"

    Copy and paste this source code into Get Standard Names.mjs.

    import { dimensions, units } from 'dimensional';

    // Use the JavaScript builtins `Object.entries`
    // and `.forEach` loop to iterate through each
    // default dimension to find the match.
    function getDimensionName(dimension) {
    console.log('Looking for match...', dimension.toString());
    let found = false;
    Object.entries(dimensions).forEach(([key, val]) => {
    if (val.is(dimension)) {
    found = true;
    console.log('The unknown dimension is ' + key + '.');
    }
    });
    if (!found) {
    console.log('No default dimension found.');
    }
    }

    // This should give us "Length".
    // Note: Base dimensions have their first letter capitalized!
    getDimensionName(dimensions.Length);


    // Let's derive a dimension using pre-packaged default
    // dimensions and see if it matches one of the defaults
    const complex_dimension = dimensions.Mass.over(dimensions.Length.times(dimensions.area));
    getDimensionName(complex_dimension);

    // The same thing can be done for naming units with the caveat.
    function getUnitName(unit) {
    console.log('Looking for match...', unit.toString());
    let found = false;
    Object.entries(units).forEach(([key, val]) => {
    if (val.is(unit)) {
    found = true;
    console.log('The unknown unit is ' + key + '.');
    }
    });
    if (!found) {
    console.log('No default unit found.');
    }
    }

    // This will work just fine
    getUnitName(units.Joule);

    // Technically, this is also a Joule
    const Newton_meter = units.Newton.times(units.meter);
    getUnitName(Newton_meter);

    // Find all other default units matching dimension
    function getUnitsForDimension(dimension) {
    console.log('Looking for match...', dimension.toString());
    const found = [];
    Object.entries(units).forEach(([key, val]) => {
    if (val.dimensions.is(dimension)) {
    found.push(key);
    }
    });
    if (found.length > 0) {
    console.log('Valid units for measurement: ' + found.join(', '));
    } else {
    console.log('No default units found for dimension.');
    }
    }

    // Shows all default units of energy/torque
    getUnitsForDimension(Newton_meter.dimensions);

    In Get_Standard_Names_demo/, execute Get Standard Names.mjs with NodeJS to generate an output.

    node "Get Standard Names.mjs"
    

    You should expect to see an output similar to the one below.

    Looking for match... {\textbf{L}}
    The unknown dimension is Length.
    Looking for match... \frac{{\textbf{M}}}{{\textbf{L}}^{3}}
    The unknown dimension is density.
    Looking for match... \text{J}
    The unknown unit is Joule.
    Looking for match... \text{N} \cdot \text{m}
    No default unit found.
    Looking for match... \frac{{\textbf{M}} \cdot {\textbf{L}}^{2}}{{\textbf{T}}^{2}}
    Valid units for measurement: Joule, calorie, foodCalorie, BritishThermalUnit