Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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...
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...
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])
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({
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)
}
it("should return null when passing an invalid quantity", () => {
expect(Qty.parse("aa")).toBeNull();
});
});
it("should return parsed quantity when passing a valid quantity", () => {
expect((Qty.parse("2.5 m") instanceof Qty)).toBe(true);
});
expect(() => { Qty.parse("foo"); }).not.toThrow("Argument should be a string");
});
getQtyFromSplitData (splitData) {
if (splitData) {
return Qty.parse(splitData[0] + splitData[1])
}
return null
}