How to use the js-quantities.parse function in js-quantities

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
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 nscozzaro / physics-is-beautiful / curricula / static / curricula / js / games / unit_conversion.jsx View on Github external
for (var column = 0; column < answers.length; column++) { // walk through columns
      var numerator = answers[column][0]
      if (numerator['box']) {
        spanNElement = numerator['box'].__controller.container[0]
      }
      var denominator = answers[column][1]
      if (denominator['box']) {
        spanDElement = denominator['box'].__controller.container[0]
      }
      var qnQty, qdQty

      if (this.clearDataText(numerator['data']) === '' || this.clearDataText(denominator['data']) === '') { // catch emtpy text
        qnQty = null
        qdQty = null
      } else {
        qnQty = Qty.parse(this.clearDataText(numerator['data']))
        qdQty = Qty.parse(this.clearDataText(denominator['data']))
      }

      var incorrectKind = false
      var incorrectCount = 0

      if (qnQty && qdQty) {
        // check for unit kind
        var correctQsUnits = [qUnit.split('/')[0]]

        if (qUnit.split('/')[1]) { // km/s
          correctQsUnits.push(qUnit.split('/')[1])
        }

        for (var i = 0; i < correctQsUnits.length; i++) {
          var initialUnitQty = Qty.parse(correctQsUnits[i])
github nscozzaro / physics-is-beautiful / curricula / static / curricula / js / games / unit_conversion.jsx View on Github external
qdQty = Qty.parse(this.clearDataText(denominator['data']))
      }

      var incorrectKind = false
      var incorrectCount = 0

      if (qnQty && qdQty) {
        // check for unit kind
        var correctQsUnits = [qUnit.split('/')[0]]

        if (qUnit.split('/')[1]) { // km/s
          correctQsUnits.push(qUnit.split('/')[1])
        }

        for (var i = 0; i < correctQsUnits.length; i++) {
          var initialUnitQty = Qty.parse(correctQsUnits[i])
          if (initialUnitQty.kind() !== (qnQty.kind() && qdQty.kind())) {
            incorrectCount++
          }
        }
        if (incorrectCount > (correctQsUnits.length - 1)) { // for km/s can be only one incorrect
          incorrectKind = true
        }
      }

      // check conversions steps
      if (qnQty && qdQty && !incorrectKind && qnQty.isCompatible(qdQty) && this.compareWithSigFigs(qnQty, qdQty)) {
        if (spanNElement) { spanNElement.classList.add('green-border') }
        if (spanDElement) { spanDElement.classList.add('green-border') }
      } else {
        if (incorrectKind) {
          this.setState({
github nscozzaro / physics-is-beautiful / curricula / static / curricula / js / games / unit_conversion.jsx View on Github external
incorrectConversion: true
          })
        }
        isRightAnswer = false
        if (spanNElement) { spanNElement.classList.add('red-border') }
        if (spanDElement) { spanDElement.classList.add('red-border') }
      }
    } // end for

    // check answer
    var initialQty = new Qty(Number(qNumber), qUnit)
    var answerSpan = document.getElementById('15')

    var answerQty
    if (this.state.answer && this.state.answer['data'] && this.clearDataText(this.state.answer['data']) !== '') {
      answerQty = Qty.parse(this.clearDataText(this.state.answer['data']))
    } else {
      answerQty = null
    }

    if (answerQty && answerQty.isCompatible(initialQty) && this.compareWithSigFigs(initialQty, answerQty)) {
      answerSpan.classList.add('green-border')
    } else {
      var correctAnswer

      if (initialQty.toBase().scalar < 1) {
        // leave just significant figures for answer
        correctAnswer = this.sigFigs(initialQty.toBase().scalar, 4)
      } else {
        correctAnswer = initialQty.toBase().scalar.toFixed(2)
      }
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
it("should return null when passing an invalid quantity", () => {
      expect(Qty.parse("aa")).toBeNull();
    });
  });
github DefinitelyTyped / DefinitelyTyped / types / js-quantities / js-quantities-tests.ts View on Github external
it("should return parsed quantity when passing a valid quantity", () => {
      expect((Qty.parse("2.5 m") instanceof Qty)).toBe(true);
    });
github DefinitelyTyped / DefinitelyTyped / js-quantities / js-quantities-tests.ts View on Github external
      expect(() => { Qty.parse("foo"); }).not.toThrow("Argument should be a string");
    });
github nscozzaro / physics-is-beautiful / curricula / static / curricula / js / games / unit_conversion.jsx View on Github external
getQtyFromSplitData (splitData) {
    if (splitData) {
      return Qty.parse(splitData[0] + splitData[1])
    }
    return null
  }