How to use js-quantities - 10 common examples

To help you get started, we’ve selected a few js-quantities examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
it("should convert value", () => {
          // TODO Same test but with m/h -> ft/s triggers rounding issue
          // (For the sake of speed, converter does not check and fix rounding issues)
          const converter = Qty.swiftConverter("m/h", "m/s");

          expect(converter(2500)).toEqual(Qty("2500 m/h").to("m/s").scalar);
        });
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
it("should convert temperatures to temperature degrees", () => {
      // according to ruby-units, temp -> deg conversion always uses the 0 relative degrees
      let qty = Qty("100 tempC");
      expect(qty.to("degC").scalar).toBe(100);

      qty = Qty("0 tempK");
      expect(qty.to("degC").scalar).toBe(0);

      qty = Qty("0 tempF");
      expect(qty.to("degK").scalar).toBe(0);

      qty = Qty("18 tempF");
      expect(qty.to("degC").scalar).toBe(10);

      qty = Qty("10 tempC");
      expect(qty.to("degF").scalar).toBe(18);
    });
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
it("should divide while trying to avoid numerical errors", () => {
      expect(Qty.divSafe(0.000773, 0.000001)).toBe(773);
      // TODO uncomment and fix
      // expect(Qty.divSafe(24.5, 0.2777777777777778)).toBe(88.2);
    });
  });
github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
it("should divide while trying to avoid numerical errors", () => {
      expect(Qty.divSafe(0.000773, 0.000001)).toBe(773);
      // TODO uncomment and fix
      // expect(Qty.divSafe(24.5, 0.2777777777777778)).toBe(88.2);
    });
  });
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
Qty('100 tempC').div(qty);        // throws error
Qty('100 tempC*unit');            // throws error
Qty('100 tempC/unit');            // throws error
Qty('100 unit/tempC');            // throws error
Qty('100 tempC').inverse();       // throws error

Qty('100 tempC').to('degC'); // => 100 degC

Qty('100 degC').to('tempC'); // => -173.15 tempC

Qty('0 tempC').add('100 degC'); // => 100 tempC

try {
  // code triggering an error inside JS-quantities
} catch (e) {
  if (e instanceof Qty.Error) {
    // ...
  } else {
    // ...
  }
}

// From project spec

describe("js-quantities", () => {
  "use strict";

  describe("initialization", () => {
    it("should create unit only", () => {
      const qty = Qty("m");
      expect(qty.numerator).toEqual(["<meter>"]);
      expect(qty.scalar).toBe(1);</meter>
github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
Qty('100 tempC').div(qty);        // throws error
Qty('100 tempC*unit');            // throws error
Qty('100 tempC/unit');            // throws error
Qty('100 unit/tempC');            // throws error
Qty('100 tempC').inverse();       // throws error

Qty('100 tempC').to('degC'); // =&gt; 100 degC

Qty('100 degC').to('tempC'); // =&gt; -173.15 tempC

Qty('0 tempC').add('100 degC'); // =&gt; 100 tempC

try {
  // code triggering an error inside JS-quantities
} catch (e) {
  if (e instanceof Qty.Error) {
    // ...
  } else {
    // ...
  }
}

// From project spec

describe("js-quantities", () =&gt; {
  "use strict";

  describe("initialization", () =&gt; {
    it("should create unit only", () =&gt; {
      const qty = Qty("m");
      expect(qty.numerator).toEqual(["<meter>"]);
      expect(qty.scalar).toBe(1);</meter>
github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
qty = Qty('1 m^2/s^2');
qty = Qty('1 m^2 s^-2'); // negative powers
qty = Qty('1 m2 s-2'); // ^ is optional

qty = Qty('1 m^2 kg^2 J^2/s^2 A');

qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value

qty = Qty('1 attoparsec/microfortnight');

let qtyCopy: Qty;

qtyCopy = Qty(qty); // quantity could be copied when used as initializing value

Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null

Qty.getKinds(); // => Array of names of every well-known kind of units

Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']

Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]

const qty1 = Qty('1m');
const qty2 = Qty('2m');

qty1.isCompatible(qty2); // => true or false

qty.kind(); // => 'length', 'area', etc...
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
qty = Qty('1 m^2/s^2');
qty = Qty('1 m^2 s^-2'); // negative powers
qty = Qty('1 m2 s-2'); // ^ is optional

qty = Qty('1 m^2 kg^2 J^2/s^2 A');

qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value

qty = Qty('1 attoparsec/microfortnight');

let qtyCopy: Qty;

qtyCopy = Qty(qty); // quantity could be copied when used as initializing value

Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null

Qty.getKinds(); // => Array of names of every well-known kind of units

Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']

Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]

const qty1 = Qty('1m');
const qty2 = Qty('2m');

qty1.isCompatible(qty2); // => true or false

qty.kind(); // => 'length', 'area', etc...
github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
qty = Qty(1.5); // number as initializing value

qty = Qty('1 attoparsec/microfortnight');

let qtyCopy: Qty;

qtyCopy = Qty(qty); // quantity could be copied when used as initializing value

Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null

Qty.getKinds(); // => Array of names of every well-known kind of units

Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']

Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]

const qty1 = Qty('1m');
const qty2 = Qty('2m');

qty1.isCompatible(qty2); // => true or false

qty.kind(); // => 'length', 'area', etc...

qty.isUnitless(); // => true or false
qty.isBase(); // => true if quantity is represented with base units

qty.toBase(); // converts to SI units (10 cm => 0.1 m) (new instance)

qty.toFloat(); // returns scalar of unitless quantity (otherwise throws error)
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value

qty = Qty('1 attoparsec/microfortnight');

let qtyCopy: Qty;

qtyCopy = Qty(qty); // quantity could be copied when used as initializing value

Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null

Qty.getKinds(); // => Array of names of every well-known kind of units

Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']

Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]

const qty1 = Qty('1m');
const qty2 = Qty('2m');

qty1.isCompatible(qty2); // => true or false

qty.kind(); // => 'length', 'area', etc...

qty.isUnitless(); // => true or false
qty.isBase(); // => true if quantity is represented with base units

qty.toBase(); // converts to SI units (10 cm => 0.1 m) (new instance)